Skip to content

Commit 1bfaf23

Browse files
committed
Make room in the party zone for more animals, proper points counting, restart completely clears and reloads level
1 parent ef72c1d commit 1bfaf23

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bevy_sandbox"
2+
name = "animal-aggregator"
33
version = "0.1.0"
44
edition = "2021"
55

src/main.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,26 @@ fn main() {
5454
..default()
5555
},
5656
ui_node: None,
57+
despawned_party_animals_count: 0,
5758
})
5859
// .register_inspectable::<Toggles>()
5960
.run();
6061
}
6162

6263
fn any_key_to_restart(
64+
mut commands: Commands,
65+
mut game_resources: ResMut<GameResources>,
6366
mut app_state: ResMut<State<AppState>>,
6467
buttons: Res<Input<GamepadButton>>,
6568
) {
6669
if buttons.get_pressed().count() > 0 {
70+
match game_resources.scene_entity {
71+
Some(entity) => {
72+
commands.entity(entity).despawn_recursive();
73+
}
74+
_ => {}
75+
}
76+
game_resources.despawned_party_animals_count = 0;
6777
app_state.set(AppState::MainMenu).unwrap();
6878
}
6979
}
@@ -118,8 +128,8 @@ fn setup_ui(
118128
}
119129
&AppState::GameOver => format!(
120130
"Congrats! You got {} out of a possible {} animals to the exit! Press any key/button for bonus animal stage :)",
121-
party_animals.iter().count(),
122-
collectables.iter().count()),
131+
party_animals.iter().count() as i32 + game_resources.despawned_party_animals_count,
132+
collectables.iter().count() as i32 + game_resources.despawned_party_animals_count),
123133
&AppState::MainMenu => "Press any button to start!".to_string(),
124134
},
125135
TextStyle {
@@ -235,6 +245,7 @@ struct GameResources {
235245
party_material: StandardMaterial,
236246
scene_entity: Option<Entity>,
237247
ui_node: Option<Entity>,
248+
despawned_party_animals_count: i32,
238249
}
239250

240251
#[derive(Component)]
@@ -277,20 +288,34 @@ fn kill_player(
277288

278289
fn start_the_party(
279290
mut commands: Commands,
280-
game_resources: Res<GameResources>,
291+
mut game_resources: ResMut<GameResources>,
281292
mut materials: ResMut<Assets<StandardMaterial>>,
282293
mut collisions: EventReader<CollisionEvent>,
283294
players: Query<&Children, With<Player>>,
284295
party_zones: Query<(), With<PartyZone>>,
285296
mut material_handles: Query<Entity, With<Handle<StandardMaterial>>>,
297+
party_animals: Query<Entity, With<PartyAnimal>>,
286298
) {
299+
let mut party_count = party_animals.iter().count();
300+
let mut party_iter = party_animals.iter();
287301
for collision in collisions.iter() {
288302
match collision {
289303
&CollisionEvent::Started(a, b, _) => {
290304
if [a, b].iter().any(|&entity| party_zones.contains(entity)) {
291305
for &entity in [a, b].iter() {
292306
match players.get(entity) {
293307
Ok(children) => {
308+
if party_count > 10 {
309+
match party_iter.next() {
310+
Some(entity) => {
311+
commands.entity(entity).despawn_recursive();
312+
}
313+
_ => {}
314+
}
315+
party_count -= 1;
316+
print!("Despawned count {}", game_resources.despawned_party_animals_count);
317+
game_resources.despawned_party_animals_count += 1;
318+
}
294319
commands
295320
.entity(entity)
296321
.remove::<Player>()
@@ -318,12 +343,15 @@ fn start_the_party(
318343

319344
fn gameover_checker(
320345
mut app_state: ResMut<State<AppState>>,
346+
game_resources: Res<GameResources>,
321347
players: Query<(), With<Player>>,
322348
party_animals: Query<(), With<PartyAnimal>>,
323349
) {
324350
if app_state.current() == &AppState::InGame {
325351
if players.iter().count() == 0 {
326-
print!("Game Over! Your score is {}", party_animals.iter().count());
352+
let score_total =
353+
party_animals.iter().count() as i32 + game_resources.despawned_party_animals_count;
354+
print!("Game Over! Your score is {}", score_total);
327355
app_state.set(AppState::GameOver).unwrap();
328356
}
329357
}

0 commit comments

Comments
 (0)