move system function constructors to System
This commit is contained in:
parent
9a3700d8f1
commit
713c4a6056
@ -1,12 +1,10 @@
|
||||
use crate::{
|
||||
plugin::{load_plugin, AppPlugin},
|
||||
schedule_plan::{SchedulePlan, System},
|
||||
stage, App, AppExit, Events,
|
||||
schedule_plan::SchedulePlan,
|
||||
stage, App, AppExit, Events, System,
|
||||
};
|
||||
|
||||
use legion::prelude::{Resources, Universe, World, IntoQuery, ResourceSet, into_system, into_resource_system};
|
||||
use legion::query::{View, DefaultFilter};
|
||||
use legion::filter::EntityFilter;
|
||||
use legion::prelude::{Resources, Universe, World};
|
||||
|
||||
static APP_MISSING_MESSAGE: &str = "This AppBuilder no longer has an App. Check to see if you already called run(). A call to app_builder.run() consumes the AppBuilder's App.";
|
||||
|
||||
@ -239,46 +237,4 @@ impl AppBuilder {
|
||||
plugin.build(self);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_system_fn<'a, Q, F, R, X>(&mut self, name: &'static str, system: F) -> &mut Self
|
||||
where
|
||||
Q: IntoQuery + DefaultFilter<Filter = R>,
|
||||
<Q as View<'a>>::Iter: Iterator<Item = Q> + 'a,
|
||||
F: FnMut(&mut X, Q) + Send + Sync + 'static,
|
||||
R: EntityFilter + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
self.add_system_fn_to_stage(stage::UPDATE, name, system)
|
||||
}
|
||||
|
||||
pub fn add_system_fn_to_stage<'a, Q, F, R, X>(&mut self, stage_name: &str, name: &'static str, system: F) -> &mut Self
|
||||
where
|
||||
Q: IntoQuery + DefaultFilter<Filter = R>,
|
||||
<Q as View<'a>>::Iter: Iterator<Item = Q> + 'a,
|
||||
F: FnMut(&mut X, Q) + Send + Sync + 'static,
|
||||
R: EntityFilter + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
let system = into_system(name, system);
|
||||
self.add_system_to_stage(stage_name, system);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_resource_system_fn<'a, F, X>(&mut self, name: &'static str, system: F) -> &mut Self
|
||||
where
|
||||
F: FnMut(&mut X) + Send + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
self.add_resource_system_fn_to_stage(stage::UPDATE, name, system)
|
||||
}
|
||||
|
||||
pub fn add_resource_system_fn_to_stage<'a, F, X>(&mut self, stage_name: &str, name: &'static str, system: F) -> &mut Self
|
||||
where
|
||||
F: FnMut(&mut X) + Send + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
let system = into_resource_system(name, system);
|
||||
self.add_system_to_stage(stage_name, system);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ mod app_builder;
|
||||
mod entity_archetype;
|
||||
mod event;
|
||||
mod plugin;
|
||||
mod system;
|
||||
pub mod schedule_plan;
|
||||
pub mod schedule_runner;
|
||||
pub mod stage;
|
||||
@ -12,3 +13,4 @@ pub use app_builder::*;
|
||||
pub use entity_archetype::*;
|
||||
pub use event::*;
|
||||
pub use plugin::*;
|
||||
pub use system::*;
|
||||
|
||||
@ -1,32 +1,6 @@
|
||||
use legion::prelude::*;
|
||||
use std::{cmp::Ordering, collections::HashMap};
|
||||
|
||||
pub enum System {
|
||||
Schedulable(Box<dyn Schedulable>),
|
||||
ThreadLocal(Box<dyn Runnable>),
|
||||
ThreadLocalFn(Box<dyn FnMut(&mut World, &mut Resources)>),
|
||||
}
|
||||
|
||||
impl From<Box<dyn Schedulable>> for System {
|
||||
fn from(system: Box<dyn Schedulable>) -> Self {
|
||||
System::Schedulable(system)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn Runnable>> for System {
|
||||
fn from(system: Box<dyn Runnable>) -> Self {
|
||||
System::ThreadLocal(system)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for System
|
||||
where
|
||||
T: FnMut(&mut World, &mut Resources) + 'static,
|
||||
{
|
||||
fn from(system: T) -> Self {
|
||||
System::ThreadLocalFn(Box::new(system))
|
||||
}
|
||||
}
|
||||
use crate::System;
|
||||
use legion::prelude::Schedule;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SchedulePlan {
|
||||
|
||||
55
crates/bevy_app/src/system.rs
Normal file
55
crates/bevy_app/src/system.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use legion::{
|
||||
filter::EntityFilter,
|
||||
prelude::{
|
||||
into_resource_system, into_system, IntoQuery, ResourceSet, Resources, Runnable,
|
||||
Schedulable, World,
|
||||
},
|
||||
query::{DefaultFilter, View},
|
||||
};
|
||||
pub enum System {
|
||||
Schedulable(Box<dyn Schedulable>),
|
||||
ThreadLocal(Box<dyn Runnable>),
|
||||
ThreadLocalFn(Box<dyn FnMut(&mut World, &mut Resources)>),
|
||||
}
|
||||
|
||||
impl From<Box<dyn Schedulable>> for System {
|
||||
fn from(system: Box<dyn Schedulable>) -> Self {
|
||||
System::Schedulable(system)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn Runnable>> for System {
|
||||
fn from(system: Box<dyn Runnable>) -> Self {
|
||||
System::ThreadLocal(system)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for System
|
||||
where
|
||||
T: FnMut(&mut World, &mut Resources) + 'static,
|
||||
{
|
||||
fn from(system: T) -> Self {
|
||||
System::ThreadLocalFn(Box::new(system))
|
||||
}
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub fn resource_for<'a, Q, F, R, X>(name: &'static str, system: F) -> Self
|
||||
where
|
||||
Q: IntoQuery + DefaultFilter<Filter = R>,
|
||||
<Q as View<'a>>::Iter: Iterator<Item = Q> + 'a,
|
||||
F: FnMut(&mut X, Q) + Send + Sync + 'static,
|
||||
R: EntityFilter + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
into_system(name, system).into()
|
||||
}
|
||||
|
||||
pub fn resource<'a, F, X>(name: &'static str, system: F) -> Self
|
||||
where
|
||||
F: FnMut(&mut X) + Send + Sync + 'static,
|
||||
X: ResourceSet<PreparedResources = X> + 'static,
|
||||
{
|
||||
into_resource_system(name, system).into()
|
||||
}
|
||||
}
|
||||
@ -13,7 +13,6 @@ use legion_core::{
|
||||
use std::marker::PhantomData;
|
||||
use bit_set::BitSet;
|
||||
|
||||
|
||||
pub fn into_system<'a, Q, F, R, X>(name: &'static str, mut system: F) -> Box<dyn Schedulable>
|
||||
where
|
||||
Q: IntoQuery + DefaultFilter<Filter = R>,
|
||||
|
||||
@ -6,8 +6,8 @@ fn main() {
|
||||
.add_event::<MyEvent>()
|
||||
.add_resource(EventTriggerState::default())
|
||||
.add_resource_init::<EventListenerState>()
|
||||
.add_resource_system_fn("event_trigger", event_trigger_system)
|
||||
.add_resource_system_fn("event_listener", event_listener_system)
|
||||
.add_system(System::resource("event_trigger", event_trigger_system))
|
||||
.add_system(System::resource("event_listener", event_listener_system))
|
||||
.run();
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ fn main() {
|
||||
..Default::default()
|
||||
})
|
||||
.add_startup_system(setup)
|
||||
.add_system_fn("move", move_system)
|
||||
.add_system(System::resource_for("move", move_system))
|
||||
.run();
|
||||
}
|
||||
|
||||
|
||||
34
examples/systems.rs
Normal file
34
examples/systems.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use bevy::{
|
||||
input::mouse::{MouseButtonInput, MouseMotion},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.add_system_init(mouse_input_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);
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@ -38,7 +38,7 @@ pub use crate::ui::{entity::*, Anchors, Margins, Node};
|
||||
pub use crate::window::{Window, WindowDescriptor, WindowPlugin, Windows};
|
||||
pub use crate::{
|
||||
app::{
|
||||
stage, App, AppBuilder, AppPlugin, EntityArchetype, EventReader, Events, GetEventReader,
|
||||
stage, App, AppBuilder, AppPlugin, EntityArchetype, EventReader, Events, GetEventReader, System
|
||||
},
|
||||
math::{self, Mat3, Mat4, Quat, Vec2, Vec3, Vec4},
|
||||
AddDefaultPlugins,
|
||||
@ -54,7 +54,7 @@ pub use legion::{
|
||||
bit_set::BitSet,
|
||||
resource::{ResourceSet, Resources, PreparedRead as Resource, PreparedWrite as ResourceMut},
|
||||
schedule::{Executor, Runnable, Schedulable, Schedule},
|
||||
SubWorld, System, SystemBuilder,
|
||||
SubWorld, SystemBuilder,
|
||||
into_system, into_resource_system
|
||||
},
|
||||
world::{Universe, World},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user