Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,51 @@ All mutability can be _piped out_ of your code.
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

fn main() {
fn main() -> AppExit {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(
Update,
pure(rainbow_clear_color) // pure() is optional, just forces the system to be read-only
.pipe(affect),
)
.run();
.run()
}

/// This system defines the clear color as a pure function of time.
fn rainbow_clear_color(time: Res<Time>) -> impl Effect + use<> {
fn rainbow_clear_color(time: Res<Time>) -> ResSet<ClearColor> {
let color = Color::hsv(time.elapsed_secs() * 20.0, 0.7, 0.7);
res_set(ClearColor(color))
}

#[cfg(test)]
mod tests {

use bevy::camera::ClearColor;
use bevy::color::Color;
use bevy::ecs::system::{Res, SystemState};
use bevy::ecs::world::World;
use bevy::time::{Real, Time};

use crate::rainbow_clear_color;

#[test]
fn test_rainbow_clear() {
// Create a Res<Time> for our system
let mut world = World::new();
world.insert_resource::<Time>(Time::default());

let mut state: SystemState<(Res<Time>,)> = SystemState::new(&mut world);
let (time,) = state.get(&mut world);

// Now that we have a Res<Time>, we can run our system and get an output
let output = rainbow_clear_color(time);

// Then all we have to do is check that output has the proper value
assert_eq!(output.value.0, Color::hsva(0.0, 0.7, 0.7, 1.0));
}
}

```

## Documentation
Expand Down
26 changes: 23 additions & 3 deletions examples/rainbow-clear-color.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

fn main() {
fn main() -> AppExit {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(
Update,
pure(rainbow_clear_color) // pure() is optional, just forces the system to be read-only
.pipe(affect),
)
.run();
.run()
}

/// This system defines the clear color as a pure function of time.
fn rainbow_clear_color(time: Res<Time>) -> impl Effect + use<> {
fn rainbow_clear_color(time: Res<Time>) -> ResSet<ClearColor> {
let color = Color::hsv(time.elapsed_secs() * 20.0, 0.7, 0.7);
res_set(ClearColor(color))
}

#[cfg(test)]
mod tests {
use bevy::ecs::system::RunSystemOnce;

use super::*;

#[test]
fn test_rainbow_clear() {
// Create a World with a Resource for our Time
let mut world = World::new();
world.insert_resource::<Time>(Time::default());

// Now that we have a Res<Time>, we can run our system and get an output
let output = world.run_system_once(rainbow_clear_color).unwrap();

// Then all we have to do is check that output has the proper value
assert_eq!(output.value.0, Color::hsva(0.0, 0.7, 0.7, 1.0));
}
}