amico_core/world/
delegate.rs

1use crate::ecs;
2
3/// A wrapper around `&mut World` to restrict the caller to
4/// access only the `add_handler` method in `World`.
5#[derive(Debug)]
6pub struct HandlerRegistry<'world> {
7    pub(crate) world: &'world mut ecs::World,
8}
9
10impl HandlerRegistry<'_> {
11    /// Register a handler to `World`.
12    pub fn register<M>(&mut self, handler: impl ecs::IntoHandler<M>) {
13        self.world.add_handler(handler);
14    }
15}
16
17/// Sends ECS events (Actions for the Agent) to the ECS `World`.
18///
19/// A wrapper around `&mut World` to restrict the caller to
20/// access only the `send` method in `World`.
21pub struct ActionSender<'world> {
22    pub(crate) world: &'world mut ecs::World,
23}
24
25impl ActionSender<'_> {
26    /// Send an ECS event to the ECS `World`.
27    pub fn send<E: ecs::GlobalEvent>(&mut self, event: E) {
28        self.world.send(event);
29    }
30
31    /// Send an ECS event to a specific entity in the ECS `World`.
32    pub fn send_to<E: ecs::TargetedEvent>(&mut self, target: ecs::EntityId, event: E) {
33        self.world.send_to(target, event);
34    }
35}