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
#[cfg(feature="serialisation")] use cereal::{CerealData, CerealResult}; #[cfg(feature="serialisation")] use std::io::{Read, Write}; use std::ops::{Deref, DerefMut}; use {EntityData, ModifyData}; use {Entity, IndexedEntity, EntityIter}; use {EntityBuilder, EntityModifier}; use entity::EntityManager; pub struct World<S> where S: SystemManager { pub systems: S, pub data: DataHelper<S::Components, S::Services>, } pub struct DataHelper<C, M> where C: ComponentManager, M: ServiceManager { pub components: C, pub services: M, entities: EntityManager<C>, } pub trait ComponentManager: 'static+Sized { #[doc(hidden)] fn __new() -> Self; #[doc(hidden)] fn __remove_all(&mut self, &IndexedEntity<Self>); } pub trait ServiceManager: 'static {} impl ServiceManager for () {} pub trait SystemManager { type Components: ComponentManager; type Services: ServiceManager; #[doc(hidden)] fn __new() -> Self; #[doc(hidden)] fn __activated(&mut self, EntityData<Self::Components>, &Self::Components, &mut Self::Services); #[doc(hidden)] fn __reactivated(&mut self, EntityData<Self::Components>, &Self::Components, &mut Self::Services); #[doc(hidden)] fn __deactivated(&mut self, EntityData<Self::Components>, &Self::Components, &mut Self::Services); #[doc(hidden)] fn __update(&mut self, &mut DataHelper<Self::Components, Self::Services>); } impl<S: SystemManager> Deref for World<S> { type Target = DataHelper<S::Components, S::Services>; fn deref(&self) -> &DataHelper<S::Components, S::Services> { &self.data } } impl<S: SystemManager> DerefMut for World<S> { fn deref_mut(&mut self) -> &mut DataHelper<S::Components, S::Services> { &mut self.data } } impl<C: ComponentManager, M: ServiceManager> Deref for DataHelper<C, M> { type Target = C; fn deref(&self) -> &C { &self.components } } impl<C: ComponentManager, M: ServiceManager> DerefMut for DataHelper<C, M> { fn deref_mut(&mut self) -> &mut C { &mut self.components } } impl<C: ComponentManager, M: ServiceManager> DataHelper<C, M> { pub fn with_entity_data<F, R>(&mut self, entity: &Entity, mut call: F) -> Option<R> where F: FnMut(EntityData<C>, &mut C) -> R { if self.entities.is_valid(entity) { Some(call(EntityData(&self.entities.indexed(&entity).__clone()), self)) } else { None } } pub fn create_entity<B>(&mut self, builder: B) -> Entity where B: EntityBuilder<C> { self.entities.create_entity(builder, &mut self.components) } pub fn remove_entity(&mut self, entity: Entity) { self.entities.remove_entity(entity); } } #[cfg(feature="serialisation")] unsafe impl<C: ComponentManager, M: ServiceManager> CerealData for DataHelper<C, M> where C: CerealData, M: CerealData { fn write(&self, w: &mut Write) -> CerealResult<()> { try!(self.services.write(w)); try!(self.entities.write(w)); self.components.write(w) } fn read(r: &mut Read) -> CerealResult<Self> { let services = try!(CerealData::read(r)); let entities = try!(CerealData::read(r)); let components = try!(CerealData::read(r)); Ok(DataHelper { components: components, services: services, entities: entities, }) } } #[cfg(feature="serialisation")] impl<S: SystemManager> World<S> where DataHelper<S::Components, S::Services>: CerealData { pub fn load(reader: &mut Read) -> CerealResult<World<S>> { let mut world = World { systems: S::__new(), data: try!(CerealData::read(reader)), }; world.refresh(); Ok(world) } pub fn save(&mut self, writer: &mut Write) -> CerealResult<()> { self.flush_queue(); self.data.write(writer) } } impl<S: SystemManager> World<S> { pub fn new() -> World<S> where S::Services: Default { World { systems: S::__new(), data: DataHelper { components: S::Components::__new(), services: S::Services::default(), entities: EntityManager::new(), }, } } pub fn with_services(services: S::Services) -> World<S> { World { systems: S::__new(), data: DataHelper { components: S::Components::__new(), services: services, entities: EntityManager::new(), }, } } pub fn entities(&self) -> EntityIter<S::Components> { self.data.entities.iter() } pub fn modify_entity<M>(&mut self, entity: Entity, modifier: M) where M: EntityModifier<S::Components> { let indexed = self.data.entities.indexed(&entity); modifier.modify(ModifyData(indexed), &mut self.data.components); self.systems.__reactivated( EntityData(indexed), &self.data.components, &mut self.data.services ); } pub fn refresh(&mut self) { self.flush_queue(); for entity in self.data.entities.iter() { self.systems.__reactivated(entity, &self.data.components, &mut self.data.services); } } pub fn flush_queue(&mut self) { self.data.entities.flush_queue( &mut self.data.components, &mut self.data.services, &mut self.systems ); } pub fn update(&mut self) { self.flush_queue(); self.systems.__update(&mut self.data); self.flush_queue(); } }