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

use crate::component::{ComponentSend, ComponentThreadLocal, Component,
    OneToNComponentSend, OneToNComponentThreadLocal};

use crate::entity::{EntitiesCreation, Entity, EntityBuilder, EntityStoragesCreation};
use crate::storage::{HierarchicalStorage, OneToNStorage, Storage};
use crate::world::World;
use crate::storage::CreationSto;
use crate::{DebugParameter, ResourcesCreation};


/// Trait implemented by `World` and `CreationProxy` allows to create
/// functions that can create new entities, components and resources
/// without relying on an specific object
///
/// That way a such function can be called during initialization passing
/// a World as parameter or during run time passing a CreationProxy
///
/// Wrappers for `World` should implement this trait
pub trait EntitiesCreationExt<'a> {
    fn with_entities_creation<'e, F, R>(&'e mut self, f: F) -> R
    where F: FnOnce(EntitiesCreation<'e>) -> R;

    /// Returns an EntityBuilder that allows to create an entity and it's components.
    ///
    /// ```
    /// # #[macro_use] extern crate rinecs_derive;
    /// # use rinecs::*;
    /// # #[derive(Component, Debug)]
    /// # struct Position{x: f32, y: f32}
    /// # #[derive(Component, Debug)]
    /// # struct Velocity{x: f32, y: f32}
    /// # let mut world = World::new();
    /// let e = world.new_entity()
    ///     .add(Position{x: 0., y: 0.})
    ///     .add(Velocity{x: 0., y: 0.})
    ///     .build();
    /// ```
    fn new_entity(&mut self) -> EntityBuilder;

    /// Adds a `Send` `Component` to an already existing `Entity`.
    fn add_component_to<'r, C: ComponentSend>(&mut self, entity: &Entity, component: C)
    where
        for<'s> <C as Component>::Storage: Storage<'s, C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
    {
        self.with_entities_creation(|mut proxy| proxy.add_component_to(entity, component))
    }

    /// Adds a non `Send` `Component` to an already existing `Entity`.
    fn add_component_to_thread_local<'r, C: ComponentThreadLocal>(&mut self, entity: &Entity, component: C)
    where
        for<'s> <C as Component>::Storage: Storage<'s, C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
    {
        self.with_entities_creation(|mut proxy| proxy.add_component_to_thread_local(entity, component))
    }

    /// Adds a `Tag` to an already existing `Entity`.
    fn add_tag_to<C: 'static>(&mut self, entity: &Entity) {
        self.with_entities_creation(|mut proxy| proxy.add_tag_to::<C>(entity))
    }

    /// Adds a `Send` `Component` to an already existing `Entity`.
    fn add_child_component_to<'r, C: ComponentSend>(&mut self, parent: &Entity, entity: &Entity, component: C)
    where
        for<'b> <C as Component>::Storage: HierarchicalStorage<'b,C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
    {
        self.with_entities_creation(|mut proxy| proxy.add_child_component_to(parent, entity, component))
    }

    /// Adds a non `Send` `Component` to an already existing `Entity`.
    fn add_child_component_to_thread_local<'r, C: ComponentThreadLocal>(&mut self, parent: &Entity, entity: &Entity, component: C)
    where
        for<'b> <C as Component>::Storage: HierarchicalStorage<'b,C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
    {
        self.with_entities_creation(|mut proxy| proxy.add_child_component_to_thread_local(parent, entity, component))
    }

    /// Adds a `Send` `OneToNComponent` to an already existing `Entity`.
    ///
    /// This allows to add a slice of components to an entity instead of only
    /// one component. This components are stored contiguously in memory for
    /// all the entities.
    fn add_slice_component_to<'r, C, I>(&mut self, entity: &Entity, component: I)
    where
        C: OneToNComponentSend,
        for<'s> <C as Component>::Storage: OneToNStorage<'s, C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
        I: IntoIterator<Item = C>
    {
        self.with_entities_creation(|mut proxy| proxy.add_slice_component_to(entity, component))
    }

    /// Adds a non `Send` `OneToNComponent` to an already existing `Entity`.
    ///
    /// This allows to add a slice of components to an entity instead of only
    /// one component. This components are stored contiguously in memory for
    /// all the entities.
    fn add_slice_component_to_thread_local<'r, C, I>(&mut self, entity: &Entity, component: I)
    where
        C: OneToNComponentThreadLocal,
        for<'s> <C as Component>::Storage: OneToNStorage<'s, C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
        I: IntoIterator<Item = C>
    {
        self.with_entities_creation(|mut proxy| proxy.add_slice_component_to_thread_local(entity, component))
    }

    /// Removes a compoenent of the specified type from an entity.
    fn remove_component_from<'r, C: Component>(&mut self, entity: &Entity)
    where
        for<'s> <C as Component>::Storage: Storage<'s, C>,
        <<C as Component>::Storage as Storage<'r, C>>::Get: DebugParameter,
    {
        self.with_entities_creation(|mut proxy| proxy.remove_component_from::<C>(entity))
    }

    /// Removes an `Entity` and all it's compoenents.
    fn remove_entity(&mut self, entity: &Entity) {
        self.with_entities_creation(|mut proxy| proxy.remove_entity(entity))
    }

    fn creation_storage<C: Component>(&mut self) -> CreationSto<C>;
}

pub trait ResourcesCreationExt {
    fn with_resources_creation<F, R>(&mut self, f: F) -> R
    where F: FnOnce(ResourcesCreation) -> R;

    /// Adds a `Send` resource to the world.
    ///
    /// Resources are globally accesible by any system through
    /// the `Resources` object passed as parameter to them.
    fn add_resource<T: 'static + Send>(&mut self, resource: T) {
        self.with_resources_creation(|mut resources| resources.add_resource(resource))
    }

    /// Removes a resource of the specified type.
    fn remove_resource<T: 'static>(&mut self) -> Option<T>{
        self.with_resources_creation(|mut resources| resources.remove_resource())
    }

    /// Adds a non `Send` resource to the world.
    ///
    /// Non `Send` resources are globally accesible by any `SystemThreadLocal`
    /// through the `ResourcesThreadLocal` object passed as parameter to them.
    fn add_resource_thread_local<T: 'static>(&mut self, resource: T) {
        self.with_resources_creation(|mut proxy| proxy.add_resource_thread_local(resource))
    }

    /// Adds a `Send` resource to the world accessible as a &dyn Trait.
    ///
    /// Resources are globally accesible by any system through
    /// the `Resources` object passed as parameter to them.
    fn add_resource_as_trait<T, U, F, FMut>(&mut self, resource: (T, F, FMut))
    where
        T: 'static + Send,
        U: 'static + Send + ?Sized,
        F: Fn(&T) -> &U + 'static,
        FMut: Fn(&mut T) -> &mut U + 'static
    {
        self.with_resources_creation(|mut proxy| proxy.add_resource_as_trait(resource))
    }

    /// Adds a non `Send` resource to the world accessible as a &dyn Trait.
    ///
    /// Resources are globally accesible by any system through
    /// the `Resources` object passed as parameter to them.
    fn add_resource_as_trait_thread_local<T, U, F, FMut>(&mut self, resource: (T, F, FMut))
    where
        T: 'static,
        U: 'static + ?Sized,
        F: Fn(&T) -> &U + 'static,
        FMut: Fn(&mut T) -> &mut U + 'static
    {
        self.with_resources_creation(|mut proxy| proxy.add_resource_as_trait_thread_local(resource))
    }
}

pub trait CreationContextExt<'a>: EntitiesCreationExt<'a> + ResourcesCreationExt {
    fn with_entities_and_resources_creation<'e, F, R>(&'e mut self, f: F) -> R
    where F: FnOnce(EntitiesCreation<'e>, ResourcesCreation<'e>) -> R;
}

impl<'a> EntitiesCreationExt<'a> for EntitiesCreation<'a>{
    fn with_entities_creation<'e, F, R>(&'e mut self, f: F) -> R
    where F: FnOnce(EntitiesCreation<'e>) -> R
    {
        f(EntitiesCreation{
            storages: self.storages.clone(),
            next_guid: self.next_guid,
        })
    }

    fn new_entity(&mut self) -> EntityBuilder{
        self.new_entity()
    }

    fn creation_storage<C: Component>(&mut self) -> CreationSto<C> {
        self.storages_mut().creation_storage()
    }
}

impl<'a> EntitiesCreationExt<'a> for EntityStoragesCreation<'a>{
    fn with_entities_creation<'e, F, R>(&'e mut self, f: F) -> R
    where F: FnOnce(EntitiesCreation<'e>) -> R
    {
        f(EntitiesCreation{
            storages: self.storages.clone(),
            next_guid: self.next_guid,
        })
    }

    fn new_entity(&mut self) -> EntityBuilder{
        self.new_entity()
    }

    fn creation_storage<C: Component>(&mut self) -> CreationSto<C> {
        self.storages_mut().creation_storage()
    }
}

impl<'a> ResourcesCreationExt for ResourcesCreation<'a> {
    fn with_resources_creation<F, R>(&mut self, f: F) -> R
    where F: FnOnce(ResourcesCreation) -> R {
        f(self.clone())
    }
}

impl<'a> EntitiesCreationExt<'a> for World{
    fn with_entities_creation<'e, F, R>(&'e mut self, f: F) -> R
    where F: FnOnce(EntitiesCreation<'e>) -> R
    {
        let creation_proxy = self.entities_creation();
        f(creation_proxy)
    }

    fn new_entity(&mut self) -> EntityBuilder{
        self.new_entity()
    }

    fn creation_storage<C: Component>(&mut self) -> CreationSto<C> {
        self.creation_storage()
    }
}

impl ResourcesCreationExt for World {
    fn with_resources_creation<F, R>(&mut self, f: F) -> R
    where F: FnOnce(ResourcesCreation) -> R {
        f(self.resources_creation())
    }
}