1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Resource locator traits.

use super::{Component, Entity};
use core::{
    any::Any,
    ops::{Deref, DerefMut},
};

/// Resource marker trait.
pub trait Resource: Any {}

impl<T: Any> Resource for T {}

/// Locator of a resource type.
pub trait ResourceLocator<'a, R: Resource> {
    /// Resource reference type.
    type Ref: Deref<Target = R> + 'a;

    /// Mutable resource reference type.
    type RefMut: DerefMut<Target = R> + 'a;

    /// Gets resource of type `R`.
    fn get(&'a self) -> Self::Ref;

    /// Gets resource of type `R` mutably.
    fn get_mut(&'a self) -> Self::RefMut;
}

/// Registry of resources.
pub trait Resources {
    /// Registers a resource type.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Resources};
    /// let mut registry = Registry::default();
    /// registry.register_resource(1u32);
    /// ```
    fn register_resource<R: Resource>(&mut self, value: R);

    /// Returns if a resource type is registered.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Resources};
    /// let mut registry = Registry::default();
    /// registry.register_resource(1u32);
    /// assert!(registry.has_resource::<u32>());
    /// ```
    fn has_resource<R: Resource>(&self) -> bool;

    /// Gets a resource.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Resources};
    /// let mut registry = Registry::default();
    /// registry.register_resource(1u32);
    /// assert_eq!(*registry.resource::<u32>(), 1u32);
    /// ```
    #[inline]
    fn resource<'a, R: Resource>(&'a self) -> <Self as ResourceLocator<'a, R>>::Ref
    where
        Self: ResourceLocator<'a, R>,
    {
        ResourceLocator::<R>::get(self)
    }

    /// Gets a resource mutably.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Resources};
    /// let mut registry = Registry::default();
    /// registry.register_resource(1u32);
    /// *registry.resource_mut::<u32>() = 2u32;
    /// assert_eq!(*registry.resource::<u32>(), 2u32);
    /// ```
    #[inline]
    fn resource_mut<'a, R: Resource>(&'a self) -> <Self as ResourceLocator<'a, R>>::RefMut
    where
        Self: ResourceLocator<'a, R>,
    {
        ResourceLocator::<R>::get_mut(self)
    }
}

/// Registry of entities.
pub trait Entities: Resources {
    /// Registers an entity type.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Entities, Entity, storage::ArenaStorage};
    /// struct Pos(u32, u32);
    /// impl Entity for Pos { type Storage = ArenaStorage<Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<Pos>();
    /// ```
    #[inline]
    fn register_entity<E: Entity>(&mut self) {
        self.register_resource(E::Storage::default())
    }

    /// Returns if an entity type is registered.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Entities, Entity, storage::ArenaStorage};
    /// struct Pos(u32, u32);
    /// impl Entity for Pos { type Storage = ArenaStorage<Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<Pos>();
    /// assert!(registry.has_entity::<Pos>());
    /// ```
    #[inline]
    fn has_entity<E: Entity>(&self) -> bool {
        self.has_resource::<E::Storage>()
    }

    /// Gets an entity storage.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Entities, Entity, storage::ArenaStorage};
    /// struct Pos(u32, u32);
    /// impl Entity for Pos { type Storage = ArenaStorage<Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<Pos>();
    /// let e = registry.entities::<Pos>();
    /// assert_eq!(e.len(), 0);
    /// ```
    #[inline]
    fn entities<'a, E: Entity>(&'a self) -> <Self as ResourceLocator<'a, E::Storage>>::Ref
    where
        Self: ResourceLocator<'a, E::Storage>,
    {
        ResourceLocator::<E::Storage>::get(self)
    }

    /// Gets an entity storage mutably.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Entities, Entity, storage::ArenaStorage};
    /// struct Pos(u32, u32);
    /// impl Entity for Pos { type Storage = ArenaStorage<Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<Pos>();
    /// let mut e = registry.entities_mut::<Pos>();
    /// e.insert(Pos(1, 2));
    /// assert_eq!(e.len(), 1);
    /// ```
    #[inline]
    fn entities_mut<'a, E: Entity>(&'a self) -> <Self as ResourceLocator<'a, E::Storage>>::RefMut
    where
        Self: ResourceLocator<'a, E::Storage>,
    {
        ResourceLocator::<E::Storage>::get_mut(self)
    }
}

impl<T: Resources> Entities for T {}

/// Registry for components.
pub trait Components: Resources {
    /// Registers an optional component type for an entity type.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Component, Components, Entity, Entities, storage::{ArenaStorage, VecStorage}};
    /// struct E;
    /// struct Pos(u32, u32);
    /// impl Entity for E { type Storage = ArenaStorage<Self>; }
    /// impl Component<E> for Pos { type Storage = VecStorage<E, Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<E>();
    /// registry.register_component::<E, Pos>();
    /// ```
    #[inline]
    fn register_component<E: Entity, C: Component<E>>(&mut self) {
        self.register_resource(C::Storage::default())
    }

    /// Returns if a component type is registered.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::ecs::{Registry, Component, Components, Entity, Entities, storage::{ArenaStorage, VecStorage}};
    /// struct E;
    /// struct Pos(u32, u32);
    /// impl Entity for E { type Storage = ArenaStorage<Self>; }
    /// impl Component<E> for Pos { type Storage = VecStorage<E, Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<E>();
    /// registry.register_component::<E, Pos>();
    /// assert!(registry.has_component::<E, Pos>());
    /// ```
    #[inline]
    fn has_component<E: Entity, C: Component<E>>(&self) -> bool {
        self.has_resource::<C::Storage>()
    }

    /// Gets a component storage.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::collections::Map;
    /// # use muds::ecs::{Registry, Component, Components, Entity, Entities, storage::{ArenaStorage, VecStorage}};
    /// struct E;
    /// struct Pos(u32, u32);
    /// impl Entity for E { type Storage = ArenaStorage<Self>; }
    /// impl Component<E> for Pos { type Storage = VecStorage<E, Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<E>();
    /// registry.register_component::<E, Pos>();
    /// let c = registry.components::<E, Pos>();
    /// assert_eq!(c.len(), 0);
    /// ```
    #[inline]
    fn components<'a, E: Entity, C: Component<E>>(
        &'a self,
    ) -> <Self as ResourceLocator<'a, C::Storage>>::Ref
    where
        Self: ResourceLocator<'a, C::Storage>,
    {
        ResourceLocator::<C::Storage>::get(self)
    }

    /// Gets a component storage mutably.
    ///
    /// # Examples
    /// ```rust
    /// # use muds::collections::{Map, MapMut};
    /// # use muds::ecs::{Registry, Component, Components, Entity, Entities, storage::{ArenaStorage, VecStorage}};
    /// struct E;
    /// struct Pos(u32, u32);
    /// impl Entity for E { type Storage = ArenaStorage<Self>; }
    /// impl Component<E> for Pos { type Storage = VecStorage<E, Self>; }
    ///
    /// let mut registry = Registry::default();
    /// registry.register_entity::<E>();
    /// registry.register_component::<E, Pos>();
    /// let mut e = registry.entities_mut::<E>();
    /// let mut c = registry.components_mut::<E, Pos>();
    ///
    /// c.insert(e.insert(E), Pos(1, 2));
    /// assert_eq!(c.len(), 1);
    /// ```
    #[inline]
    fn components_mut<'a, E: Entity, C: Component<E>>(
        &'a self,
    ) -> <Self as ResourceLocator<'a, C::Storage>>::RefMut
    where
        Self: ResourceLocator<'a, C::Storage>,
    {
        ResourceLocator::<C::Storage>::get_mut(self)
    }
}

impl<T: Resources> Components for T {}