]> git.seodisparate.com - LudumDare47_StuckInALoop/commitdiff
More work on the game framework
authorStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 02:57:13 +0000 (11:57 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 02:57:13 +0000 (11:57 +0900)
src/game.rs
src/main.rs
src/scenes/gamestart.rs [new file with mode: 0644]
src/scenes/mod.rs [new file with mode: 0644]
src/subeventhandler.rs [new file with mode: 0644]

index 55266d4f044f8ab779437987a3613d7c1daecd84..0330507eb250cf76be4be99e1a6052937fd79e16 100644 (file)
-use ggez::{Context, GameResult};
 use ggez::event::EventHandler;
 use ggez::graphics;
+use ggez::input::keyboard::{KeyCode, KeyMods};
+use ggez::input::mouse::MouseButton;
+use ggez::{Context, GameResult};
+
+use crate::scenes::gamestart::GameStartScene;
+use crate::subeventhandler::SubEventHandler;
 
 pub struct Game {
+    current_scene: Vec<Box<dyn SubEventHandler>>,
+    state: GameState,
+}
+
+enum GameState {
+    GameStart,
+    Opening,
+}
+
+impl GameState {
+    fn set_scene(&self, ctx: &mut Context, current_scene: &mut Vec<Box<dyn SubEventHandler>>) {
+        current_scene.clear();
+        match self {
+            GameState::GameStart => {
+                current_scene.push(GameStartScene::new_boxed(ctx));
+            }
+            GameState::Opening => {}
+        }
+    }
+
+    fn get_next_state(&self) -> GameState {
+        match self {
+            GameState::GameStart => GameState::Opening,
+            GameState::Opening => GameState::GameStart,
+        }
+    }
 }
 
 impl Game {
-    pub fn new(_ctx: &mut Context) -> Game {
-        Game { }
+    pub fn new(ctx: &mut Context) -> Game {
+        let mut game = Game {
+            current_scene: Vec::new(),
+            state: GameState::GameStart,
+        };
+
+        game.state.set_scene(ctx, &mut game.current_scene);
+
+        game
+    }
+
+    fn scene_next(&mut self) -> GameResult<()> {
+        for scene in &mut self.current_scene {
+            scene.next();
+        }
+        Ok(())
     }
 }
 
 impl EventHandler for Game {
-    fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
+    fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
+        if !self.current_scene.is_empty() {
+            let mut finished = true;
+            for scene in &mut self.current_scene {
+                scene.update(ctx)?;
+                if !scene.finished() {
+                    finished = false;
+                }
+            }
+            if finished {
+                self.state = self.state.get_next_state();
+                self.state.set_scene(ctx, &mut self.current_scene);
+            }
+        } else {
+        }
+
         Ok(())
     }
 
     fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
         graphics::clear(ctx, graphics::WHITE);
 
+        for scene in &mut self.current_scene {
+            scene.draw(ctx)?;
+        }
+
         graphics::present(ctx)
     }
+
+    fn mouse_button_down_event(
+        &mut self,
+        _ctx: &mut Context,
+        _button: MouseButton,
+        _x: f32,
+        _y: f32,
+    ) {
+        if !self.current_scene.is_empty() {
+            self.scene_next().unwrap();
+        }
+    }
+
+    fn key_down_event(
+        &mut self,
+        _ctx: &mut Context,
+        _keycode: KeyCode,
+        _keymods: KeyMods,
+        _repeat: bool,
+    ) {
+        if !self.current_scene.is_empty() {
+            self.scene_next().unwrap();
+        }
+    }
 }
index ca3c2ef892bc303b4b1ec73364ec57bad55bd0f9..ef44fc401aa7c7402c519d94035028b34d280826 100644 (file)
@@ -1,18 +1,19 @@
 mod game;
+mod scenes;
+mod subeventhandler;
 
-use ggez::ContextBuilder;
 use ggez::event;
+use ggez::ContextBuilder;
 
 fn main() {
-    let (mut ctx, mut event_loop) =
-        ContextBuilder::new("ld47_stuckinaloop", "Stephen Seo")
-            .build()
-            .unwrap();
+    let (mut ctx, mut event_loop) = ContextBuilder::new("ld47_stuckinaloop", "Stephen Seo")
+        .build()
+        .unwrap();
 
     let mut game = game::Game::new(&mut ctx);
 
     match event::run(&mut ctx, &mut event_loop, &mut game) {
         Ok(_) => println!("Exited cleanly"),
-        Err(e) => println!("ERROR: {}", e)
+        Err(e) => println!("ERROR: {}", e),
     }
 }
diff --git a/src/scenes/gamestart.rs b/src/scenes/gamestart.rs
new file mode 100644 (file)
index 0000000..0f7d476
--- /dev/null
@@ -0,0 +1,30 @@
+use crate::subeventhandler::SubEventHandler;
+use ggez::{Context, GameResult};
+
+pub struct GameStartScene {}
+
+impl GameStartScene {
+    pub fn new(_ctx: &mut Context) -> Self {
+        Self {}
+    }
+
+    pub fn new_boxed(ctx: &mut Context) -> Box<Self> {
+        Box::new(Self::new(ctx))
+    }
+}
+
+impl SubEventHandler for GameStartScene {
+    fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
+        Ok(())
+    }
+
+    fn draw(&mut self, _ctx: &mut Context) -> GameResult<()> {
+        Ok(())
+    }
+
+    fn next(&mut self) {}
+
+    fn finished(&mut self) -> bool {
+        false
+    }
+}
diff --git a/src/scenes/mod.rs b/src/scenes/mod.rs
new file mode 100644 (file)
index 0000000..6f36287
--- /dev/null
@@ -0,0 +1 @@
+pub mod gamestart;
diff --git a/src/subeventhandler.rs b/src/subeventhandler.rs
new file mode 100644 (file)
index 0000000..8841262
--- /dev/null
@@ -0,0 +1,8 @@
+use ggez::{Context, GameResult};
+
+pub trait SubEventHandler {
+    fn update(&mut self, ctx: &mut Context) -> GameResult<()>;
+    fn draw(&mut self, ctx: &mut Context) -> GameResult<()>;
+    fn next(&mut self);
+    fn finished(&mut self) -> bool;
+}