Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom commands instead of observers for spawning #242

Merged
merged 10 commits into from
Aug 8, 2024
20 changes: 12 additions & 8 deletions src/demo/level.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
//! Spawn the main level by triggering other observers.
benfrankel marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
21 changes: 15 additions & 6 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, world::Command},
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved
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,22 @@ 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;

/// Command to spawn the player character.
/// We can add fields to this struct if we need data for spawning.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Command to spawn the player character.
/// We can add fields to this struct if we need data for spawning.
/// A command to spawn the player character.

I don't like the second line here personally. There are a lot of things the user could do to expand on the template, and we're not going to list them all. At the very least this doesn't make sense as a doc comment.

Copy link
Member Author

@janhohenheim janhohenheim Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do however mention things that have a very high probability of coming up and are low-hanging expansions.
I want to keep this comment somewhere in the file, preferably around the struct.
Moved the comment into the struct: d2c59b6 (#242)

#[derive(Debug)]
pub struct SpawnPlayer;

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

fn spawn_player(
_trigger: Trigger<SpawnPlayer>,
mut commands: Commands,
image_handles: Res<ImageHandles>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
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