Skip to content

Commit

Permalink
Fix alien_cake_addict example (#16281)
Browse files Browse the repository at this point in the history
# Objective

Fixes #15729

## Solution

Use the state-scoped pattern.

## Testing

Tested manually. See the showcase.

---

## Showcase



https://github.com/user-attachments/assets/14ffefca-40c6-4c7e-b15b-f92466a2b0a5
  • Loading branch information
BenjaminBrienen authored and mockersf committed Nov 11, 2024
1 parent e3be8f1 commit c53e733
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::prelude::*;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
enum GameState {
#[default]
Playing,
Expand All @@ -25,6 +25,7 @@ fn main() {
TimerMode::Repeating,
)))
.init_state::<GameState>()
.enable_state_scoped_entities::<GameState>()
.add_systems(Startup, setup_cameras)
.add_systems(OnEnter(GameState::Playing), setup)
.add_systems(
Expand All @@ -38,13 +39,11 @@ fn main() {
)
.run_if(in_state(GameState::Playing)),
)
.add_systems(OnExit(GameState::Playing), teardown)
.add_systems(OnEnter(GameState::GameOver), display_score)
.add_systems(
Update,
gameover_keyboard.run_if(in_state(GameState::GameOver)),
)
.add_systems(OnExit(GameState::GameOver), teardown)
.run();
}

Expand Down Expand Up @@ -122,6 +121,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
game.player.move_cooldown = Timer::from_seconds(0.3, TimerMode::Once);

commands.spawn((
StateScoped(GameState::Playing),
PointLight {
intensity: 2_000_000.0,
shadows_enabled: true,
Expand All @@ -140,6 +140,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
.map(|i| {
let height = rng.gen_range(-0.1..0.1);
commands.spawn((
StateScoped(GameState::Playing),
Transform::from_xyz(i as f32, height - 0.2, j as f32),
SceneRoot(cell_scene.clone()),
));
Expand All @@ -153,6 +154,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
game.player.entity = Some(
commands
.spawn((
StateScoped(GameState::Playing),
Transform {
translation: Vec3::new(
game.player.i as f32,
Expand All @@ -176,6 +178,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu

// scoreboard
commands.spawn((
StateScoped(GameState::Playing),
Text::new("Score:"),
TextFont {
font_size: 33.0,
Expand All @@ -193,13 +196,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
commands.insert_resource(Random(rng));
}

// remove all entities that are not a camera or window
fn teardown(mut commands: Commands, entities: Query<Entity, (Without<Camera>, Without<Window>)>) {
for entity in &entities {
commands.entity(entity).despawn();
}
}

// control the game character
fn move_player(
mut commands: Commands,
Expand Down Expand Up @@ -344,6 +340,7 @@ fn spawn_bonus(
game.bonus.entity = Some(
commands
.spawn((
StateScoped(GameState::Playing),
Transform::from_xyz(
game.bonus.i as f32,
game.board[game.bonus.j][game.bonus.i].height + 0.2,
Expand Down Expand Up @@ -393,12 +390,15 @@ fn gameover_keyboard(
// display the number of cake eaten before losing
fn display_score(mut commands: Commands, game: Res<Game>) {
commands
.spawn(Node {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
})
.spawn((
StateScoped(GameState::GameOver),
Node {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
))
.with_child((
Text::new(format!("Cake eaten: {}", game.cake_eaten)),
TextFont {
Expand Down

0 comments on commit c53e733

Please sign in to comment.