Skip to content

Commit

Permalink
Implemented SystemDesc builder for library systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Aug 1, 2019
1 parent afc0a51 commit a4f1f92
Show file tree
Hide file tree
Showing 44 changed files with 631 additions and 318 deletions.
11 changes: 6 additions & 5 deletions amethyst_animation/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{
resources::AnimationSampling,
skinning::VertexSkinningSystem,
skinning::VertexSkinningSystemDesc,
systems::{
AnimationControlSystem, AnimationProcessor, SamplerInterpolationSystem, SamplerProcessor,
AnimationControlSystemDesc, AnimationProcessor, SamplerInterpolationSystem,
SamplerProcessor,
},
};
use amethyst_core::{
ecs::prelude::{Component, DispatcherBuilder, World},
SystemBundle,
SystemBundle, SystemDesc,
};
use amethyst_error::Error;
use std::{hash::Hash, marker};
Expand Down Expand Up @@ -41,7 +42,7 @@ impl<'a, 'b, 'c> SystemBundle<'a, 'b> for VertexSkinningBundle<'c> {
builder: &mut DispatcherBuilder<'a, 'b>,
) -> Result<(), Error> {
builder.add(
VertexSkinningSystem::new(world),
VertexSkinningSystemDesc::default().build(world),
"vertex_skinning_system",
self.dep,
);
Expand Down Expand Up @@ -155,7 +156,7 @@ where
) -> Result<(), Error> {
builder.add(AnimationProcessor::<T>::new(), "", &[]);
builder.add(
AnimationControlSystem::<I, T>::new(world),
AnimationControlSystemDesc::<I, T>::default().build(world),
self.animation_name,
self.dep,
);
Expand Down
25 changes: 18 additions & 7 deletions amethyst_animation/src/skinning/systems.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use amethyst_core::{
ecs::prelude::{
BitSet, ComponentEvent, Join, ReadStorage, ReaderId, System, World, WriteStorage,
BitSet, ComponentEvent, Join, ReadStorage, ReaderId, System, SystemData, World,
WriteStorage,
},
math::{convert, Matrix4},
Transform,
SystemDesc, Transform,
};
use amethyst_rendy::skinning::JointTransforms;

Expand All @@ -14,6 +15,20 @@ use thread_profiler::profile_scope;

use super::resources::*;

/// Builds a `VertexSkinningSystem`.
#[derive(Default, Debug)]
pub struct VertexSkinningSystemDesc;

impl<'a, 'b> SystemDesc<'a, 'b, VertexSkinningSystem> for VertexSkinningSystemDesc {
fn build(self, world: &mut World) -> VertexSkinningSystem {
<VertexSkinningSystem as System<'_>>::SystemData::setup(world);
let mut transform = WriteStorage::<Transform>::fetch(&world);
let updated_id = transform.register_reader();

VertexSkinningSystem::new(updated_id)
}
}

/// System for performing vertex skinning.
///
/// Needs to run after global transforms have been updated for the current frame.
Expand All @@ -28,11 +43,7 @@ pub struct VertexSkinningSystem {

impl VertexSkinningSystem {
/// Creates a new `VertexSkinningSystem`
pub fn new(mut world: &mut World) -> Self {
use amethyst_core::ecs::prelude::SystemData;
<Self as System<'_>>::SystemData::setup(&mut world);
let mut transform = WriteStorage::<Transform>::fetch(&world);
let updated_id = transform.register_reader();
pub fn new(updated_id: ReaderId<ComponentEvent>) -> Self {
Self {
updated: BitSet::default(),
updated_skins: BitSet::default(),
Expand Down
33 changes: 29 additions & 4 deletions amethyst_animation/src/systems/control.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::{hash::Hash, marker, time::Duration};
use std::{
hash::Hash,
marker::{self, PhantomData},
time::Duration,
};

use derivative::Derivative;
use fnv::FnvHashMap;
use log::error;
use minterpolate::InterpolationPrimitive;
Expand All @@ -11,6 +16,7 @@ use amethyst_core::{
WriteStorage,
},
timing::secs_to_duration,
SystemDesc,
};

use crate::resources::{
Expand All @@ -22,6 +28,27 @@ use crate::resources::{
#[cfg(feature = "profiler")]
use thread_profiler::profile_scope;

/// Builds an `AnimationControlSystem`.
#[derive(Derivative, Debug)]
#[derivative(Default(bound = ""))]
pub struct AnimationControlSystemDesc<I, T> {
marker: PhantomData<(I, T)>,
}

impl<'a, 'b, I, T> SystemDesc<'a, 'b, AnimationControlSystem<I, T>>
for AnimationControlSystemDesc<I, T>
where
I: Copy + Eq + Hash + Send + Sync + 'static,
T: AnimationSampling + Component + Clone,
{
fn build(self, world: &mut World) -> AnimationControlSystem<I, T> {
<AnimationControlSystem<I, T> as System<'_>>::SystemData::setup(world);
ReadStorage::<AnimationSet<I, T>>::setup(world);

AnimationControlSystem::new()
}
}

/// System for setting up animations, should run before `SamplerInterpolationSystem`.
///
/// Will process all active `AnimationControl` + `AnimationHierarchy`, and do processing of the
Expand Down Expand Up @@ -51,9 +78,7 @@ where
T: AnimationSampling + Component + Clone,
{
/// Creates a new `AnimationControlSystem`
pub fn new(mut world: &mut World) -> Self {
<Self as System<'_>>::SystemData::setup(&mut world);
ReadStorage::<AnimationSet<I, T>>::setup(&mut world);
pub fn new() -> Self {
AnimationControlSystem {
m: marker::PhantomData,
next_id: 1,
Expand Down
5 changes: 4 additions & 1 deletion amethyst_animation/src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use amethyst_assets::Processor;

use crate::resources::{Animation, Sampler};

pub use self::{control::AnimationControlSystem, sampling::SamplerInterpolationSystem};
pub use self::{
control::{AnimationControlSystem, AnimationControlSystemDesc},
sampling::SamplerInterpolationSystem,
};

mod control;
mod sampling;
Expand Down
1 change: 1 addition & 0 deletions amethyst_assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ amethyst_core = { path = "../amethyst_core", version = "0.6.0" }
amethyst_error = { path = "../amethyst_error", version = "0.1.0" }
crossbeam-queue = "0.1.2"
derivative = "1.0"
derive-new = "0.5"
fnv = "1"
log = "0.4.6"
parking_lot = "0.6"
Expand Down
4 changes: 3 additions & 1 deletion amethyst_assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub use crate::{
formats::RonFormat,
helper::AssetLoaderSystemData,
loader::Loader,
prefab::{AssetPrefab, Prefab, PrefabData, PrefabLoader, PrefabLoaderSystem},
prefab::{
AssetPrefab, Prefab, PrefabData, PrefabLoader, PrefabLoaderSystem, PrefabLoaderSystemDesc,
},
progress::{Completion, Progress, ProgressCounter, Tracker},
reload::{HotReloadBundle, HotReloadStrategy, HotReloadSystem, Reload, SingleFile},
source::{Directory, Source},
Expand Down
6 changes: 3 additions & 3 deletions amethyst_assets/src/prefab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Asset, AssetStorage, Format, Handle, Loader, Progress, ProgressCounter, SerializableFormat,
};

pub use self::system::PrefabLoaderSystem;
pub use self::system::{PrefabLoaderSystem, PrefabLoaderSystemDesc};

mod impls;
mod system;
Expand Down Expand Up @@ -471,7 +471,7 @@ mod tests {

use amethyst_core::{
ecs::{Builder, RunNow, World, WorldExt},
Time, Transform,
SystemDesc, Time, Transform,
};

use crate::Loader;
Expand All @@ -487,7 +487,7 @@ mod tests {
world.insert(pool.clone());
world.insert(Loader::new(".", pool));
world.insert(Time::default());
let mut system = PrefabLoaderSystem::<MyPrefab>::new(&mut world);
let mut system = PrefabLoaderSystemDesc::<MyPrefab>::default().build(&mut world);
RunNow::setup(&mut system, &mut world);

let prefab = Prefab::new_main(Transform::default());
Expand Down
30 changes: 24 additions & 6 deletions amethyst_assets/src/prefab/system.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{collections::HashMap, marker::PhantomData, ops::Deref};

use derivative::Derivative;
use log::error;

use amethyst_core::{
ecs::{
storage::ComponentEvent, BitSet, Entities, Entity, Join, Read, ReadExpect, ReadStorage,
ReaderId, System, World, Write, WriteStorage,
ReaderId, System, SystemData, World, Write, WriteStorage,
},
ArcThreadPool, Parent, Time,
ArcThreadPool, Parent, SystemDesc, Time,
};
use amethyst_error::{format_err, Error, ResultExt};

Expand All @@ -18,6 +19,26 @@ use crate::{AssetStorage, Completion, Handle, HotReloadStrategy, ProcessingState

use super::{Prefab, PrefabData, PrefabTag};

/// Builds a `PrefabLoaderSystem`.
#[derive(Derivative, Debug)]
#[derivative(Default(bound = ""))]
pub struct PrefabLoaderSystemDesc<T> {
marker: PhantomData<T>,
}

impl<'a, 'b, T> SystemDesc<'a, 'b, PrefabLoaderSystem<T>> for PrefabLoaderSystemDesc<T>
where
T: PrefabData<'a> + Send + Sync + 'static,
{
fn build(self, world: &mut World) -> PrefabLoaderSystem<T> {
<PrefabLoaderSystem<T> as System<'_>>::SystemData::setup(world);

let insert_reader = WriteStorage::<Handle<Prefab<T>>>::fetch(&world).register_reader();

PrefabLoaderSystem::new(insert_reader)
}
}

/// System that load `Prefab`s for `PrefabData` `T`.
///
/// ### Type parameters:
Expand All @@ -37,10 +58,7 @@ where
T: PrefabData<'a> + Send + Sync + 'static,
{
/// Creates a new `PrefabLoaderSystem`.
pub fn new(mut world: &mut World) -> Self {
use amethyst_core::ecs::prelude::SystemData;
<Self as System<'_>>::SystemData::setup(&mut world);
let insert_reader = WriteStorage::<Handle<Prefab<T>>>::fetch(&world).register_reader();
pub fn new(insert_reader: ReaderId<ComponentEvent>) -> Self {
Self {
_m: PhantomData,
entities: Vec::default(),
Expand Down
38 changes: 24 additions & 14 deletions amethyst_assets/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
use std::{sync::Arc, time::Instant};

use derive_new::new;

use amethyst_core::{
ecs::prelude::{DispatcherBuilder, Read, System, World, Write},
SystemBundle, Time,
ecs::prelude::{DispatcherBuilder, Read, System, SystemData, World, Write},
SystemBundle, SystemDesc, Time,
};
use amethyst_error::Error;

Expand Down Expand Up @@ -34,7 +36,7 @@ impl<'a, 'b> SystemBundle<'a, 'b> for HotReloadBundle {
dispatcher: &mut DispatcherBuilder<'a, 'b>,
) -> Result<(), Error> {
dispatcher.add(
HotReloadSystem::new(world, self.strategy),
HotReloadSystemDesc::new(self.strategy).build(world),
"hot_reload",
&[],
);
Expand All @@ -56,7 +58,7 @@ impl<'a, 'b> SystemBundle<'a, 'b> for HotReloadBundle {
/// world.insert(HotReloadStrategy::every(2));
/// # }
/// ```
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct HotReloadStrategy {
inner: HotReloadStrategyInner,
}
Expand Down Expand Up @@ -123,7 +125,7 @@ impl Default for HotReloadStrategy {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
enum HotReloadStrategyInner {
Every {
interval: u8,
Expand All @@ -137,20 +139,28 @@ enum HotReloadStrategyInner {
Never,
}

/// System for updating `HotReloadStrategy`.
pub struct HotReloadSystem;
/// Builds a `HotReloadSystem`.
#[derive(Debug, new)]
pub struct HotReloadSystemDesc {
/// The `HotReloadStrategy`.
pub strategy: HotReloadStrategy,
}

impl<'a, 'b> SystemDesc<'a, 'b, HotReloadSystem> for HotReloadSystemDesc {
fn build(self, world: &mut World) -> HotReloadSystem {
<HotReloadSystem as System<'_>>::SystemData::setup(world);

impl HotReloadSystem {
/// Create a new reload system
pub fn new(mut world: &mut World, strategy: HotReloadStrategy) -> Self {
use amethyst_core::ecs::prelude::SystemData;
<Self as System<'_>>::SystemData::setup(&mut world);
world.insert(strategy);
world.insert(self.strategy);
world.fetch_mut::<Loader>().set_hot_reload(true);
Self

HotReloadSystem::new()
}
}

/// System for updating `HotReloadStrategy`.
#[derive(Debug, new)]
pub struct HotReloadSystem;

impl<'a> System<'a> for HotReloadSystem {
type SystemData = (Read<'a, Time>, Write<'a, HotReloadStrategy>);

Expand Down
14 changes: 12 additions & 2 deletions amethyst_assets/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use rayon::ThreadPool;
use amethyst_core::{
ecs::{
hibitset::BitSet,
prelude::{Component, Read, ReadExpect, System, VecStorage, Write},
prelude::{Component, Read, ReadExpect, System, SystemData, VecStorage, World, Write},
storage::UnprotectedStorage,
},
Time,
SystemDesc, Time,
};
use amethyst_error::{Error, ResultExt};

Expand Down Expand Up @@ -516,6 +516,16 @@ impl<A> Processor<A> {
}
}

impl<'a, 'b, A> SystemDesc<'a, 'b, Processor<A>> for Processor<A>
where
A: Asset + ProcessableAsset,
{
fn build(self, world: &mut World) -> Processor<A> {
<Processor<A> as System<'_>>::SystemData::setup(world);
self
}
}

impl<'a, A> System<'a> for Processor<A>
where
A: Asset + ProcessableAsset,
Expand Down
1 change: 1 addition & 0 deletions amethyst_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ amethyst_assets = { path = "../amethyst_assets", version = "0.7.0"}
amethyst_core = { path = "../amethyst_core", version = "0.6.0"}
amethyst_error = { path = "../amethyst_error", version = "0.1.0"}
cpal = "0.8"
derive-new = "0.5"
log = "0.4.6"
rodio = "0.9"
serde = { version = "1.0", features = ["derive"] }
Expand Down
9 changes: 7 additions & 2 deletions amethyst_audio/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use amethyst_assets::Processor;
use amethyst_core::{
bundle::SystemBundle,
ecs::prelude::{DispatcherBuilder, World},
SystemDesc,
};
use amethyst_error::Error;

use crate::{output::Output, source::*, systems::AudioSystem};
use crate::{output::Output, source::*, systems::AudioSystemDesc};

/// Audio bundle
///
Expand All @@ -25,7 +26,11 @@ impl<'a, 'b> SystemBundle<'a, 'b> for AudioBundle {
world: &mut World,
builder: &mut DispatcherBuilder<'a, 'b>,
) -> Result<(), Error> {
builder.add(AudioSystem::new(world, self.0), "audio_system", &[]);
builder.add(
AudioSystemDesc::new(self.0).build(world),
"audio_system",
&[],
);
builder.add(Processor::<Source>::new(), "source_processor", &[]);
Ok(())
}
Expand Down
Loading

0 comments on commit a4f1f92

Please sign in to comment.