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

Update to latest Bevy #24

Merged
merged 8 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.idea/
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exclude = ["assets"]
default = [ "fixedtimestep", "states", "bevy-compat", "app" ]
fixedtimestep = [
"bevy_core",
"bevy_time"
]
states = [
"bevy_utils",
Expand All @@ -32,6 +33,7 @@ bevy_ecs = { git = "https://github.com/bevyengine/bevy" }
bevy_app = { git = "https://github.com/bevyengine/bevy", optional = true }
bevy_core = { git = "https://github.com/bevyengine/bevy", optional = true }
bevy_utils = { git = "https://github.com/bevyengine/bevy", optional = true }
bevy_time = { git = "https://github.com/bevyengine/bevy", optional = true }

[dev-dependencies]
bevy = { git = "https://github.com/bevyengine/bevy" }
Expand Down
2 changes: 1 addition & 1 deletion examples/fixedtimestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ fn reposition_entities(mut q: Query<&mut Transform, With<MySprite>>) {
}

fn setup_camera(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(Camera2dBundle::default());
}
26 changes: 9 additions & 17 deletions examples/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy::prelude::*;
use iyes_loopless::prelude::*;

use bevy::app::AppExit;
use bevy::input::system::exit_on_esc_system;
use bevy::window::close_on_esc;

use std::time::Duration;

Expand Down Expand Up @@ -49,16 +49,14 @@ fn main() {
.add_enter_system(GameState::MainMenu, setup_menu)
// menu cleanup (state exit) systems
.add_exit_system(GameState::MainMenu, despawn_with::<MainMenu>)
// game setup (state enter) systems
.add_enter_system(GameState::InGame, setup_game_camera)
// game cleanup (state exit) systems
.add_exit_system(GameState::InGame, despawn_with::<MySprite>)
.add_exit_system(GameState::InGame, despawn_with::<GameCamera>)
// menu stuff
.add_system_set(
ConditionSet::new()
.run_in_state(GameState::MainMenu)
.with_system(exit_on_esc_system)
.with_system(close_on_esc)
.with_system(butt_interact_visual)
// our menu button handlers
.with_system(butt_exit.run_if(on_butt_interact::<ExitButt>))
Expand All @@ -77,7 +75,7 @@ fn main() {
// our other various systems:
.add_system(debug_current_state)
// setup our UI camera globally at startup and keep it alive at all times
.add_startup_system(setup_ui_camera)
.add_startup_system(setup_camera)
.run();
}

Expand Down Expand Up @@ -154,15 +152,9 @@ fn spawn_sprite(mut commands: Commands) {
.insert(MySprite);
}

/// Spawn the UI camera
fn setup_ui_camera(mut commands: Commands) {
commands.spawn_bundle(UiCameraBundle::default());
}

/// Spawn the game camera
fn setup_game_camera(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d())
.insert(GameCamera);
/// Spawn the camera
fn setup_camera(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default()).insert(GameCamera);
}

/// Rotate all the sprites
Expand Down Expand Up @@ -222,8 +214,8 @@ fn setup_menu(mut commands: Commands, ass: Res<AssetServer>) {
let butt_style = Style {
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
padding: Rect::all(Val::Px(8.0)),
margin: Rect::all(Val::Px(4.0)),
padding: UiRect::all(Val::Px(8.0)),
margin: UiRect::all(Val::Px(4.0)),
flex_grow: 1.0,
..Default::default()
};
Expand All @@ -238,7 +230,7 @@ fn setup_menu(mut commands: Commands, ass: Res<AssetServer>) {
color: UiColor(Color::rgb(0.5, 0.5, 0.5)),
style: Style {
size: Size::new(Val::Auto, Val::Auto),
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
align_self: AlignSelf::Center,
flex_direction: FlexDirection::ColumnReverse,
//align_items: AlignItems::Stretch,
Expand Down
4 changes: 2 additions & 2 deletions src/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,12 @@ impl ConditionSet {
}

pub fn before<Marker>(mut self, label: impl AsSystemLabel<Marker> + 'static) -> Self {
self.labellers.push(Box::new(move |set: SystemSet| set.before(label.as_system_label())));
self.labellers.push(Box::new(move |set: SystemSet| set.before(label)));
self
}

pub fn after<Marker>(mut self, label: impl AsSystemLabel<Marker> + 'static) -> Self {
self.labellers.push(Box::new(move |set: SystemSet| set.after(label.as_system_label())));
self.labellers.push(Box::new(move |set: SystemSet| set.after(label)));
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fixedtimestep.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;
use bevy_time::Time;

use bevy_core::Time;
use bevy_ecs::prelude::*;

/// This type will be available as a resource, while a fixed timestep stage
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub mod prelude {
pub use crate::condition::{ConditionHelpers, IntoConditionalSystem, ConditionSet, AddConditionalToSet};
#[cfg(feature = "fixedtimestep")]
pub use crate::fixedtimestep::{FixedTimestepInfo, FixedTimestepStage};
#[cfg(feature = "states")]
pub use crate::state::{CurrentState, NextState, StateTransitionStage};
#[cfg(feature = "app")]
pub use crate::state::app::AppLooplessStateExt;
pub use crate::state::schedule::ScheduleLooplessStateExt;
#[cfg(feature = "states")]
pub use crate::state::{CurrentState, NextState, StateTransitionStage};
}
140 changes: 133 additions & 7 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,16 @@ pub mod app {

use super::StateTransitionStage;

#[derive(Debug, Clone, PartialEq, Eq, Hash, StageLabel)]
#[derive(Debug, Clone)]
pub struct StateTransitionStageLabel(TypeId, String);

impl StageLabel for StateTransitionStageLabel {
fn as_str(&self) -> &'static str {
let s = format!("{:?}{}", self.0, self.1);
Box::leak(s.into_boxed_str())
}
}

impl StateTransitionStageLabel {
pub fn from_type<T: StateData>() -> Self {
use std::any::type_name;
Expand Down Expand Up @@ -303,37 +310,156 @@ pub mod app {
}
fn add_enter_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_enter_system(state, system);
self
}
fn add_exit_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_exit_system(state, system);
self
}
fn add_enter_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_enter_system_set(state, system_set);
self
}
fn add_exit_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_exit_system_set(state, system_set);
self
}
fn set_enter_stage<T: StateData>(&mut self, state: T, enter_stage: impl Stage) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.set_enter_stage(state, enter_stage);
self
}
fn set_exit_stage<T: StateData>(&mut self, state: T, exit_stage: impl Stage) -> &mut App {
let stage = self.schedule.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transiton Stage not found (assuming auto-added label)");
.expect("State Transition Stage not found (assuming auto-added label)");
stage.set_exit_stage(state, exit_stage);
self
}
}
}

pub mod schedule {
use std::any::TypeId;

use bevy_ecs::schedule::{StageLabel, Stage, StateData, IntoSystemDescriptor, SystemSet, Schedule};

use super::StateTransitionStage;

#[derive(Debug, Clone)]
pub struct StateTransitionStageLabel(TypeId, String);

impl StageLabel for StateTransitionStageLabel {
fn as_str(&self) -> &'static str {
let s = format!("{:?}{}", self.0, self.1);
Box::leak(s.into_boxed_str())
}
}

impl StateTransitionStageLabel {
pub fn from_type<T: StateData>() -> Self {
use std::any::type_name;
StateTransitionStageLabel(TypeId::of::<T>(), type_name::<T>().to_owned())
}
}

pub trait ScheduleLooplessStateExt {
/// Add a `StateTransitionStage` after the specified stage
fn add_loopless_state_after_stage<T: StateData>(&mut self, stage: impl StageLabel, init: T) -> &mut Schedule;
/// Add a `StateTransitionStage` before the specified stage
fn add_loopless_state_before_stage<T: StateData>(&mut self, stage: impl StageLabel, init: T) -> &mut Schedule;
/// Add an enter system for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
fn add_enter_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut Schedule;
/// Add an exit system for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
fn add_exit_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut Schedule;
/// Add an enter system set for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
fn add_enter_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut Schedule;
/// Add an exit system set for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
fn add_exit_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut Schedule;
/// Add a custom stage to execute for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
///
/// Cannot be used together with `add_enter_system`.
fn set_enter_stage<T: StateData>(&mut self, state: T, system: impl Stage) -> &mut Schedule;
/// Add a custom stage to execute for the given state
///
/// Requires the stage to be labeled with a `StateTransitionStageLabel`
/// (as done by the `add_loopless_state*` methods).
///
/// Cannot be used together with `add_enter_system`.
fn set_exit_stage<T: StateData>(&mut self, state: T, system: impl Stage) -> &mut Schedule;
}

impl ScheduleLooplessStateExt for Schedule {
fn add_loopless_state_after_stage<T: StateData>(&mut self, stage: impl StageLabel, init: T) -> &mut Schedule {
self.add_stage_after(
stage,
StateTransitionStageLabel::from_type::<T>(),
StateTransitionStage::new(init)
)
}
fn add_loopless_state_before_stage<T: StateData>(&mut self, stage: impl StageLabel, init: T) -> &mut Schedule {
self.add_stage_before(
stage,
StateTransitionStageLabel::from_type::<T>(),
StateTransitionStage::new(init)
)
}
fn add_enter_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_enter_system(state, system);
self
}
fn add_exit_system<T: StateData, Params>(&mut self, state: T, system: impl IntoSystemDescriptor<Params>) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_exit_system(state, system);
self
}
fn add_enter_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_enter_system_set(state, system_set);
self
}
fn add_exit_system_set<T: StateData>(&mut self, state: T, system_set: SystemSet) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.add_exit_system_set(state, system_set);
self
}
fn set_enter_stage<T: StateData>(&mut self, state: T, enter_stage: impl Stage) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.set_enter_stage(state, enter_stage);
self
}
fn set_exit_stage<T: StateData>(&mut self, state: T, exit_stage: impl Stage) -> &mut Schedule {
let stage = self.get_stage_mut::<StateTransitionStage<T>>(&StateTransitionStageLabel::from_type::<T>())
.expect("State Transition Stage not found (assuming auto-added label)");
stage.set_exit_stage(state, exit_stage);
self
}
Expand Down