1use game_gem::prelude::*;
6
7struct BouncingBall {
8 position: Vec2,
9 velocity: Vec2,
10 radius: f32,
11 color: Color,
12 trail: Vec<(Vec2, f32)>, }
14
15impl BouncingBall {
16 fn new() -> Self {
17 Self {
18 position: vec2(400.0, 300.0),
19 velocity: vec2(250.0, -180.0),
20 radius: 30.0,
21 color: Color::GOLD,
22 trail: Vec::with_capacity(60),
23 }
24 }
25}
26
27struct BasicsExample {
28 ball: BouncingBall,
29 hue: f32,
30 click_count: u32,
31}
32
33impl GameState for BasicsExample {
34 fn on_enter(&mut self, ctx: &mut Context) {
35 let mut jump = InputAction::new("shake");
37 jump.bind_key(KeyCode::Space);
38 ctx.input.register_action(jump);
39
40 ctx.graphics.camera.follow(self.ball.position, 0.05);
42 }
43
44 fn update(&mut self, ctx: &mut Context) {
45 let dt = ctx.time.delta() as f32;
46
47 self.ball.velocity.y += 600.0 * dt; self.ball.position += self.ball.velocity * dt;
50
51 let w = ctx.screen_width();
53 let h = ctx.screen_height();
54
55 if self.ball.position.x - self.ball.radius < 0.0 {
56 self.ball.position.x = self.ball.radius;
57 self.ball.velocity.x = self.ball.velocity.x.abs();
58 }
59 if self.ball.position.x + self.ball.radius > w {
60 self.ball.position.x = w - self.ball.radius;
61 self.ball.velocity.x = -self.ball.velocity.x.abs();
62 }
63 if self.ball.position.y - self.ball.radius < 0.0 {
64 self.ball.position.y = self.ball.radius;
65 self.ball.velocity.y = self.ball.velocity.y.abs();
66 }
67 if self.ball.position.y + self.ball.radius > h {
68 self.ball.position.y = h - self.ball.radius;
69 self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; }
71
72 self.ball.trail.push((self.ball.position, 1.0));
74 if self.ball.trail.len() > 60 {
75 self.ball.trail.remove(0);
76 }
77 for (_, alpha) in &mut self.ball.trail {
78 *alpha -= dt * 2.0;
79 }
80 self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82 if ctx.input.is_action_pressed("shake") {
84 ctx.graphics.camera.shake(10.0, 0.3);
85 }
86
87 if ctx.input.mouse.is_pressed(MouseButton::Left) {
88 self.click_count += 1;
89 self.hue = (self.hue + 30.0) % 360.0;
91 self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92 let dir = ctx.input.mouse.position - self.ball.position;
94 self.ball.velocity += dir.normalize_or_zero() * 200.0;
95 }
96
97 let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99 ctx.graphics.camera.set_zoom(zoom);
100
101 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103 ctx.quit();
104 }
105
106 ctx.graphics.camera.follow(self.ball.position, 0.05);
108 }
109
110 fn render(&mut self, ctx: &mut Context) {
111 ctx.graphics.clear(ctx.window.clear_color);
112
113 for &(pos, alpha) in &self.ball.trail {
115 ctx.graphics.draw_circle(
116 pos.x, pos.y,
117 self.ball.radius * 0.5 * alpha,
118 self.ball.color.with_alpha(alpha * 0.5),
119 );
120 }
121
122 ctx.graphics.draw_circle(
124 self.ball.position.x,
125 self.ball.position.y,
126 self.ball.radius,
127 self.ball.color,
128 );
129
130 ctx.graphics.reset_camera();
132 ctx.graphics.draw_text(
133 &format!("FPS: {:.0}", ctx.time.fps()),
134 10.0, 10.0, 18.0, Color::WHITE,
135 );
136 ctx.graphics.draw_text(
137 &format!("Clicks: {}", self.click_count),
138 10.0, 35.0, 18.0, Color::WHITE,
139 );
140 ctx.graphics.draw_text(
141 "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142 10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143 );
144 }
145}
146
147fn main() {
148 let example = BasicsExample {
149 ball: BouncingBall::new(),
150 hue: 0.0,
151 click_count: 0,
152 };
153
154 Game::new()
155 .window_title("game-gem: Basics Example")
156 .window_size(800, 600)
157 .clear_color(Color::from_hex("#0F0F23").unwrap())
158 .run(example);
159}