Skip to content

Commit

Permalink
Bevy 0.14.0-rc.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Jun 13, 2024
1 parent 2d4b55e commit d0abfd8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ repository = "https://github.com/Seldom-SE/seldom_state"
leafwing_input = ["dep:leafwing-input-manager"]

[dependencies]
bevy = { version = "0.13.0", default-features = false }
leafwing-input-manager = { git = "https://github.com/Leafwing-Studios/leafwing-input-manager", rev = "a603533", default-features = false, optional = true }
bevy = { version = "0.14.0-rc.2", default-features = false }
either = "1.9"
leafwing-input-manager = { version = "0.13.0", default-features = false, optional = true }
seldom_fn_plugin = "0.6.0"

[dev-dependencies]
bevy = "0.13.0"
leafwing-input-manager = "0.13.0"
bevy = "0.14.0-rc.2"
leafwing-input-manager = { git = "https://github.com/Leafwing-Studios/leafwing-input-manager", rev = "a603533" }

[[example]]
name = "input"
Expand Down
12 changes: 1 addition & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,23 @@ pub mod set;
mod state;
pub mod trigger;

use machine::machine_plugin;
use prelude::*;
use trigger::trigger_plugin;

/// Add to your app to use this crate
#[derive(Debug, Default)]
pub struct StateMachinePlugin;

impl Plugin for StateMachinePlugin {
fn build(&self, app: &mut App) {
app.fn_plugin(state_machine_plugin);
app.add_plugins((machine::plug, trigger::plug));
}
}

/// Function called by [`StateMachinePlugin`]. You may instead call it directly or use
/// `seldom_fn_plugin`, which is another crate I maintain.
pub fn state_machine_plugin(app: &mut App) {
app.fn_plugin(machine_plugin).fn_plugin(trigger_plugin);
}

/// Module for convenient imports. Use with `use seldom_state::prelude::*;`.
pub mod prelude {
pub(crate) use bevy::prelude::*;
#[cfg(feature = "leafwing_input")]
pub(crate) use leafwing_input_manager::prelude::*;
pub(crate) use seldom_fn_plugin::FnPluginExt;

#[cfg(feature = "leafwing_input")]
pub use crate::trigger::{
Expand All @@ -48,7 +39,6 @@ pub mod prelude {
pub use crate::{
machine::StateMachine,
state::{AnyState, EntityState},
state_machine_plugin,
trigger::{always, done, on_event, Done, IntoTrigger, Never, Trigger},
StateMachinePlugin,
};
Expand Down
35 changes: 19 additions & 16 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
};

use bevy::{
ecs::system::{Command, EntityCommands, SystemState},
ecs::{
system::{EntityCommands, SystemState},
world::Command,
},
tasks::{ComputeTaskPool, ParallelSliceMut},
utils::HashMap,
};
Expand All @@ -17,7 +20,7 @@ use crate::{
trigger::{IntoTrigger, TriggerOut},
};

pub(crate) fn machine_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.add_systems(PostUpdate, transition.in_set(StateSet::Transition));
}

Expand Down Expand Up @@ -363,7 +366,7 @@ pub(crate) fn transition(
let par_commands = system_state.get(world);
let task_pool = ComputeTaskPool::get();
// chunk size of None means to automatically pick
borrowed_machines.par_splat_map_mut(task_pool, None, |chunk| {
borrowed_machines.par_splat_map_mut(task_pool, None, |_, chunk| {
for (entity, machine) in chunk {
par_commands.command_scope(|mut commands| machine.run(world, *entity, &mut commands));
}
Expand Down Expand Up @@ -403,11 +406,11 @@ mod tests {
let mut app = App::new();
app.add_systems(Update, transition);
let machine = StateMachine::default().with_state::<StateOne>();
let entity = app.world.spawn((machine, StateOne)).id();
let entity = app.world_mut().spawn((machine, StateOne)).id();
app.update();
// should have moved to state two
assert!(
app.world.get::<StateOne>(entity).is_some(),
app.world().get::<StateOne>(entity).is_some(),
"StateMachine should have the initial component"
);
}
Expand All @@ -420,25 +423,25 @@ mod tests {
let machine = StateMachine::default()
.trans::<StateOne, _>(always, StateTwo)
.trans::<StateTwo, _>(resource_present, StateThree);
let entity = app.world.spawn((machine, StateOne)).id();
let entity = app.world_mut().spawn((machine, StateOne)).id();

assert!(app.world.get::<StateOne>(entity).is_some());
assert!(app.world().get::<StateOne>(entity).is_some());

app.update();
// should have moved to state two
assert!(app.world.get::<StateOne>(entity).is_none());
assert!(app.world.get::<StateTwo>(entity).is_some());
assert!(app.world().get::<StateOne>(entity).is_none());
assert!(app.world().get::<StateTwo>(entity).is_some());

app.update();
// not yet...
assert!(app.world.get::<StateTwo>(entity).is_some());
assert!(app.world.get::<StateThree>(entity).is_none());
assert!(app.world().get::<StateTwo>(entity).is_some());
assert!(app.world().get::<StateThree>(entity).is_none());

app.world.insert_resource(SomeResource);
app.world_mut().insert_resource(SomeResource);
app.update();
// okay, *now*
assert!(app.world.get::<StateTwo>(entity).is_none());
assert!(app.world.get::<StateThree>(entity).is_some());
assert!(app.world().get::<StateTwo>(entity).is_none());
assert!(app.world().get::<StateThree>(entity).is_some());
}

#[test]
Expand All @@ -447,7 +450,7 @@ mod tests {
app.add_systems(Update, transition);

let entity = app
.world
.world_mut()
.spawn((
StateMachine::default().trans::<StateOne, _>(always, StateOne),
StateOne,
Expand All @@ -457,7 +460,7 @@ mod tests {
// the sort of bug this is trying to catch: if you insert the new state and then remove the
// old state, self-transitions will leave you without the state
assert!(
app.world.get::<StateOne>(entity).is_some(),
app.world().get::<StateOne>(entity).is_some(),
"transitioning from a state to itself should work"
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
fmt::{self, Debug, Formatter},
};

use bevy::ecs::system::{Command, EntityCommands};
use bevy::ecs::{system::EntityCommands, world::Command};

use crate::prelude::*;

Expand Down Expand Up @@ -137,17 +137,17 @@ mod tests {
.on_exit::<StateOne>(|commands| commands.commands().insert_resource(SomeResource))
.on_enter::<StateTwo>(|commands| commands.commands().insert_resource(AnotherResource));

let entity = app.world.spawn((machine, StateOne)).id();
assert!(app.world.get::<StateOne>(entity).is_some());
let entity = app.world_mut().spawn((machine, StateOne)).id();
assert!(app.world().get::<StateOne>(entity).is_some());

app.update();
assert!(app.world.get::<StateTwo>(entity).is_some());
assert!(app.world().get::<StateTwo>(entity).is_some());
assert!(
app.world.contains_resource::<SomeResource>(),
app.world().contains_resource::<SomeResource>(),
"exit state triggers should run"
);
assert!(
app.world.contains_resource::<AnotherResource>(),
app.world().contains_resource::<AnotherResource>(),
"exit state triggers should run"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{convert::Infallible, fmt::Debug};

use crate::{prelude::*, set::StateSet};

pub(crate) fn trigger_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.configure_sets(
PostUpdate,
StateSet::RemoveDoneMarkers.after(StateSet::Transition),
Expand Down

0 comments on commit d0abfd8

Please sign in to comment.