Skip to main content

scene_demo/
scene_demo.rs

1//! Scene management example with transitions.
2//!
3//! Run with: `cargo run --example scene_demo`
4
5use game_gem::prelude::*;
6
7// ─────────────────────────────────────────────
8// Menu Scene
9// ─────────────────────────────────────────────
10
11struct MenuScene {
12    selected: usize,
13    options: Vec<String>,
14}
15
16impl MenuScene {
17    fn new() -> Self {
18        Self {
19            selected: 0,
20            options: vec![
21                "Play Game".to_string(),
22                "Settings".to_string(),
23                "Quit".to_string(),
24            ],
25        }
26    }
27}
28
29impl Scene for MenuScene {
30    fn on_enter(&mut self, _ctx: &mut Context) {
31        self.selected = 0;
32    }
33
34    fn update(&mut self, ctx: &mut Context) {
35        if ctx.input.keyboard.is_pressed(KeyCode::Up) || ctx.input.keyboard.is_pressed(KeyCode::W) {
36            self.selected = self.selected.saturating_sub(1);
37        }
38        if ctx.input.keyboard.is_pressed(KeyCode::Down) || ctx.input.keyboard.is_pressed(KeyCode::S) {
39            self.selected = (self.selected + 1).min(self.options.len() - 1);
40        }
41        if ctx.input.keyboard.is_pressed(KeyCode::Enter) {
42            match self.selected {
43                0 => {
44                    // Transition to game scene
45                    let _transition = Transition::Fade {
46                        duration: 0.5,
47                        color: [0.0, 0.0, 0.0, 1.0],
48                    };
49                    // ctx.scene.push_with_transition(GameScene::new(), transition);
50                }
51                2 => {
52                    ctx.quit();
53                }
54                _ => {}
55            }
56        }
57    }
58
59    fn render(&mut self, ctx: &mut Context) {
60        ctx.graphics.clear(Color::from_hex("#16213E").unwrap());
61
62        // Title
63        ctx.graphics.draw_text("game-gem Scene Demo", 250.0, 150.0, 36.0, Color::GOLD);
64
65        // Menu options
66        for (i, option) in self.options.iter().enumerate() {
67            let color = if i == self.selected {
68                Color::GOLD
69            } else {
70                Color::LIGHT_GRAY
71            };
72            let prefix = if i == self.selected { "> " } else { "  " };
73            ctx.graphics.draw_text(
74                &format!("{}{}", prefix, option),
75                320.0, 250.0 + i as f32 * 50.0,
76                24.0, color,
77            );
78        }
79
80        ctx.graphics.draw_text("Arrow Keys / WASD to navigate, Enter to select", 200.0, 500.0, 14.0, Color::GRAY);
81    }
82}
83
84// ─────────────────────────────────────────────
85// Main
86// ─────────────────────────────────────────────
87
88struct SceneDemo {
89    scenes: SceneManager,
90}
91
92impl GameState for SceneDemo {
93    fn on_enter(&mut self, _ctx: &mut Context) {
94        self.scenes.push(MenuScene::new());
95    }
96
97    fn update(&mut self, ctx: &mut Context) {
98        self.scenes.update(ctx);
99    }
100
101    fn render(&mut self, ctx: &mut Context) {
102        self.scenes.render(ctx);
103    }
104}
105
106fn main() {
107    let example = SceneDemo {
108        scenes: SceneManager::new(),
109    };
110
111    Game::new()
112        .window_title("game-gem: Scene Demo")
113        .window_size(800, 600)
114        .run(example);
115}