use std::{any::Any, hash::Hash};
use super::{
component::Components,
event::EventQueues,
system::param::SystemParameter,
world::UnsafeWorldCell,
};
pub struct ComponentCommands<'w> {
components: &'w mut Components,
}
impl<'w> ComponentCommands<'w> {
pub const fn from_components(components: &'w mut Components) -> Self {
Self { components }
}
pub fn spawn<T>(&mut self, component: T) -> &mut Self
where
T: 'static,
{
self.components.spawn(component);
self
}
pub fn spawn_indexed<TKey, TValue>(&mut self, key: TKey, component: TValue) -> &mut Self
where
TKey: Eq + Hash + 'static,
TValue: 'static,
{
self.components.spawn_indexed(key, component);
self
}
}
impl<'w> SystemParameter for ComponentCommands<'w> {
fn fetch(cell: &UnsafeWorldCell) -> Self {
let world = cell.get_mut();
let components = world.components_mut();
Self { components }
}
}
pub struct EventCommands<'w> {
events: &'w mut EventQueues,
}
impl<'w> EventCommands<'w> {
pub const fn from_events(events: &'w mut EventQueues) -> Self {
Self { events }
}
pub fn queue<E>(&mut self, event: E) -> &mut Self
where
E: Any + 'static,
{
self.events.queue(event);
self
}
}
impl<'w> SystemParameter for EventCommands<'w> {
fn fetch(cell: &UnsafeWorldCell) -> Self {
let world = cell.get_mut();
let events = world.events_mut();
Self::from_events(events)
}
}