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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#![deny(missing_docs)]
extern crate fnv;
extern crate rustc_serialize;
use std::default::Default;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::collections::{HashMap, hash_map, HashSet, hash_set};
use std::iter::FromIterator;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, RustcEncodable, RustcDecodable)]
pub struct Entity(pub usize);
pub trait AnyComponent {
fn remove(&mut self, e: Entity);
}
pub struct ComponentData<C> {
data: fnv::FnvHashMap<Entity, C>,
}
impl<C: Encodable+Clone> Encodable for ComponentData<C> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let data: HashMap<Entity, C> = HashMap::from_iter(self.data.clone().into_iter());
data.encode(s)
}
}
impl<C: Decodable> Decodable for ComponentData<C> {
fn decode<D: Decoder>(d: &mut D) -> Result<ComponentData<C>, D::Error> {
let data: HashMap<Entity, C> = HashMap::decode(d)?;
Ok(ComponentData {
data: fnv::FnvHashMap::from_iter(data.into_iter())
})
}
}
impl<C> ComponentData<C> {
pub fn new() -> ComponentData<C> {
ComponentData { data: fnv::FnvHashMap::default() }
}
pub fn insert(&mut self, e: Entity, comp: C) {
self.data.insert(e, comp);
}
pub fn contains(&self, e: Entity) -> bool {
self.data.contains_key(&e)
}
pub fn get(&self, e: Entity) -> Option<&C> {
self.data.get(&e)
}
pub fn get_mut(&mut self, e: Entity) -> Option<&mut C> {
self.data.get_mut(&e)
}
pub fn iter(&self) -> hash_map::Iter<Entity, C> {
self.data.iter()
}
pub fn iter_mut(&mut self) -> hash_map::IterMut<Entity, C> {
self.data.iter_mut()
}
}
impl<C> Index<Entity> for ComponentData<C> {
type Output = C;
fn index<'a>(&'a self, e: Entity) -> &'a C {
self.data.get(&e).unwrap()
}
}
impl<C> IndexMut<Entity> for ComponentData<C> {
fn index_mut<'a>(&'a mut self, e: Entity) -> &'a mut C {
self.data.get_mut(&e).unwrap()
}
}
impl<C> AnyComponent for ComponentData<C> {
fn remove(&mut self, e: Entity) {
self.data.remove(&e);
}
}
pub trait Store {
fn for_each_component<F>(&mut self, f: F) where F: FnMut(&mut AnyComponent);
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct Ecs<ST> {
next_uid: usize,
active: HashSet<Entity>,
store: ST,
}
impl<ST: Default + Store> Ecs<ST> {
pub fn new() -> Ecs<ST> {
Ecs {
next_uid: 1,
active: HashSet::default(),
store: Default::default(),
}
}
pub fn make(&mut self) -> Entity {
let next = self.next_uid;
self.next_uid += 1;
let ret = Entity(next);
self.active.insert(ret);
ret
}
pub fn remove(&mut self, e: Entity) {
self.active.remove(&e);
self.store.for_each_component(|c| c.remove(e));
}
pub fn contains(&self, e: Entity) -> bool {
self.active.contains(&e)
}
pub fn iter(&self) -> hash_set::Iter<Entity> {
self.active.iter()
}
}
impl<ST> Deref for Ecs<ST> {
type Target = ST;
fn deref(&self) -> &ST {
&self.store
}
}
impl<ST> DerefMut for Ecs<ST> {
fn deref_mut(&mut self) -> &mut ST {
&mut self.store
}
}
#[macro_export]
macro_rules! Ecs {
{
$($compname:ident: $comptype:ty,)+
} => {
mod _ecs_inner {
#[allow(non_camel_case_types, dead_code)]
pub enum ComponentNum {
$($compname,)+
}
}
pub use self::_ecs_inner::ComponentNum;
#[derive(RustcEncodable, RustcDecodable)]
pub struct _ComponentStore {
$(pub $compname: $crate::ComponentData<$comptype>),+
}
impl ::std::default::Default for _ComponentStore {
fn default() -> _ComponentStore {
_ComponentStore {
$($compname: $crate::ComponentData::new()),+
}
}
}
impl $crate::Store for _ComponentStore {
fn for_each_component<F>(&mut self, mut f: F)
where F: FnMut(&mut $crate::AnyComponent)
{
$(f(&mut self.$compname as &mut $crate::AnyComponent);)+
}
}
#[allow(dead_code)]
pub fn matches_mask(ecs: &$crate::Ecs<_ComponentStore>, e: $crate::Entity, mask: u64) -> bool {
$(if mask & (1 << ComponentNum::$compname as u8) != 0 && !ecs.$compname.contains(e) {
return false;
})+
return true;
}
pub trait Component {
fn add_to_ecs(&self, ecs: &mut $crate::Ecs<_ComponentStore>, e: $crate::Entity);
fn add_to_loadout(self, loadout: &mut Loadout);
}
$(impl Component for $comptype {
fn add_to_ecs(&self, ecs: &mut $crate::Ecs<_ComponentStore>, e: $crate::Entity) {
ecs.$compname.insert(e, self.clone());
}
fn add_to_loadout(self, loadout: &mut Loadout) {
loadout.$compname = Some(self);
}
})+
pub type Ecs = $crate::Ecs<_ComponentStore>;
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Loadout {
$(pub $compname: Option<$comptype>),+
}
impl ::std::default::Default for Loadout {
fn default() -> Loadout {
Loadout {
$($compname: None),+
}
}
}
#[allow(dead_code)]
impl Loadout {
pub fn new() -> Loadout { Default::default() }
pub fn get(ecs: &Ecs, e: $crate::Entity) -> Loadout {
Loadout {
$($compname: ecs.$compname.get(e).map(|e| e.clone())),+
}
}
pub fn make(&self, ecs: &mut Ecs) -> $crate::Entity {
let e = ecs.make();
$(self.$compname.as_ref().map(|c| ecs.$compname.insert(e, c.clone()));)+
e
}
pub fn c<C: Component>(mut self, comp: C) -> Loadout {
comp.add_to_loadout(&mut self);
self
}
}
}
}
#[macro_export]
macro_rules! build_mask {
( $($compname:ident),+ ) => {
0u64 $(| (1u64 << ComponentNum::$compname as u8))+
}
}