Refactor the stepper

This commit is contained in:
Jay Oster 2019-10-06 03:31:16 -07:00
parent 63369b8fe0
commit 1e5621c820

View file

@ -7,6 +7,8 @@ type CachedSprite = (usize, usize, Rc<Vec<u8>>);
// Invader positioning // Invader positioning
const START: Point = Point::new(24, 60); const START: Point = Point::new(24, 60);
const GRID: Point = Point::new(16, 16); const GRID: Point = Point::new(16, 16);
const ROWS: usize = 5;
const COLS: usize = 11;
// Screen handling // Screen handling
pub const SCREEN_WIDTH: usize = 224; pub const SCREEN_WIDTH: usize = 224;
@ -142,7 +144,7 @@ impl World {
sprite: SpriteRef::new(&assets, "player1"), sprite: SpriteRef::new(&assets, "player1"),
pos: Point::new(80, 216), pos: Point::new(80, 216),
}; };
let shields = (0..5) let shields = (0..4)
.map(|i| Shield { .map(|i| Shield {
sprite: Sprite::new(&assets, "shield"), sprite: Sprite::new(&assets, "shield"),
pos: Point::new(i * 45 + 32, 192), pos: Point::new(i * 45 + 32, 192),
@ -166,43 +168,37 @@ impl World {
} }
pub fn update(&mut self) { pub fn update(&mut self) {
// Update the next invader // Find the next invader
let row = self.invaders.stepper.row; let mut invader = None;
let col = self.invaders.stepper.col; while let None = invader {
let (col, row) = self.invaders.stepper.incr();
invader = self.invaders.grid[row][col].as_mut();
}
let invader = invader.unwrap();
// Animate the invader // Animate the invader
if let Some(invader) = &mut self.invaders.grid[row][col] { let (pixels, frame) = match invader.sprite.frame.as_ref() {
invader.sprite.frame = match invader.sprite.frame.as_ref() { "blipjoy1" => (
"blipjoy1" => { self.assets.sprites.get("blipjoy2").unwrap().2.clone(),
invader.sprite.pixels = self.assets.sprites.get("blipjoy2").unwrap().2.clone(); "blipjoy2".into(),
"blipjoy2".into() ),
} "blipjoy2" => (
"blipjoy2" => { self.assets.sprites.get("blipjoy1").unwrap().2.clone(),
invader.sprite.pixels = self.assets.sprites.get("blipjoy1").unwrap().2.clone(); "blipjoy1".into(),
"blipjoy1".into() ),
} "ferris1" => (
"ferris1" => { self.assets.sprites.get("ferris2").unwrap().2.clone(),
invader.sprite.pixels = self.assets.sprites.get("ferris2").unwrap().2.clone(); "ferris2".into(),
"ferris2".into() ),
} "ferris2" => (
"ferris2" => { self.assets.sprites.get("ferris1").unwrap().2.clone(),
invader.sprite.pixels = self.assets.sprites.get("ferris1").unwrap().2.clone(); "ferris1".into(),
"ferris1".into() ),
}
_ => unreachable!(), _ => unreachable!(),
}; };
}
// Find the next invader invader.sprite.pixels = pixels;
self.invaders.stepper.col += 1; invader.sprite.frame = frame;
if self.invaders.stepper.col >= 11 {
self.invaders.stepper.col = 0;
if self.invaders.stepper.row == 0 {
self.invaders.stepper.row = 4;
} else {
self.invaders.stepper.row -= 1;
}
}
} }
pub fn draw(&mut self) -> &[u8] { pub fn draw(&mut self) -> &[u8] {
@ -256,9 +252,28 @@ impl std::ops::Mul for Point {
} }
} }
impl Stepper {
fn incr(&mut self) -> (usize, usize) {
self.col += 1;
if self.col >= COLS {
self.col = 0;
if self.row == 0 {
self.row = ROWS - 1;
} else {
self.row -= 1;
}
}
(self.col, self.row)
}
}
impl Default for Stepper { impl Default for Stepper {
fn default() -> Self { fn default() -> Self {
Self { row: 4, col: 0 } Self {
row: 0,
col: COLS - 1,
}
} }
} }
@ -266,8 +281,8 @@ impl Default for Bounds {
fn default() -> Self { fn default() -> Self {
Self { Self {
left: START.x, left: START.x,
right: START.x + 11 * GRID.x, right: START.x + COLS * GRID.x,
bottom: START.y + 5 * GRID.y, bottom: START.y + ROWS * GRID.y,
} }
} }
} }
@ -435,7 +450,7 @@ fn make_invader_grid(assets: &Assets) -> Vec<Vec<Option<Invader>>> {
(0..1) (0..1)
.map(|y| { .map(|y| {
(0..11) (0..COLS)
.map(|x| { .map(|x| {
Some(Invader { Some(Invader {
sprite: SpriteRef::new(assets, "blipjoy1"), sprite: SpriteRef::new(assets, "blipjoy1"),
@ -446,7 +461,7 @@ fn make_invader_grid(assets: &Assets) -> Vec<Vec<Option<Invader>>> {
.collect() .collect()
}) })
.chain((1..3).map(|y| { .chain((1..3).map(|y| {
(0..11) (0..COLS)
.map(|x| { .map(|x| {
Some(Invader { Some(Invader {
sprite: SpriteRef::new(assets, "ferris1"), sprite: SpriteRef::new(assets, "ferris1"),
@ -457,7 +472,7 @@ fn make_invader_grid(assets: &Assets) -> Vec<Vec<Option<Invader>>> {
.collect() .collect()
})) }))
.chain((3..5).map(|y| { .chain((3..5).map(|y| {
(0..11) (0..COLS)
.map(|x| { .map(|x| {
Some(Invader { Some(Invader {
// TODO: Need a third invader // TODO: Need a third invader