adapt examples to use system functions and state pattern

This commit is contained in:
Carter Anderson 2020-04-30 13:52:11 -07:00
parent 98f9639050
commit 2447672c63
11 changed files with 94 additions and 93 deletions

View file

@ -4,7 +4,7 @@ pub mod system;
use bevy_app::{AppBuilder, AppPlugin};
use keyboard::KeyboardInput;
use mouse::{MouseButtonInput, MouseMotion};
use mouse::{MouseButtonInput, MouseMotionInput};
#[derive(Default)]
pub struct InputPlugin;
@ -13,6 +13,6 @@ impl AppPlugin for InputPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_event::<KeyboardInput>()
.add_event::<MouseButtonInput>()
.add_event::<MouseMotion>();
.add_event::<MouseMotionInput>();
}
}

View file

@ -15,6 +15,6 @@ pub enum MouseButton {
}
#[derive(Debug, Clone)]
pub struct MouseMotion {
pub struct MouseMotionInput {
pub delta: (f64, f64),
}

View file

@ -4,7 +4,7 @@ pub use winit_windows::*;
use bevy_input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion},
mouse::{MouseButtonInput, MouseMotionInput},
};
use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events, GetEventReader};
@ -114,8 +114,8 @@ pub fn winit_runner(mut app: App) {
event::Event::DeviceEvent { ref event, .. } => match event {
DeviceEvent::MouseMotion { delta } => {
let mut mouse_motion_events =
app.resources.get_mut::<Events<MouseMotion>>().unwrap();
mouse_motion_events.send(MouseMotion { delta: *delta });
app.resources.get_mut::<Events<MouseMotionInput>>().unwrap();
mouse_motion_events.send(MouseMotionInput { delta: *delta });
}
_ => {}
},

View file

@ -17,7 +17,7 @@ fn main() {
.add_plugin(ScheduleRunnerPlugin {
run_mode: RunMode::Once,
})
.add_system(hello_world.system())
.add_system(hello_world_system.system())
.run();
// this app loops forever at 60 fps
@ -27,10 +27,10 @@ fn main() {
wait: Some(Duration::from_secs_f64(1.0 / 60.0)),
},
})
.add_system(hello_world.system())
.add_system(hello_world_system.system())
.run();
}
pub fn hello_world() {
fn hello_world_system() {
println!("hello world");
}

View file

@ -3,10 +3,10 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_system(hello_world.system())
.add_system(hello_world_system.system())
.run();
}
pub fn hello_world() {
fn hello_world_system() {
println!("hello world");
}

View file

@ -6,53 +6,60 @@ use bevy::{
fn main() {
App::build()
.add_default_plugins()
.add_system_init(move_on_input_system)
.add_resource_init::<State>()
.add_system(collect_input_system.system())
.add_system(move_system.system())
.add_startup_system(setup)
.run();
}
#[derive(Resource)]
struct State {
event_reader: EventReader<KeyboardInput>,
moving_right: bool,
moving_left: bool,
}
/// adjusts move state based on keyboard input
fn collect_input_system(
mut state: ResourceMut<State>,
keyboard_input_events: Resource<Events<KeyboardInput>>,
) {
for event in state.event_reader.iter(&keyboard_input_events) {
match event {
KeyboardInput {
virtual_key_code: Some(VirtualKeyCode::Left),
state: element_state,
..
} => {
state.moving_left = element_state.is_pressed();
}
KeyboardInput {
virtual_key_code: Some(VirtualKeyCode::Right),
state: element_state,
..
} => {
state.moving_right = element_state.is_pressed();
}
_ => {}
}
}
}
/// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed
pub fn move_on_input_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
let mut moving_left = false;
let mut moving_right = false;
SystemBuilder::new("input_handler")
.read_resource::<Time>()
.read_resource::<Events<KeyboardInput>>()
.with_query(<(Write<Translation>, Read<Handle<Mesh>>)>::query())
.build(
move |_command_buffer, world, (time, keyboard_input_events), mesh_query| {
for event in keyboard_input_event_reader.iter(&keyboard_input_events) {
match event {
KeyboardInput {
virtual_key_code: Some(VirtualKeyCode::Left),
state,
..
} => {
moving_left = state.is_pressed();
}
KeyboardInput {
virtual_key_code: Some(VirtualKeyCode::Right),
state,
..
} => {
moving_right = state.is_pressed();
}
_ => {}
}
}
fn move_system(
state: Resource<State>,
time: Resource<Time>,
mut translation: RefMut<Translation>,
_: Ref<Handle<Mesh>>,
) {
if state.moving_left {
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
}
for (mut translation, _mesh) in mesh_query.iter_mut(world) {
if moving_left {
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
}
if moving_right {
translation.0 += math::vec3(-1.0, 0.0, 0.0) * time.delta_seconds;
}
}
},
)
if state.moving_right {
translation.0 += math::vec3(-1.0, 0.0, 0.0) * time.delta_seconds;
}
}
/// creates a simple scene

View file

@ -1,34 +1,36 @@
use bevy::{
input::mouse::{MouseButtonInput, MouseMotion},
input::mouse::{MouseButtonInput, MouseMotionInput},
prelude::*,
};
fn main() {
App::build()
.add_default_plugins()
.add_system_init(mouse_input_system)
.add_resource_init::<State>()
.add_system(mouse_input_system.system())
.run();
}
/// prints out mouse events as they come in
pub fn mouse_input_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut mouse_button_input_event_reader = resources.get_event_reader::<MouseButtonInput>();
let mut mouse_motion_event_reader = resources.get_event_reader::<MouseMotion>();
SystemBuilder::new("mouse_input")
.read_resource::<Events<MouseButtonInput>>()
.read_resource::<Events<MouseMotion>>()
.build(
move |_command_buffer,
_world,
(mouse_button_input_events, mouse_motion_events),
_queries| {
for event in mouse_button_input_event_reader.iter(&mouse_button_input_events) {
println!("{:?}", event);
}
for event in mouse_motion_event_reader.iter(&mouse_motion_events) {
println!("{:?}", event);
}
},
)
#[derive(Resource)]
struct State {
mouse_button_event_reader: EventReader<MouseButtonInput>,
mouse_motion_event_reader: EventReader<MouseMotionInput>,
}
/// prints out mouse events as they come in
fn mouse_input_system(
mut state: ResourceMut<State>,
mouse_button_input_events: Ref<Events<MouseButtonInput>>,
mouse_motion_events: Ref<Events<MouseMotionInput>>,
) {
for event in state
.mouse_button_event_reader
.iter(&mouse_button_input_events)
{
println!("{:?}", event);
}
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
println!("{:?}", event);
}
}

View file

@ -6,12 +6,12 @@ fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_system(rotator.system())
.add_system(rotator_system.system())
.run();
}
/// rotates the parent, which will result in the child also rotating
fn rotator(time: Resource<Time>, _rotator: RefMut<Rotator>, mut rotation: RefMut<Rotation>) {
fn rotator_system(time: Resource<Time>, _rotator: RefMut<Rotator>, mut rotation: RefMut<Rotation>) {
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
}

View file

@ -35,7 +35,7 @@ fn main() {
#[derive(Serialize, Deserialize, TypeUuid)]
#[uuid = "14dec17f-ae14-40a3-8e44-e487fc423287"]
pub struct Test {
struct Test {
pub x: f32,
pub y: f32,
}

View file

@ -3,14 +3,14 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(startup.system())
.add_startup_system(startup_system.system())
.run();
}
/// Set up a simple scene using a "startup system".
/// Startup systems are run exactly once when the app starts up.
/// They run right before "normal" systems run.
fn startup(
fn startup_system(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<AssetStorage<Mesh>>,
mut materials: ResourceMut<AssetStorage<StandardMaterial>>,

View file

@ -4,7 +4,7 @@ fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_system(build_move_system())
.add_system(move_system.system())
.add_plugin(DiagnosticsPlugin {
print_diagnostics: true,
..Default::default()
@ -12,18 +12,10 @@ fn main() {
.run();
}
fn build_move_system() -> Box<dyn Schedulable> {
SystemBuilder::new("move")
.read_resource::<Time>()
.with_query(<Write<Node>>::query())
.build(move |_, world, time, query| {
for (_i, mut node) in query.iter_mut(world).enumerate() {
if node.color.r > 0.2 {
node.position += Vec2::new(0.1 * time.delta_seconds, 0.0);
// println!("{}", node.position.x());
}
}
})
fn move_system(time: Resource<Time>, mut node: RefMut<Node>) {
if node.color.r > 0.2 {
node.position += Vec2::new(0.1 * time.delta_seconds, 0.0);
}
}
fn setup(world: &mut World, _resources: &mut Resources) {