Skip to content

Commit

Permalink
[Release]: v.0.5.0 (#44)
Browse files Browse the repository at this point in the history
## Fix

- fixed a bug where the execution run condition `switch_just_*` was not  working correctly.

## Breaking Change

- rename `switch_turned_on` to ``switch_is_on`
- rename `switch_turned_off` to `switch_is_on`
- delete `Switch::just_turned_on`
- delete `Switch::just_turned_off`
  • Loading branch information
not-elm committed May 6, 2024
1 parent dc1eb65 commit 2b06955
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 87 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ name = "effect"
path = "examples/effect.rs"
required-features = ["tokio"]

[[example]]
name = "switch_just_change"
path = "examples/bug_check/switch_just_change.rs"

[dependencies]
bevy = { version = "0.13.2", default-features = false, features = ["multi-threaded"] }
flurx = { version = "0.1.5" }
Expand Down
1 change: 1 addition & 0 deletions examples/bug_check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module checks cases that cannot be tested by unit tests.
46 changes: 46 additions & 0 deletions examples/bug_check/switch_just_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! Testing that the system runs properly when the switch is switched

use bevy::prelude::*;

use bevy_flurx::prelude::*;

struct S;

fn main() {
App::new()
.add_plugins((
DefaultPlugins,
FlurxPlugin
))
.add_systems(Startup, setup)
.add_systems(Update, (
console_switch_just_on::<1>.run_if(switch_just_turned_on::<S>),
console_switch_just_on::<2>.run_if(switch_just_turned_on::<S>),
console_switch_just_off::<1>.run_if(switch_just_turned_off::<S>),
console_switch_just_off::<2>.run_if(switch_just_turned_off::<S>)
))
.run();
}

fn setup(
mut commands: Commands
) {
commands.spawn(Reactor::schedule(|task| async move {
loop {
task.will(Update, wait::input::just_pressed().with(KeyCode::KeyT)
.then(once::switch::on::<S>())
.then(delay::frames().with(1))
.then(wait::input::just_pressed().with(KeyCode::KeyT))
.then(once::switch::off::<S>()),
).await;
}
}));
}

fn console_switch_just_on<const NUM: u8>() {
println!("[{NUM}] switch just turned on!");
}

fn console_switch_just_off<const NUM: u8>() {
println!("[{NUM}] switch just turned off!");
}
8 changes: 4 additions & 4 deletions examples/cut_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ fn main() {
setup,
))
.add_systems(Update, (
cut_in::<CutInBackground, 200>.run_if(switch_turned_on::<CutInBackground>),
cut_in_ferris.run_if(switch_turned_on::<HandsomeFerris>),
move_left_down::<25>.run_if(switch_turned_on::<MoveSlowly>),
move_left_down::<10000>.run_if(switch_turned_on::<MoveFast>)
cut_in::<CutInBackground, 200>.run_if(switch_is_on::<CutInBackground>),
cut_in_ferris.run_if(switch_is_on::<HandsomeFerris>),
move_left_down::<25>.run_if(switch_is_on::<MoveSlowly>),
move_left_down::<10000>.run_if(switch_is_on::<MoveFast>)
))
.run();
}
Expand Down
143 changes: 62 additions & 81 deletions src/action/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
//!
//! This is to solve the problem that systems created from `Reactors`
//! cannot run except on the main thread.
//!
//!
//! Resource
//!
//!
//! - [`Switch`]
//!
//!
//! run conditions
//!
//! - [`switch_turned_on`]
//! - [`switch_turned_off`]
//!
//! - [`switch_is_on`]
//! - [`switch_is_off`]
//! - [`switch_just_turned_on`]
//! - [`switch_just_turned_off`]
//!
//!
//! actions
//!
//!
//! - [`once::switch::on`](crate::prelude::once::switch::on)
//! - [`once::switch::off`](crate::prelude::once::switch::off)
//! - [`wait::switch::on`](crate::prelude::wait::switch::on)
Expand All @@ -24,42 +24,67 @@

use std::marker::PhantomData;

use bevy::app::PostUpdate;
use bevy::ecs::schedule::ScheduleLabel;
use bevy::prelude::{FromWorld, Mut, Res, ResMut, Resource, Schedules, World};
use bevy::prelude::{Local, Mut, Res, Resource, World};

use crate::runner::initialize_schedule;

/// A Condition-satisfying system that returns true if the switch has been turned on.
/// A Condition-satisfying system that returns true if the switch has been turned on.
#[inline]
pub fn switch_turned_on<M>(switch: Option<Res<Switch<M>>>) -> bool
pub fn switch_is_on<M>(switch: Option<Res<Switch<M>>>) -> bool
where M: Send + Sync + 'static
{
switch.is_some_and(|s| s.turned_on())
switch.is_some_and(|s| s.is_on())
}

/// A Condition-satisfying system that returns true if the switch has just been turned on.

/// A Condition-satisfying system that returns true if the switch has been turned off.
#[inline]
pub fn switch_just_turned_on<M>(switch: Option<Res<Switch<M>>>) -> bool
pub fn switch_is_off<M>(switch: Option<Res<Switch<M>>>) -> bool
where M: Send + Sync + 'static
{
switch.is_some_and(|s| s.just_turned_on())
switch.is_some_and(|s| {
s.is_off()
})
}

/// A Condition-satisfying system that returns true if the switch has been turned off.
/// A Condition-satisfying system that returns true if the switch has just been turned on.
#[inline]
pub fn switch_turned_off<M>(switch: Option<Res<Switch<M>>>) -> bool
pub fn switch_just_turned_on<M>(
switch: Option<Res<Switch<M>>>,
mut is_on: Local<bool>,
) -> bool
where M: Send + Sync + 'static
{
switch.is_some_and(|s| s.turned_off())
if switch.is_some_and(|s| s.is_on()) {
if *is_on {
false
} else {
*is_on = true;
true
}
} else {
*is_on = false;
false
}
}

/// A Condition-satisfying system that returns true if the switch has just been turned off.
#[inline]
pub fn switch_just_turned_off<M>(switch: Option<Res<Switch<M>>>) -> bool
pub fn switch_just_turned_off<M>(
switch: Option<Res<Switch<M>>>,
mut is_off: Local<bool>,
) -> bool
where M: Send + Sync + 'static
{
switch.is_some_and(|s| s.just_turned_off())
if switch.is_some_and(|s| s.is_off()) {
if *is_off {
false
} else {
*is_off = true;
true
}
} else {
*is_off = false;
false
}
}

/// A switch is a structure that represents two states: `on` and `off`.
Expand All @@ -81,7 +106,7 @@ pub fn switch_just_turned_off<M>(switch: Option<Res<Switch<M>>>) -> bool
/// //..
///
/// switch.off();
/// }).run_if(switch_turned_on::<HeavyTask>))
/// }).run_if(switch_is_on::<HeavyTask>))
/// .add_systems(Update, |mut commands: Commands|{
/// commands.spawn(Reactor::schedule(|task| async move{
/// task.will(Update, once::switch::on::<HeavyTask>()).await;
Expand All @@ -93,6 +118,7 @@ pub fn switch_just_turned_off<M>(switch: Option<Res<Switch<M>>>) -> bool
pub struct Switch<M> {
just_change: bool,
turned_on: bool,
checked: bool,
_m: PhantomData<M>,
}

Expand All @@ -109,31 +135,20 @@ impl<M> Switch<M>
Self {
just_change: true,
turned_on: turn_on,
checked: false,
_m: PhantomData,
}
}

/// Returns true if the switch has just been turned on.
#[inline(always)]
pub const fn just_turned_on(&self) -> bool {
self.just_change && self.turned_on
}

/// Returns true if the switch has just been turned off.
#[inline(always)]
pub const fn just_turned_off(&self) -> bool {
self.just_change && self.turned_off()
}

/// Returns true if switch is on.
#[inline(always)]
pub const fn turned_on(&self) -> bool {
pub const fn is_on(&self) -> bool {
self.turned_on
}

/// Returns true if switch is off.
#[inline(always)]
pub const fn turned_off(&self) -> bool {
pub const fn is_off(&self) -> bool {
!self.turned_on
}

Expand All @@ -149,7 +164,7 @@ impl<M> Switch<M>
/// Turn on the switch.
#[inline(always)]
pub fn on(&mut self) {
if self.turned_off() {
if self.is_off() {
self.just_change = true;
self.turned_on = true;
}
Expand All @@ -165,43 +180,23 @@ impl<M> Switch<M>
}

pub(crate) fn setup(world: &mut World, turn_on: bool) -> Mut<Switch<M>> {
if world.get_resource::<Switch<M>>().is_some() {
let mut s = world.resource_mut::<Switch<M>>();
s.set(turn_on);
s
} else {
let mut s = Self::from_world(world);
s.set(turn_on);
world.insert_resource(s);
world.resource_mut::<Switch<M>>()
}
world.insert_resource(Self::new(turn_on));
world.resource_mut::<Switch<M>>()
}
}

impl<M> FromWorld for Switch<M>
impl<M> Default for Switch<M>
where M: Send + Sync + 'static
{
fn from_world(world: &mut World) -> Self {
world.resource_scope(|_, mut schedules: Mut<Schedules>| {
let schedule = initialize_schedule(&mut schedules, PostUpdate.intern());
schedule.add_systems(|mut switch: ResMut<Switch<M>>| {
switch.just_change = false;
});
});

fn default() -> Self {
Self::new(false)
}
}


#[cfg(test)]
mod tests {
use bevy::prelude::{Res, ResMut, Update};
use bevy_test_helper::resource::bool::{Bool, BoolExtension};
use bevy_test_helper::resource::DirectResourceControl;

use crate::prelude::Switch;
use crate::tests::test_app;

struct T;

Expand All @@ -213,31 +208,17 @@ mod tests {
s.just_change = false;
s.off();
assert!(s.just_change);
assert!(s.turned_off());
assert!(s.is_off());
}

#[test]
fn on() {
let mut s = Switch::<T>::new(false);
assert!(s.just_change);
assert!(s.turned_off());
assert!(s.is_off());
s.just_change = false;
s.on();
assert!(s.just_change);
assert!(s.turned_on());
}

#[test]
fn cleanup() {
let mut app = test_app();
app.init_resource::<Switch<T>>();
app.add_systems(Update, |mut b: ResMut<Bool>, s: Res<Switch<T>>| {
if s.just_turned_off() {
**b = true;
}
});
app.update();
assert!(app.is_bool_true());
assert!(!app.resource::<Switch<T>>().just_turned_off());
assert!(s.is_on());
}
}
4 changes: 2 additions & 2 deletions src/action/wait/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn on<M>() -> ActionSeed
where M: Send + Sync + 'static
{
wait::until(|switch: Option<Res<Switch<M>>>| {
switch.is_some_and(|s| s.turned_on())
switch.is_some_and(|s| s.is_on())
})
}

Expand All @@ -55,6 +55,6 @@ pub fn off<M>() -> ActionSeed
where M: Send + Sync + 'static
{
wait::until(|switch: Option<Res<Switch<M>>>| {
switch.is_some_and(|s| s.turned_off())
switch.is_some_and(|s| s.is_off())
})
}

0 comments on commit 2b06955

Please sign in to comment.