-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpong.rs
302 lines (252 loc) · 7.94 KB
/
pong.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use geese::*;
use macroquad::prelude::*;
pub struct GameGraphics {
ctx: GeeseContextHandle<Self>,
draw_rect: Rect,
winning_player: Option<u32>,
}
impl GameGraphics {
fn update_draw_rect(&mut self) {
let side_length = screen_height().min(screen_width());
self.draw_rect = Rect::new(
0.5 * (screen_width() - side_length),
0.5 * (screen_height() - side_length),
side_length,
side_length,
);
}
fn relative_to_absolute(&self, rect: &Rect) -> Rect {
Rect::new(
self.draw_rect.w * rect.x + self.draw_rect.x,
self.draw_rect.h * rect.y + self.draw_rect.y,
self.draw_rect.w * rect.w,
self.draw_rect.h * rect.h,
)
}
fn draw_rect(&self, rect: &Rect, color: Color) {
let absolute = self.relative_to_absolute(rect);
draw_rectangle(absolute.x, absolute.y, absolute.w, absolute.h, color);
}
fn draw_world(&self) {
let world = self.ctx.get::<Store<GameWorld>>();
self.draw_rect(&world.left_paddle_rect(), WHITE);
self.draw_rect(&world.right_paddle_rect(), WHITE);
self.draw_rect(&world.ball_rect(), WHITE);
}
fn end_game(&mut self, event: &on::GameOver) {
if self.winning_player.is_none() {
self.winning_player = Some(event.winning_player);
}
}
fn draw_game(&mut self, _: &on::NewFrame) {
self.update_draw_rect();
if let Some(winner) = self.winning_player {
clear_background(VIOLET);
draw_text(
&format!("Player {winner} wins!"),
self.draw_rect.center().x,
self.draw_rect.center().y,
30.0,
WHITE,
);
} else {
clear_background(DARKBLUE);
draw_rectangle_lines(
self.draw_rect.x,
self.draw_rect.y,
self.draw_rect.w,
self.draw_rect.h,
1.0,
WHITE,
);
self.draw_world();
}
}
}
impl GeeseSystem for GameGraphics {
const DEPENDENCIES: Dependencies = dependencies().with::<Store<GameWorld>>();
const EVENT_HANDLERS: EventHandlers<Self> =
event_handlers().with(Self::draw_game).with(Self::end_game);
fn new(ctx: GeeseContextHandle<Self>) -> Self {
let draw_rect = Rect::default();
let winning_player = None;
Self {
ctx,
draw_rect,
winning_player,
}
}
}
pub struct GameInput {
ctx: GeeseContextHandle<Self>,
}
impl GameInput {
fn move_paddles(&mut self, event: &on::NewFrame) {
let mut world = self.ctx.get_mut::<Store<GameWorld>>();
let movement = world.paddle_move_speed * event.delta_time;
if is_key_down(KeyCode::W) {
world.left_paddle_pos -= movement;
}
if is_key_down(KeyCode::S) {
world.left_paddle_pos += movement;
}
if is_key_down(KeyCode::Up) {
world.right_paddle_pos -= movement;
}
if is_key_down(KeyCode::Down) {
world.right_paddle_pos += movement;
}
let min_height = 0.5 * world.paddle_height;
let max_height = 1.0 - min_height;
world.left_paddle_pos = world.left_paddle_pos.clamp(min_height, max_height);
world.right_paddle_pos = world.right_paddle_pos.clamp(min_height, max_height);
}
}
impl GeeseSystem for GameInput {
const DEPENDENCIES: Dependencies = dependencies().with::<Mut<Store<GameWorld>>>();
const EVENT_HANDLERS: EventHandlers<Self> = event_handlers().with(Self::move_paddles);
fn new(ctx: GeeseContextHandle<Self>) -> Self {
Self { ctx }
}
}
pub struct GamePhysics {
ctx: GeeseContextHandle<Self>,
}
impl GamePhysics {
fn collide_ball_with_paddles(&mut self) {
let mut world = self.ctx.get_mut::<Store<GameWorld>>();
let ball_rect = world.ball_rect();
let collision = ball_rect.overlaps(&if world.ball_velocity.x < 0.0 {
world.left_paddle_rect()
} else {
world.right_paddle_rect()
});
if collision {
world.ball_velocity.x *= -1.0;
world.ball_velocity.y =
rand::gen_range(-world.max_ball_velocity.y, world.max_ball_velocity.y);
}
}
fn collide_ball_with_walls(&mut self) {
let mut world = self.ctx.get_mut::<Store<GameWorld>>();
let ball_rect = world.ball_rect();
let vertical_collision = if world.ball_velocity.y < 0.0 {
ball_rect.bottom() <= 0.0
} else {
ball_rect.top() >= 1.0
};
if vertical_collision {
world.ball_velocity.y *= -1.0;
}
if world.ball_velocity.x < 0.0 {
if ball_rect.left() <= 0.0 {
drop(world);
self.ctx.raise_event(on::GameOver { winning_player: 2 });
}
} else {
if ball_rect.right() >= 1.0 {
drop(world);
self.ctx.raise_event(on::GameOver { winning_player: 1 });
}
}
}
fn move_ball_position(&mut self, frame: &on::NewFrame) {
let mut world = self.ctx.get_mut::<Store<GameWorld>>();
let move_delta = frame.delta_time * world.ball_velocity;
world.ball_position += move_delta;
}
fn move_ball(&mut self, frame: &on::NewFrame) {
self.move_ball_position(frame);
self.collide_ball_with_paddles();
self.collide_ball_with_walls();
}
}
impl GeeseSystem for GamePhysics {
const DEPENDENCIES: Dependencies = dependencies().with::<Mut<Store<GameWorld>>>();
const EVENT_HANDLERS: EventHandlers<Self> = event_handlers().with(Self::move_ball);
fn new(ctx: GeeseContextHandle<Self>) -> Self {
Self { ctx }
}
}
#[derive(Clone, Debug)]
pub struct GameWorld {
pub left_paddle_pos: f32,
pub right_paddle_pos: f32,
pub paddle_width: f32,
pub paddle_height: f32,
pub paddle_move_speed: f32,
pub ball_position: Vec2,
pub ball_velocity: Vec2,
pub ball_size: f32,
pub max_ball_velocity: Vec2,
}
impl GameWorld {
pub fn left_paddle_rect(&self) -> Rect {
Rect::new(
0.0,
self.left_paddle_pos - 0.5 * self.paddle_height,
self.paddle_width,
self.paddle_height,
)
}
pub fn right_paddle_rect(&self) -> Rect {
Rect::new(
1.0 - self.paddle_width,
self.right_paddle_pos - 0.5 * self.paddle_height,
self.paddle_width,
self.paddle_height,
)
}
pub fn ball_rect(&self) -> Rect {
Rect::new(
self.ball_position.x - 0.5 * self.ball_size,
self.ball_position.y - 0.5 * self.ball_size,
self.ball_size,
self.ball_size,
)
}
}
impl Default for GameWorld {
fn default() -> Self {
Self {
left_paddle_pos: 0.5,
right_paddle_pos: 0.5,
paddle_height: 0.2,
paddle_width: 0.05,
paddle_move_speed: 0.5,
ball_position: vec2(0.5, 0.5),
ball_velocity: vec2(0.5, 0.0),
ball_size: 0.025,
max_ball_velocity: vec2(0.7, 1.0),
}
}
}
pub struct PongGame;
impl GeeseSystem for PongGame {
const DEPENDENCIES: Dependencies = dependencies()
.with::<GameGraphics>()
.with::<GameInput>()
.with::<GamePhysics>();
fn new(_: GeeseContextHandle<Self>) -> Self {
Self
}
}
mod on {
pub struct NewFrame {
pub delta_time: f32,
}
pub struct GameOver {
pub winning_player: u32,
}
}
#[macroquad::main("Pong")]
async fn main() {
let mut ctx = GeeseContext::default();
ctx.flush().with(geese::notify::add_system::<PongGame>());
loop {
ctx.flush().with(on::NewFrame {
delta_time: get_frame_time(),
});
next_frame().await;
}
}