From 34f5bc10bde3dec5b56ff993d0ae71f40389e8df Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 3 Oct 2020 14:07:32 +0900 Subject: [PATCH] Impl player sprite --- src/game.rs | 14 +++---- src/main.rs | 6 ++- src/player.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 src/player.rs diff --git a/src/game.rs b/src/game.rs index 6ff4d41..313c594 100644 --- a/src/game.rs +++ b/src/game.rs @@ -62,14 +62,9 @@ impl EventHandler for Game { graphics::present(ctx) } - fn mouse_button_down_event( - &mut self, - ctx: &mut Context, - button: MouseButton, - x: f32, - y: f32, - ) { - self.current_scene.mouse_button_down_event(ctx, button, x, y); + fn mouse_button_down_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) { + self.current_scene + .mouse_button_down_event(ctx, button, x, y); } fn key_down_event( @@ -79,6 +74,7 @@ impl EventHandler for Game { keymods: KeyMods, repeat: bool, ) { - self.current_scene.key_down_event(ctx, keycode, keymods, repeat); + self.current_scene + .key_down_event(ctx, keycode, keymods, repeat); } } diff --git a/src/main.rs b/src/main.rs index b57b134..03c8506 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ mod game; +mod player; mod scenes; -use ggez::event; -use ggez::ContextBuilder; +use ggez::conf::WindowSetup; +use ggez::{event, ContextBuilder}; fn main() { let (mut ctx, mut event_loop) = ContextBuilder::new("ld47_stuckinaloop", "Stephen Seo") + .window_setup(WindowSetup::default().vsync(true)) .build() .unwrap(); diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..e3d1ea7 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,103 @@ +use ggez::event::EventHandler; +use ggez::graphics::{self, Color, DrawParam, Image, Rect}; +use ggez::timer::delta; +use ggez::{Context, GameResult}; + +const WALK_TIME: f32 = 0.4f32; + +pub enum PlayerState { + Standing, + Walking(bool, f32), +} + +pub struct Player { + sprite: Image, + pub x: f32, + pub y: f32, + state: PlayerState, + color: Color, +} + +impl Player { + pub fn new(ctx: &mut Context, color: Color) -> GameResult { + Ok(Self { + sprite: Image::new(ctx, "res/player_sprite.png")?, + x: 0f32, + y: 0f32, + state: PlayerState::Standing, + color, + }) + } + + pub fn set_walking(&mut self, is_walking: bool) { + if is_walking { + self.state = PlayerState::Standing; + } else { + self.state = PlayerState::Walking(true, 0f32); + } + } +} + +impl EventHandler for Player { + fn update(&mut self, ctx: &mut Context) -> GameResult<()> { + let dt = delta(ctx); + match &mut self.state { + PlayerState::Standing => (), + PlayerState::Walking(left, timer) => { + *timer += dt.as_secs_f32(); + if *timer >= WALK_TIME { + *timer -= WALK_TIME; + *left = !*left; + } + } + } + Ok(()) + } + + fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { + match &self.state { + PlayerState::Standing => { + graphics::draw( + ctx, + &self.sprite, + DrawParam::new() + .src(Rect::new(0f32, 0f32, 0.3333333333333f32, 1f32)) + .dest([self.x, self.y]) + .color(self.color), + )?; + } + PlayerState::Walking(left, _) => { + if *left { + graphics::draw( + ctx, + &self.sprite, + DrawParam::new() + .src(Rect::new( + 0.3333333333333f32, + 0f32, + 0.3333333333333f32, + 1f32, + )) + .dest([self.x, self.y]) + .color(self.color), + )?; + } else { + graphics::draw( + ctx, + &self.sprite, + DrawParam::new() + .src(Rect::new( + 0.6666666666666f32, + 0f32, + 0.3333333333333f32, + 1f32, + )) + .dest([self.x, self.y]) + .color(self.color), + )?; + } + } + } + Ok(()) + } +} -- 2.49.0