-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.rs
354 lines (303 loc) · 10.1 KB
/
main.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use macroquad::prelude::*;
use shipyard::{
AddComponent, AllStoragesViewMut, Component, EntitiesViewMut, IntoIter, IntoWithId, SparseSet,
UniqueView, UniqueViewMut, View, ViewMut, Workload, World,
};
const WIDTH: i32 = 640;
const HEIGHT: i32 = 360;
const INIT_SIZE: f32 = 5.;
const MAX_SIZE: f32 = 25.;
const GROWTH_RATE: f32 = 0.15;
const SPEED: f32 = 1.5;
const ACCELERATION_RATE: f32 = 0.01;
const SQUARE_SPAWN_RATE: u32 = 25;
const SQUAGUM_SPAWN_RATE: u32 = 150;
#[derive(Component)]
struct MyRect(macroquad::prelude::Rect);
#[derive(Component)]
struct Player {
is_invincible: bool,
i_counter: u32,
squagum: bool,
squagum_counter: u32,
rect: Rect,
}
#[derive(Component)]
struct Squagum(Vec2);
#[derive(Component)]
struct Acceleration(f32);
#[derive(Component)]
struct ToDelete;
#[derive(Debug, Component)]
enum GameOver {
Loose,
Victory,
}
impl std::error::Error for GameOver {}
impl std::fmt::Display for GameOver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}
/// generates a new random square.
fn new_square() -> (MyRect, Acceleration) {
(
MyRect(Rect {
x: rand::gen_range(MAX_SIZE / 2.0, WIDTH as f32 - MAX_SIZE / 2.),
y: rand::gen_range(MAX_SIZE / 2.0, HEIGHT as f32 - MAX_SIZE / 2.),
w: INIT_SIZE,
h: INIT_SIZE,
}),
Acceleration(0.),
)
}
fn window_conf() -> Conf {
Conf {
window_title: "Square Eater".to_owned(),
window_width: WIDTH,
window_height: HEIGHT,
..Default::default()
}
}
fn init_world(world: &mut World) {
let _ = world.remove_unique::<Player>();
world
.add_unique(Player {
is_invincible: false,
i_counter: 0,
squagum: false,
squagum_counter: 0,
rect: Rect::new(0., 0., INIT_SIZE * 3., INIT_SIZE * 3.),
})
.unwrap();
world.bulk_add_entity((0..7).map(|_| new_square()));
}
// Entry point of the program
#[macroquad::main(window_conf)]
async fn main() {
let mut world = World::new();
init_world(&mut world);
// seed the random number generator with a random value
rand::srand(macroquad::miniquad::date::now() as u64);
Workload::builder("Game loop")
.with_system(counters)
.with_system(move_player)
.with_system(move_square)
.with_system(grow_square)
.with_system(new_squares)
.with_system(collision)
.with_try_system(clean_up)
.with_system(render)
.add_to_world(&world)
.unwrap();
let mut is_started = false;
loop {
if is_started {
clear_background(WHITE);
if let Err(Some(err)) = world
.run_default()
.map_err(shipyard::error::RunWorkload::custom_error)
{
match err.downcast_ref::<GameOver>().unwrap() {
GameOver::Loose => debug!("GameOver"),
GameOver::Victory => debug!("Victory"),
}
is_started = false;
world.clear();
init_world(&mut world);
}
} else {
if is_mouse_button_pressed(MouseButton::Left) {
is_started = true;
unsafe {
get_internal_gl().quad_context.show_mouse(false);
}
}
clear_background(BLACK);
let text_dimensions = measure_text("Click to start", None, 40, 1.);
draw_text(
"Click to start",
WIDTH as f32 / 2. - text_dimensions.width / 2.,
HEIGHT as f32 / 2. - text_dimensions.height / 2.,
40.,
WHITE,
);
}
next_frame().await
}
}
fn counters(mut player: UniqueViewMut<Player>) {
if player.is_invincible {
player.i_counter += 1;
if player.i_counter >= 10 {
player.is_invincible = false;
player.i_counter = 0;
}
}
if player.squagum {
player.squagum_counter += 1;
if player.squagum_counter >= 120 {
player.squagum = false;
player.squagum_counter = 0;
}
}
}
fn move_player(mut player: UniqueViewMut<Player>) {
let (x, y) = mouse_position();
player.rect.x = x.clamp(player.rect.w / 2., WIDTH as f32 - player.rect.w / 2.);
player.rect.y = y.clamp(player.rect.h / 2., HEIGHT as f32 - player.rect.h / 2.);
}
fn move_square(
player: UniqueView<Player>,
mut rects: ViewMut<MyRect>,
mut accelerations: ViewMut<Acceleration>,
) {
for mut acceleration in (&mut accelerations).iter() {
acceleration.0 += ACCELERATION_RATE;
}
let mut dirs = vec![Vec2::ZERO; rects.len()];
for ((id, MyRect(rect)), dir) in rects.iter().with_id().zip(&mut dirs) {
if rect.w > player.rect.w && rect.h > player.rect.h {
let player_dir = player.rect.point()
- Vec2::new(player.rect.w / 2., player.rect.h / 2.)
- Vec2::new(rect.x - rect.w / 2., rect.y - rect.h / 2.);
*dir = player_dir.normalize();
if player.squagum {
*dir = -*dir;
}
let mut neighbourg_dir = Vec2::ZERO;
for MyRect(neighbourg) in rects.iter() {
if rect.point().distance_squared(neighbourg.point()) < rect.w * rect.h / 1.5 {
neighbourg_dir += Vec2::new(rect.x - neighbourg.x, rect.y - neighbourg.y);
}
}
if rect.w == MAX_SIZE && rect.h == MAX_SIZE {
*dir *= SPEED + accelerations[id].0;
} else {
*dir *= SPEED;
}
*dir += rect.point() + neighbourg_dir * 0.05;
dir.x = dir.x.clamp(INIT_SIZE / 2., WIDTH as f32 - INIT_SIZE / 2.);
dir.y = dir.y.clamp(INIT_SIZE / 2., HEIGHT as f32 - INIT_SIZE / 2.);
}
}
for (rect, dir) in (&mut rects).iter().zip(dirs) {
if dir != Vec2::ZERO {
rect.0.move_to(dir);
}
}
}
fn grow_square(mut rects: ViewMut<MyRect>) {
for mut rect in (&mut rects).iter() {
rect.0.w = (rect.0.w + GROWTH_RATE).min(MAX_SIZE);
rect.0.h = (rect.0.h + GROWTH_RATE).min(MAX_SIZE);
}
}
fn new_squares(
mut entities: EntitiesViewMut,
mut rects: ViewMut<MyRect>,
mut accelerations: ViewMut<Acceleration>,
mut squagums: ViewMut<Squagum>,
) {
if rand::gen_range(0, SQUARE_SPAWN_RATE) == 0 {
entities.add_entity((&mut rects, &mut accelerations), new_square());
}
if rand::gen_range(0, SQUAGUM_SPAWN_RATE) == 0 {
entities.add_entity(
&mut squagums,
Squagum(Vec2::new(
rand::gen_range(0.0, WIDTH as f32),
rand::gen_range(0.0, HEIGHT as f32),
)),
);
}
}
fn collision(
mut player: UniqueViewMut<Player>,
rects: View<MyRect>,
squagums: View<Squagum>,
mut to_delete: ViewMut<ToDelete>,
) {
for (id, squagum) in squagums.iter().with_id() {
if player.rect.contains(squagum.0)
|| player
.rect
.contains(squagum.0 + Vec2::new(INIT_SIZE, INIT_SIZE))
{
player.squagum = true;
to_delete.add_component_unchecked(id, ToDelete);
}
}
for (id, rect) in rects.iter().with_id() {
if rect.0.w == MAX_SIZE
&& rect.0.h == MAX_SIZE
&& rect.0.x - rect.0.w / 2. <= player.rect.x + player.rect.w / 2.
&& rect.0.x + rect.0.w / 2. >= player.rect.x - player.rect.w / 2.
&& rect.0.y - rect.0.h / 2. <= player.rect.y + player.rect.h / 2.
&& rect.0.y + rect.0.h / 2. >= player.rect.y - player.rect.h / 2.
{
if player.squagum {
player.rect.w = (player.rect.w + INIT_SIZE / 4.).min(MAX_SIZE - 0.01);
player.rect.h = (player.rect.h + INIT_SIZE / 4.).min(MAX_SIZE - 0.01);
to_delete.add_component_unchecked(id, ToDelete);
}
if !player.is_invincible {
player.is_invincible = true;
player.rect.w -= INIT_SIZE / 2.;
player.rect.h -= INIT_SIZE / 2.;
}
} else if player.rect.x >= rect.0.w
&& player.rect.h >= rect.0.h
&& player.rect.x - player.rect.w / 2. <= rect.0.x + rect.0.w / 2.
&& player.rect.x + player.rect.w / 2. >= rect.0.x - rect.0.w / 2.
&& player.rect.y - player.rect.h / 2. <= rect.0.y + rect.0.h / 2.
&& player.rect.y + player.rect.h / 2. >= rect.0.y - rect.0.h / 2.
{
player.rect.w = (player.rect.w + INIT_SIZE / 2.).min(MAX_SIZE - 0.01);
player.rect.h = (player.rect.h + INIT_SIZE / 2.).min(MAX_SIZE - 0.01);
to_delete.add_component_unchecked(id, ToDelete)
}
}
}
fn clean_up(mut all_storages: AllStoragesViewMut) -> Result<(), GameOver> {
all_storages.delete_any::<SparseSet<ToDelete>>();
let (player, rects) = all_storages
.borrow::<(UniqueView<Player>, View<MyRect>)>()
.unwrap();
if player.rect.w < INIT_SIZE || player.rect.h < INIT_SIZE {
Err(GameOver::Loose)
} else {
if rects.is_empty() {
Err(GameOver::Victory)
} else {
Ok(())
}
}
}
fn render(player: UniqueView<Player>, rects: View<MyRect>, squagums: View<Squagum>) {
for MyRect(rect) in rects.iter() {
draw_rectangle(
rect.x - rect.w / 2.,
rect.y - rect.h / 2.,
rect.w,
rect.h,
if rect.h == MAX_SIZE && rect.w == MAX_SIZE {
RED
} else if rect.w > player.rect.w && rect.h > player.rect.h {
GRAY
} else {
GREEN
},
);
}
for squagum in squagums.iter() {
draw_rectangle(squagum.0.x, squagum.0.y, INIT_SIZE, INIT_SIZE, YELLOW);
}
draw_rectangle(
player.rect.x - player.rect.w / 2.,
player.rect.y - player.rect.h / 2.,
player.rect.w,
player.rect.h,
if !player.squagum { BLUE } else { YELLOW },
);
}