]> git.seodisparate.com - LudumDare47_StuckInALoop/commitdiff
Impl player sprite
authorStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 05:07:32 +0000 (14:07 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 05:07:32 +0000 (14:07 +0900)
src/game.rs
src/main.rs
src/player.rs [new file with mode: 0644]

index 6ff4d41587e0513d237dd5b4f92c774d88b0fce7..313c5941c355a93983300756c30b25ff3f5c83cb 100644 (file)
@@ -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);
     }
 }
index b57b134d82054d35908d825cf35ce64c0e70fecd..03c850631be7d2fdcc818f3c2399a6809032cf51 100644 (file)
@@ -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 (file)
index 0000000..e3d1ea7
--- /dev/null
@@ -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<Self> {
+        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(())
+    }
+}