Skip to content

Commit

Permalink
Simplify commands a bit (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Aug 15, 2024
1 parent d9279be commit 2c6a95a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
20 changes: 7 additions & 13 deletions src/demo/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ pub(super) fn plugin(_app: &mut App) {
// later if needed.
}

#[derive(Debug)]
pub struct SpawnLevel;

impl Command for SpawnLevel {
fn apply(self, world: &mut World) {
// The only thing we have in our level is a player,
// but add things like walls etc. here.
world.commands().add(SpawnPlayer { max_speed: 400.0 });

// Flush the commands we just added so that they are
// all executed now, as part of this command.
world.flush_commands();
}
/// A [`Command`] to spawn the level.
/// Functions that accept only `&mut World` as their parameter implement [`Command`].
/// We use this style when a command requires no configuration.
pub fn spawn_level(world: &mut World) {
// The only thing we have in our level is a player,
// but add things like walls etc. here.
SpawnPlayer { max_speed: 400.0 }.apply(world);
}
6 changes: 4 additions & 2 deletions src/screens/playing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use bevy::{input::common_conditions::input_just_pressed, prelude::*};

use super::Screen;
use crate::{asset_tracking::LoadResource, audio::Music, demo::level::SpawnLevel};
use crate::{
asset_tracking::LoadResource, audio::Music, demo::level::spawn_level as spawn_level_command,
};

pub(super) fn plugin(app: &mut App) {
app.load_resource::<GameplayMusic>();
Expand Down Expand Up @@ -35,7 +37,7 @@ impl FromWorld for GameplayMusic {
}

fn spawn_level(mut commands: Commands, mut music: ResMut<GameplayMusic>) {
commands.add(SpawnLevel);
commands.add(spawn_level_command);
music.entity = Some(
commands
.spawn((
Expand Down

0 comments on commit 2c6a95a

Please sign in to comment.