use std::collections::HashMap;
use amethyst::core::bundle::{Error as BundleError, ErrorKind};
use amethyst::core::{ArcThreadPool, SystemBundle};
use amethyst::ecs::{Dispatcher, DispatcherBuilder, System};
use amethyst::prelude::*;
use amethyst::renderer::DisplayConfig;
use amethyst::DataInit;
use super::internal_helpers::*;
use super::CustomGameData;
pub struct CustomGameDataBuilder<'a, 'b, T = ()> {
core_dispatcher: DispatcherBuilder<'a, 'b>,
dispatchers: HashMap<String, DispatcherBuilder<'a, 'b>>,
custom: Option<T>,
}
impl<'a, 'b, T> CustomGameDataBuilder<'a, 'b, T> {
pub fn new() -> Self {
Self {
core_dispatcher: DispatcherBuilder::new(),
dispatchers: HashMap::new(),
custom: None,
}
}
pub fn dispatcher<U>(mut self, name: U) -> amethyst::Result<Self>
where
U: ToString,
{
if self
.dispatchers
.insert(name.to_string(), DispatcherBuilder::new())
.is_none()
{
Ok(self)
} else {
Err(amethyst_error(format!(
"A dispatcher with the given name has already been \
initialized: {}",
name.to_string()
)))
}
}
pub fn custom(mut self, custom: T) -> Self {
self.custom = Some(custom);
self
}
pub fn with_core_bundle<B>(mut self, bundle: B) -> amethyst::Result<Self>
where
B: SystemBundle<'a, 'b>,
{
bundle
.build(&mut self.core_dispatcher)
.map_err(|err| amethyst::Error::Core(err))?;
Ok(self)
}
pub fn with_bundle<U, B>(
mut self,
dispatcher_name: U,
bundle: B,
) -> amethyst::Result<Self>
where
U: ToString,
B: SystemBundle<'a, 'b>,
{
let dispatcher_name = dispatcher_name.to_string();
if let Some(dispatcher) =
self.dispatchers.get_mut(&dispatcher_name.to_string())
{
bundle
.build(dispatcher)
.map_err(|err| amethyst::Error::Core(err))?;
Ok(self)
} else {
Err(dispatcher_not_found(dispatcher_name))
}
}
pub fn with_core<S>(
mut self,
system: S,
name: &str,
dependencies: &[&str],
) -> amethyst::Result<Self>
where
for<'c> S: System<'c> + Send + 'a,
{
self.core_dispatcher.add(system, name, dependencies);
Ok(self)
}
pub fn with<U, S>(
mut self,
dispatcher_name: U,
system: S,
name: &str,
dependencies: &[&str],
) -> amethyst::Result<Self>
where
U: ToString,
for<'c> S: System<'c> + Send + 'a,
{
let dispatcher_name = dispatcher_name.to_string();
if let Some(dispatcher) = self.dispatchers.get_mut(&dispatcher_name) {
dispatcher.add(system, name, dependencies);
Ok(self)
} else {
Err(dispatcher_not_found(dispatcher_name))
}
}
}
impl<'a, 'b, T> DataInit<CustomGameData<'a, 'b, T>>
for CustomGameDataBuilder<'a, 'b, T>
{
fn build(self, world: &mut World) -> CustomGameData<'a, 'b, T> {
let pool = world.read_resource::<ArcThreadPool>().clone();
let mut core_dispatcher =
self.core_dispatcher.with_pool(pool.clone()).build();
core_dispatcher.setup(&mut world.res);
let dispatchers = self
.dispatchers
.into_iter()
.map(|(name, dispatcher_builder)| {
let mut dispatcher =
dispatcher_builder.with_pool(pool.clone()).build();
dispatcher.setup(&mut world.res);
(name, dispatcher)
})
.collect();
CustomGameData {
core_dispatcher,
dispatchers,
custom: self.custom,
}
}
}
impl<'a, 'b, T> Default for CustomGameDataBuilder<'a, 'b, T> {
fn default() -> Self {
Self::new()
}
}