Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/games/alien_cake_addict.rs
6592 views
1
//! Eat the cakes. Eat them all. An example 3D game.
2
3
use std::f32::consts::PI;
4
5
use bevy::prelude::*;
6
use rand::{Rng, SeedableRng};
7
use rand_chacha::ChaCha8Rng;
8
9
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
10
enum GameState {
11
#[default]
12
Playing,
13
GameOver,
14
}
15
16
#[derive(Resource)]
17
struct BonusSpawnTimer(Timer);
18
19
fn main() {
20
App::new()
21
.add_plugins(DefaultPlugins)
22
.init_resource::<Game>()
23
.insert_resource(BonusSpawnTimer(Timer::from_seconds(
24
5.0,
25
TimerMode::Repeating,
26
)))
27
.init_state::<GameState>()
28
.add_systems(Startup, setup_cameras)
29
.add_systems(OnEnter(GameState::Playing), setup)
30
.add_systems(
31
Update,
32
(
33
move_player,
34
focus_camera,
35
rotate_bonus,
36
scoreboard_system,
37
spawn_bonus,
38
)
39
.run_if(in_state(GameState::Playing)),
40
)
41
.add_systems(OnEnter(GameState::GameOver), display_score)
42
.add_systems(
43
Update,
44
game_over_keyboard.run_if(in_state(GameState::GameOver)),
45
)
46
.run();
47
}
48
49
struct Cell {
50
height: f32,
51
}
52
53
#[derive(Default)]
54
struct Player {
55
entity: Option<Entity>,
56
i: usize,
57
j: usize,
58
move_cooldown: Timer,
59
}
60
61
#[derive(Default)]
62
struct Bonus {
63
entity: Option<Entity>,
64
i: usize,
65
j: usize,
66
handle: Handle<Scene>,
67
}
68
69
#[derive(Resource, Default)]
70
struct Game {
71
board: Vec<Vec<Cell>>,
72
player: Player,
73
bonus: Bonus,
74
score: i32,
75
cake_eaten: u32,
76
camera_should_focus: Vec3,
77
camera_is_focus: Vec3,
78
}
79
80
#[derive(Resource, Deref, DerefMut)]
81
struct Random(ChaCha8Rng);
82
83
const BOARD_SIZE_I: usize = 14;
84
const BOARD_SIZE_J: usize = 21;
85
86
const RESET_FOCUS: [f32; 3] = [
87
BOARD_SIZE_I as f32 / 2.0,
88
0.0,
89
BOARD_SIZE_J as f32 / 2.0 - 0.5,
90
];
91
92
fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {
93
game.camera_should_focus = Vec3::from(RESET_FOCUS);
94
game.camera_is_focus = game.camera_should_focus;
95
commands.spawn((
96
Camera3d::default(),
97
Transform::from_xyz(
98
-(BOARD_SIZE_I as f32 / 2.0),
99
2.0 * BOARD_SIZE_J as f32 / 3.0,
100
BOARD_SIZE_J as f32 / 2.0 - 0.5,
101
)
102
.looking_at(game.camera_is_focus, Vec3::Y),
103
));
104
}
105
106
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMut<Game>) {
107
let mut rng = if std::env::var("GITHUB_ACTIONS") == Ok("true".to_string()) {
108
// We're seeding the PRNG here to make this example deterministic for testing purposes.
109
// This isn't strictly required in practical use unless you need your app to be deterministic.
110
ChaCha8Rng::seed_from_u64(19878367467713)
111
} else {
112
ChaCha8Rng::from_os_rng()
113
};
114
115
// reset the game state
116
game.cake_eaten = 0;
117
game.score = 0;
118
game.player.i = BOARD_SIZE_I / 2;
119
game.player.j = BOARD_SIZE_J / 2;
120
game.player.move_cooldown = Timer::from_seconds(0.3, TimerMode::Once);
121
122
commands.spawn((
123
DespawnOnExit(GameState::Playing),
124
PointLight {
125
intensity: 2_000_000.0,
126
shadows_enabled: true,
127
range: 30.0,
128
..default()
129
},
130
Transform::from_xyz(4.0, 10.0, 4.0),
131
));
132
133
// spawn the game board
134
let cell_scene =
135
asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/AlienCake/tile.glb"));
136
game.board = (0..BOARD_SIZE_J)
137
.map(|j| {
138
(0..BOARD_SIZE_I)
139
.map(|i| {
140
let height = rng.random_range(-0.1..0.1);
141
commands.spawn((
142
DespawnOnExit(GameState::Playing),
143
Transform::from_xyz(i as f32, height - 0.2, j as f32),
144
SceneRoot(cell_scene.clone()),
145
));
146
Cell { height }
147
})
148
.collect()
149
})
150
.collect();
151
152
// spawn the game character
153
game.player.entity = Some(
154
commands
155
.spawn((
156
DespawnOnExit(GameState::Playing),
157
Transform {
158
translation: Vec3::new(
159
game.player.i as f32,
160
game.board[game.player.j][game.player.i].height,
161
game.player.j as f32,
162
),
163
rotation: Quat::from_rotation_y(-PI / 2.),
164
..default()
165
},
166
SceneRoot(
167
asset_server
168
.load(GltfAssetLabel::Scene(0).from_asset("models/AlienCake/alien.glb")),
169
),
170
))
171
.id(),
172
);
173
174
// load the scene for the cake
175
game.bonus.handle =
176
asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/AlienCake/cakeBirthday.glb"));
177
178
// scoreboard
179
commands.spawn((
180
DespawnOnExit(GameState::Playing),
181
Text::new("Score:"),
182
TextFont {
183
font_size: 33.0,
184
..default()
185
},
186
TextColor(Color::srgb(0.5, 0.5, 1.0)),
187
Node {
188
position_type: PositionType::Absolute,
189
top: px(5),
190
left: px(5),
191
..default()
192
},
193
));
194
195
commands.insert_resource(Random(rng));
196
}
197
198
// control the game character
199
fn move_player(
200
mut commands: Commands,
201
keyboard_input: Res<ButtonInput<KeyCode>>,
202
mut game: ResMut<Game>,
203
mut transforms: Query<&mut Transform>,
204
time: Res<Time>,
205
) {
206
if game.player.move_cooldown.tick(time.delta()).is_finished() {
207
let mut moved = false;
208
let mut rotation = 0.0;
209
210
if keyboard_input.pressed(KeyCode::ArrowUp) {
211
if game.player.i < BOARD_SIZE_I - 1 {
212
game.player.i += 1;
213
}
214
rotation = -PI / 2.;
215
moved = true;
216
}
217
if keyboard_input.pressed(KeyCode::ArrowDown) {
218
if game.player.i > 0 {
219
game.player.i -= 1;
220
}
221
rotation = PI / 2.;
222
moved = true;
223
}
224
if keyboard_input.pressed(KeyCode::ArrowRight) {
225
if game.player.j < BOARD_SIZE_J - 1 {
226
game.player.j += 1;
227
}
228
rotation = PI;
229
moved = true;
230
}
231
if keyboard_input.pressed(KeyCode::ArrowLeft) {
232
if game.player.j > 0 {
233
game.player.j -= 1;
234
}
235
rotation = 0.0;
236
moved = true;
237
}
238
239
// move on the board
240
if moved {
241
game.player.move_cooldown.reset();
242
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
243
translation: Vec3::new(
244
game.player.i as f32,
245
game.board[game.player.j][game.player.i].height,
246
game.player.j as f32,
247
),
248
rotation: Quat::from_rotation_y(rotation),
249
..default()
250
};
251
}
252
}
253
254
// eat the cake!
255
if let Some(entity) = game.bonus.entity
256
&& game.player.i == game.bonus.i
257
&& game.player.j == game.bonus.j
258
{
259
game.score += 2;
260
game.cake_eaten += 1;
261
commands.entity(entity).despawn();
262
game.bonus.entity = None;
263
}
264
}
265
266
// change the focus of the camera
267
fn focus_camera(
268
time: Res<Time>,
269
mut game: ResMut<Game>,
270
mut transforms: ParamSet<(Query<&mut Transform, With<Camera3d>>, Query<&Transform>)>,
271
) {
272
const SPEED: f32 = 2.0;
273
// if there is both a player and a bonus, target the mid-point of them
274
if let (Some(player_entity), Some(bonus_entity)) = (game.player.entity, game.bonus.entity) {
275
let transform_query = transforms.p1();
276
if let (Ok(player_transform), Ok(bonus_transform)) = (
277
transform_query.get(player_entity),
278
transform_query.get(bonus_entity),
279
) {
280
game.camera_should_focus = player_transform
281
.translation
282
.lerp(bonus_transform.translation, 0.5);
283
}
284
// otherwise, if there is only a player, target the player
285
} else if let Some(player_entity) = game.player.entity {
286
if let Ok(player_transform) = transforms.p1().get(player_entity) {
287
game.camera_should_focus = player_transform.translation;
288
}
289
// otherwise, target the middle
290
} else {
291
game.camera_should_focus = Vec3::from(RESET_FOCUS);
292
}
293
// calculate the camera motion based on the difference between where the camera is looking
294
// and where it should be looking; the greater the distance, the faster the motion;
295
// smooth out the camera movement using the frame time
296
let mut camera_motion = game.camera_should_focus - game.camera_is_focus;
297
if camera_motion.length() > 0.2 {
298
camera_motion *= SPEED * time.delta_secs();
299
// set the new camera's actual focus
300
game.camera_is_focus += camera_motion;
301
}
302
// look at that new camera's actual focus
303
for mut transform in transforms.p0().iter_mut() {
304
*transform = transform.looking_at(game.camera_is_focus, Vec3::Y);
305
}
306
}
307
308
// despawn the bonus if there is one, then spawn a new one at a random location
309
fn spawn_bonus(
310
time: Res<Time>,
311
mut timer: ResMut<BonusSpawnTimer>,
312
mut next_state: ResMut<NextState<GameState>>,
313
mut commands: Commands,
314
mut game: ResMut<Game>,
315
mut rng: ResMut<Random>,
316
) {
317
// make sure we wait enough time before spawning the next cake
318
if !timer.0.tick(time.delta()).is_finished() {
319
return;
320
}
321
322
if let Some(entity) = game.bonus.entity {
323
game.score -= 3;
324
commands.entity(entity).despawn();
325
game.bonus.entity = None;
326
if game.score <= -5 {
327
next_state.set(GameState::GameOver);
328
return;
329
}
330
}
331
332
// ensure bonus doesn't spawn on the player
333
loop {
334
game.bonus.i = rng.random_range(0..BOARD_SIZE_I);
335
game.bonus.j = rng.random_range(0..BOARD_SIZE_J);
336
if game.bonus.i != game.player.i || game.bonus.j != game.player.j {
337
break;
338
}
339
}
340
game.bonus.entity = Some(
341
commands
342
.spawn((
343
DespawnOnExit(GameState::Playing),
344
Transform::from_xyz(
345
game.bonus.i as f32,
346
game.board[game.bonus.j][game.bonus.i].height + 0.2,
347
game.bonus.j as f32,
348
),
349
SceneRoot(game.bonus.handle.clone()),
350
children![(
351
PointLight {
352
color: Color::srgb(1.0, 1.0, 0.0),
353
intensity: 500_000.0,
354
range: 10.0,
355
..default()
356
},
357
Transform::from_xyz(0.0, 2.0, 0.0),
358
)],
359
))
360
.id(),
361
);
362
}
363
364
// let the cake turn on itself
365
fn rotate_bonus(game: Res<Game>, time: Res<Time>, mut transforms: Query<&mut Transform>) {
366
if let Some(entity) = game.bonus.entity
367
&& let Ok(mut cake_transform) = transforms.get_mut(entity)
368
{
369
cake_transform.rotate_y(time.delta_secs());
370
cake_transform.scale =
371
Vec3::splat(1.0 + (game.score as f32 / 10.0 * ops::sin(time.elapsed_secs())).abs());
372
}
373
}
374
375
// update the score displayed during the game
376
fn scoreboard_system(game: Res<Game>, mut display: Single<&mut Text>) {
377
display.0 = format!("Sugar Rush: {}", game.score);
378
}
379
380
// restart the game when pressing spacebar
381
fn game_over_keyboard(
382
mut next_state: ResMut<NextState<GameState>>,
383
keyboard_input: Res<ButtonInput<KeyCode>>,
384
) {
385
if keyboard_input.just_pressed(KeyCode::Space) {
386
next_state.set(GameState::Playing);
387
}
388
}
389
390
// display the number of cake eaten before losing
391
fn display_score(mut commands: Commands, game: Res<Game>) {
392
commands.spawn((
393
DespawnOnExit(GameState::GameOver),
394
Node {
395
width: percent(100),
396
align_items: AlignItems::Center,
397
justify_content: JustifyContent::Center,
398
..default()
399
},
400
children![(
401
Text::new(format!("Cake eaten: {}", game.cake_eaten)),
402
TextFont {
403
font_size: 67.0,
404
..default()
405
},
406
TextColor(Color::srgb(0.5, 0.5, 1.0)),
407
)],
408
));
409
}
410
411