Skip to content

Commit

Permalink
Use custom commands instead of observers for spawning (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Frankel <[email protected]>
  • Loading branch information
janhohenheim and benfrankel authored Aug 8, 2024
1 parent 00d39a8 commit 3ec03f0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/audio/soundtrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ impl Command for PlaySoundtrack {
/// This command will despawn the current soundtrack, then spawn a new one
/// if necessary.
fn apply(self, world: &mut World) {
world.run_system_once_with(self, apply_play_soundtrack);
world.run_system_once_with(self, play_soundtrack);
}
}

fn apply_play_soundtrack(
In(play_soundtrack): In<PlaySoundtrack>,
fn play_soundtrack(
In(config): In<PlaySoundtrack>,
mut commands: Commands,
soundtrack_query: Query<Entity, With<IsSoundtrack>>,
soundtrack_handles: Res<SoundtrackHandles>,
Expand All @@ -40,7 +40,7 @@ fn apply_play_soundtrack(
commands.entity(entity).despawn_recursive();
}

let soundtrack_key = match play_soundtrack {
let soundtrack_key = match config {
PlaySoundtrack::Key(key) => key,
PlaySoundtrack::Disable => return,
};
Expand Down
26 changes: 17 additions & 9 deletions src/demo/level.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
//! Spawn the main level by triggering other observers.
//! Spawn the main level.
use bevy::prelude::*;
use bevy::{ecs::world::Command, prelude::*};

use super::player::SpawnPlayer;

pub(super) fn plugin(app: &mut App) {
app.observe(spawn_level);
pub(super) fn plugin(_app: &mut App) {
// No setup required for this plugin.
// It's still good to have a function here so that we can add some setup
// later if needed.
}

#[derive(Event, Debug)]
#[derive(Debug)]
pub struct SpawnLevel;

fn spawn_level(_trigger: Trigger<SpawnLevel>, mut commands: Commands) {
// The only thing we have in our level is a player,
// but add things like walls etc. here.
commands.trigger(SpawnPlayer);
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();
}
}
29 changes: 22 additions & 7 deletions src/demo/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//! Note that this is separate from the `movement` module as that could be used
//! for other characters as well.
use bevy::prelude::*;
use bevy::{
ecs::{system::RunSystemOnce as _, world::Command},
prelude::*,
};

use crate::{
assets::ImageHandles,
Expand All @@ -15,7 +18,6 @@ use crate::{
};

pub(super) fn plugin(app: &mut App) {
app.observe(spawn_player);
app.register_type::<Player>();

// Record directional input as movement controls.
Expand All @@ -25,15 +27,25 @@ pub(super) fn plugin(app: &mut App) {
);
}

#[derive(Event, Debug)]
pub struct SpawnPlayer;

#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Default, Reflect)]
#[reflect(Component)]
pub struct Player;

/// A command to spawn the player character.
#[derive(Debug)]
pub struct SpawnPlayer {
/// See [`MovementController::max_speed`].
pub max_speed: f32,
}

impl Command for SpawnPlayer {
fn apply(self, world: &mut World) {
world.run_system_once_with(self, spawn_player);
}
}

fn spawn_player(
_trigger: Trigger<SpawnPlayer>,
In(config): In<SpawnPlayer>,
mut commands: Commands,
image_handles: Res<ImageHandles>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
Expand All @@ -59,7 +71,10 @@ fn spawn_player(
layout: texture_atlas_layout.clone(),
index: player_animation.get_atlas_index(),
},
MovementController::default(),
MovementController {
max_speed: config.max_speed,
..default()
},
ScreenWrap,
player_animation,
StateScoped(Screen::Playing),
Expand Down
2 changes: 1 addition & 1 deletion src/screens/playing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(super) fn plugin(app: &mut App) {
}

fn spawn_level(mut commands: Commands) {
commands.trigger(SpawnLevel);
commands.add(SpawnLevel);
commands.play_soundtrack(SoundtrackHandles::KEY_GAMEPLAY);
}

Expand Down

0 comments on commit 3ec03f0

Please sign in to comment.