diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-03-13 18:51:49 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-03-13 18:51:49 +0100 |
commit | e3238817205b2c5d2504330135ffc552881b1a73 (patch) | |
tree | 0e10e2f2b2398bd6a3b6c37a691f90b0ab95b561 /src | |
parent | 456577c767c243f4521285840280c02f9a3d653d (diff) |
setup animation for astronaut
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 121 |
1 files changed, 115 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index 59a4f8c..13c5c6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,111 @@ impl Distribution<Occupant> for Standard { } #[derive(Clone, Copy)] +enum CosmonautFrames { + None, + One, + Two, + Three, + Four, +} + +impl CosmonautFrames { + pub fn next(&mut self) { + *self = match self { + CosmonautFrames::None => CosmonautFrames::One, + CosmonautFrames::One => CosmonautFrames::Two, + CosmonautFrames::Two => CosmonautFrames::Three, + CosmonautFrames::Three => CosmonautFrames::Four, + CosmonautFrames::Four => CosmonautFrames::None, + }; + } +} + +struct Cosmonaut { + destination: Point2<f32>, + image: Image, + frame: CosmonautFrames, + timer: Instant, + scale: Vector2<f32>, +} + +impl Cosmonaut { + pub fn new(context: &mut Context) -> GameResult<Cosmonaut> { + let mut image = Image::new(context, "/cosmonaut.png")?; + image.set_filter(FilterMode::Nearest); + + Ok(Cosmonaut { + image, + destination: Point2 { x: 600.0, y: 200.0 }, + frame: CosmonautFrames::None, + timer: Instant::now(), + scale: Vector2 { + x: TILE_SCALE * 2.0, + y: TILE_SCALE * 2.0, + }, + }) + } + + pub fn draw(&self, context: &mut Context) -> GameResult { + graphics::draw( + context, + &self.image, + DrawParam::default() + .dest(self.destination) + .scale(self.scale) + .src(Rect::new(0.0, 0.0, 1.0 / 3.0, 1.0)), + )?; + let source = match self.frame { + CosmonautFrames::None => None, + CosmonautFrames::One => Some(Rect::new(1.0 / 3.0, 0.0, 1.0 / 3.0, 1.0 / 2.0)), + CosmonautFrames::Two => Some(Rect::new(2.0 / 3.0, 0.0, 1.0, 1.0 / 2.0)), + CosmonautFrames::Three => Some(Rect::new(1.0 / 3.0, 1.0 / 2.0, 1.0 / 3.0, 1.0 / 2.0)), + CosmonautFrames::Four => Some(Rect::new(2.0 / 3.0, 1.0 / 2.0, 1.0, 1.0)), + }; + if let Some(source) = source { + graphics::draw( + context, + &self.image, + DrawParam::default() + .dest(self.destination) + .src(source) + .scale(self.scale), + )?; + } + + Ok(()) + } + + pub fn update(&mut self) { + match self.frame { + CosmonautFrames::None => (), + _ => { + if self.timer.elapsed().as_millis() > 50 { + self.frame.next(); + self.timer = Instant::now(); + } + } + } + } + + pub fn start(&mut self) { + if let CosmonautFrames::None = self.frame { + if self.timer.elapsed().as_secs() > 5 { + self.timer = Instant::now(); + self.frame.next() + } + } + } + + pub fn contains(&self, position: Point2<f32>) -> bool { + position.x > self.destination.x + && position.y > self.destination.y + && position.x < self.destination.x + TILE_WIDTH * TILE_SCALE * 2.0 + && position.y < self.destination.y + TILE_WIDTH * TILE_SCALE * 2.0 + } +} + +#[derive(Clone, Copy)] struct Cell { occupant: Occupant, position: Point2<f32>, @@ -62,15 +167,10 @@ impl Cell { } pub fn contains(&self, position: Point2<f32>) -> bool { - if position.x > self.position.x + position.x > self.position.x && position.y > self.position.y && position.x < self.position.x + TILE_WIDTH * TILE_SCALE && position.y < self.position.y + TILE_WIDTH * TILE_SCALE - { - return true; - } - - false } pub fn moveable(&self) -> bool { @@ -162,6 +262,7 @@ struct Game { spritebatch: SpriteBatch, grid: Vec<Vec<Cell>>, background: Image, + cosmonaut: Cosmonaut, } impl Game { @@ -191,6 +292,7 @@ impl Game { Ok(Game { grid, selected: None, + cosmonaut: Cosmonaut::new(context)?, spritebatch: SpriteBatch::new(image), background, }) @@ -312,6 +414,12 @@ impl EventHandler for Game { let position = mouse::position(context); + if self.cosmonaut.contains(position) { + self.cosmonaut.start(); + } + + self.cosmonaut.update(); + for row in self.grid.iter_mut() { for cell in row.iter_mut() { if cell.contains(position) { @@ -391,6 +499,7 @@ impl EventHandler for Game { fn draw(&mut self, context: &mut Context) -> GameResult { graphics::clear(context, [0.0, 0.0, 0.0, 1.0].into()); graphics::draw(context, &self.background, DrawParam::default())?; + self.cosmonaut.draw(context)?; for row in self.grid.iter() { for cell in row.iter() { |