feat: different enemy sprites

fix: lines at edge of sprites
This commit is contained in:
thetek 2023-06-18 15:35:17 +02:00
parent 9cccbe610c
commit b4102193d9
2 changed files with 41 additions and 14 deletions

View File

@ -20,6 +20,7 @@ struct Player {
struct Enemy {
pos_x: f32,
pos_y: f32,
sprite_type: u8,
}
struct Bullet {
@ -70,8 +71,12 @@ impl Game {
let mut pos_y = config::ENEMY_OFFSET_TOP;
for _ in 0..config::ENEMY_COLS {
for _ in 0..config::ENEMY_ROWS {
v.push(Enemy { pos_x, pos_y });
for row in 0..config::ENEMY_ROWS {
v.push(Enemy {
pos_x,
pos_y,
sprite_type: row as u8,
});
pos_y -= config::ENEMY_ROW_SKIP;
}
pos_x += config::ENEMY_COL_SKIP;
@ -314,17 +319,37 @@ impl Enemy {
const ENEMY_WIDTH_HALF: f32 = (config::ENEMY_WIDTH * config::ENEMY_PIXEL_SIZE) / 2.0;
const ENEMY_HEIGHT_HALF: f32 = (config::ENEMY_HEIGHT * config::ENEMY_PIXEL_SIZE) / 2.0;
let to = (((time % 2000.0) / 1000.0) as i32) as f32 / 2.0;
let to_time = (((time % 2000.0) / 1000.0) as i32) as f32 * 0.125;
let to_sprite = self.sprite_type as f32 * 0.25;
let to = to_time + to_sprite;
let left = self.pos_x - ENEMY_WIDTH_HALF;
let right = self.pos_x + ENEMY_WIDTH_HALF;
let top = self.pos_y - ENEMY_HEIGHT_HALF;
let bottom = self.pos_y + ENEMY_HEIGHT_HALF;
vertices.append(&mut vec![left, top, 0.0, 1.0, 1.0, 1.0, 0.0, 0.5 + to]);
vertices.append(&mut vec![left, top, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0625 + to]);
vertices.append(&mut vec![left, bottom, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0 + to]);
vertices.append(&mut vec![right, bottom, 0.0, 1.0, 1.0, 1.0, 0.5, 0.0 + to]);
vertices.append(&mut vec![right, top, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5 + to]);
vertices.append(&mut vec![
right,
bottom,
0.0,
1.0,
1.0,
1.0,
0.09375,
0.0 + to,
]);
vertices.append(&mut vec![
right,
top,
0.0,
1.0,
1.0,
1.0,
0.09375,
0.0625 + to,
]);
let indices = vec![0, 1, 2, 0, 2, 3];
@ -341,10 +366,12 @@ impl Player {
let bottom = config::PLAYER_OFFSET_BOTTOM;
let top = bottom + config::PLAYER_HEIGHT;
vertices.append(&mut vec![left, top, 0.0, 1.0, 1.0, 1.0, 0.5, 0.0]);
vertices.append(&mut vec![left, bottom, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5]);
vertices.append(&mut vec![right, bottom, 0.0, 1.0, 1.0, 1.0, 1.0, 0.5]);
vertices.append(&mut vec![right, top, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]);
vertices.append(&mut vec![left, top, 0.0, 1.0, 1.0, 1.0, 0.125, 0.0]);
vertices.append(&mut vec![left, bottom, 0.0, 1.0, 1.0, 1.0, 0.125, 0.0625]);
vertices.append(&mut vec![
right, bottom, 0.0, 1.0, 1.0, 1.0, 0.21875, 0.0625,
]);
vertices.append(&mut vec![right, top, 0.0, 1.0, 1.0, 1.0, 0.21875, 0.0]);
let indices = vec![0, 1, 2, 0, 2, 3];
@ -372,10 +399,10 @@ impl Bullet {
let (r, g, b) = (self.color[0], self.color[1], self.color[2]);
vertices.append(&mut vec![left, top, 0.0, r, g, b, 0.2, 0.2]);
vertices.append(&mut vec![left, bottom, 0.0, r, g, b, 0.2, 0.3]);
vertices.append(&mut vec![right, bottom, 0.0, r, g, b, 0.3, 0.2]);
vertices.append(&mut vec![right, top, 0.0, r, g, b, 0.3, 0.3]);
vertices.append(&mut vec![left, top, 0.0, r, g, b, 0.9375, 0.9375]);
vertices.append(&mut vec![left, bottom, 0.0, r, g, b, 0.9375, 1.0]);
vertices.append(&mut vec![right, bottom, 0.0, r, g, b, 1.0, 1.0]);
vertices.append(&mut vec![right, top, 0.0, r, g, b, 1.0, 0.9375]);
let indices = vec![0, 1, 2, 0, 2, 3];

BIN
www/dist/texture.png vendored

Binary file not shown.

Before

Width:  |  Height:  |  Size: 213 B

After

Width:  |  Height:  |  Size: 719 B