mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
Fix alien_cake_addict
example (#16281)
# 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
This commit is contained in:
parent
9beb1d96e7
commit
94f2fe35f7
1 changed files with 16 additions and 16 deletions
|
@ -6,7 +6,7 @@ use bevy::prelude::*;
|
||||||
use rand::{Rng, SeedableRng};
|
use rand::{Rng, SeedableRng};
|
||||||
use rand_chacha::ChaCha8Rng;
|
use rand_chacha::ChaCha8Rng;
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
||||||
enum GameState {
|
enum GameState {
|
||||||
#[default]
|
#[default]
|
||||||
Playing,
|
Playing,
|
||||||
|
@ -25,6 +25,7 @@ fn main() {
|
||||||
TimerMode::Repeating,
|
TimerMode::Repeating,
|
||||||
)))
|
)))
|
||||||
.init_state::<GameState>()
|
.init_state::<GameState>()
|
||||||
|
.enable_state_scoped_entities::<GameState>()
|
||||||
.add_systems(Startup, setup_cameras)
|
.add_systems(Startup, setup_cameras)
|
||||||
.add_systems(OnEnter(GameState::Playing), setup)
|
.add_systems(OnEnter(GameState::Playing), setup)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
|
@ -38,13 +39,11 @@ fn main() {
|
||||||
)
|
)
|
||||||
.run_if(in_state(GameState::Playing)),
|
.run_if(in_state(GameState::Playing)),
|
||||||
)
|
)
|
||||||
.add_systems(OnExit(GameState::Playing), teardown)
|
|
||||||
.add_systems(OnEnter(GameState::GameOver), display_score)
|
.add_systems(OnEnter(GameState::GameOver), display_score)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
gameover_keyboard.run_if(in_state(GameState::GameOver)),
|
gameover_keyboard.run_if(in_state(GameState::GameOver)),
|
||||||
)
|
)
|
||||||
.add_systems(OnExit(GameState::GameOver), teardown)
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
game.player.move_cooldown = Timer::from_seconds(0.3, TimerMode::Once);
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
StateScoped(GameState::Playing),
|
||||||
PointLight {
|
PointLight {
|
||||||
intensity: 2_000_000.0,
|
intensity: 2_000_000.0,
|
||||||
shadows_enabled: true,
|
shadows_enabled: true,
|
||||||
|
@ -140,6 +140,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
let height = rng.gen_range(-0.1..0.1);
|
let height = rng.gen_range(-0.1..0.1);
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
StateScoped(GameState::Playing),
|
||||||
Transform::from_xyz(i as f32, height - 0.2, j as f32),
|
Transform::from_xyz(i as f32, height - 0.2, j as f32),
|
||||||
SceneRoot(cell_scene.clone()),
|
SceneRoot(cell_scene.clone()),
|
||||||
));
|
));
|
||||||
|
@ -153,6 +154,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
|
||||||
game.player.entity = Some(
|
game.player.entity = Some(
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
StateScoped(GameState::Playing),
|
||||||
Transform {
|
Transform {
|
||||||
translation: Vec3::new(
|
translation: Vec3::new(
|
||||||
game.player.i as f32,
|
game.player.i as f32,
|
||||||
|
@ -176,6 +178,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
|
||||||
|
|
||||||
// scoreboard
|
// scoreboard
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
StateScoped(GameState::Playing),
|
||||||
Text::new("Score:"),
|
Text::new("Score:"),
|
||||||
TextFont {
|
TextFont {
|
||||||
font_size: 33.0,
|
font_size: 33.0,
|
||||||
|
@ -193,13 +196,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
|
||||||
commands.insert_resource(Random(rng));
|
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
|
// control the game character
|
||||||
fn move_player(
|
fn move_player(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
@ -344,6 +340,7 @@ fn spawn_bonus(
|
||||||
game.bonus.entity = Some(
|
game.bonus.entity = Some(
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
StateScoped(GameState::Playing),
|
||||||
Transform::from_xyz(
|
Transform::from_xyz(
|
||||||
game.bonus.i as f32,
|
game.bonus.i as f32,
|
||||||
game.board[game.bonus.j][game.bonus.i].height + 0.2,
|
game.board[game.bonus.j][game.bonus.i].height + 0.2,
|
||||||
|
@ -393,12 +390,15 @@ fn gameover_keyboard(
|
||||||
// display the number of cake eaten before losing
|
// display the number of cake eaten before losing
|
||||||
fn display_score(mut commands: Commands, game: Res<Game>) {
|
fn display_score(mut commands: Commands, game: Res<Game>) {
|
||||||
commands
|
commands
|
||||||
.spawn(Node {
|
.spawn((
|
||||||
width: Val::Percent(100.),
|
StateScoped(GameState::GameOver),
|
||||||
align_items: AlignItems::Center,
|
Node {
|
||||||
justify_content: JustifyContent::Center,
|
width: Val::Percent(100.),
|
||||||
..default()
|
align_items: AlignItems::Center,
|
||||||
})
|
justify_content: JustifyContent::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
))
|
||||||
.with_child((
|
.with_child((
|
||||||
Text::new(format!("Cake eaten: {}", game.cake_eaten)),
|
Text::new(format!("Cake eaten: {}", game.cake_eaten)),
|
||||||
TextFont {
|
TextFont {
|
||||||
|
|
Loading…
Reference in a new issue