scion 0.7.0

Game making library on top of wgpu, winit, hecs
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::any::TypeId;

use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use std::hash::Hasher;

use crate::core::components::maths::camera::DefaultCamera;
use crate::core::resources::asset_manager::AssetManager;
use crate::core::resources::audio::Audio;
use crate::core::resources::events::Events;
use crate::core::resources::inputs::inputs_controller::InputsController;
use crate::core::resources::time::Timers;
use crate::core::resources::window::Window;
use crate::core::scene::SceneController;
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use downcast_rs::{impl_downcast, Downcast};
use hecs::{
    Component, ComponentError, DynamicBundle, Entity, NoSuchEntity, Query, QueryBorrow,
    QueryMut, QueryOne, QueryOneError,
};
use crate::core::resources::focus_manager::FocusManager;
use crate::core::resources::font_atlas::FontAtlas;
use crate::core::state::GameState;

pub trait World {
    fn entities(&self) -> HashSet<Entity>;
    fn clear(&mut self);
    fn push(&mut self, components: impl DynamicBundle) -> Entity;
    fn remove(&mut self, entity: Entity) -> Result<(), NoSuchEntity>;
    fn add_components(
        &mut self,
        entity: Entity,
        components: impl DynamicBundle,
    ) -> Result<(), NoSuchEntity>;
    fn remove_component<T: Component>(&mut self, entity: Entity) -> Result<T, ComponentError>;
    fn query<Q: Query>(&self) -> QueryBorrow<'_, Q>;
    fn query_mut<Q: Query>(&mut self) -> QueryMut<'_, Q>;
    fn entry<Q: Query>(&self, entity: Entity) -> Result<QueryOne<'_, Q>, NoSuchEntity>;
    fn entry_mut<Q: Query>(&mut self, entity: Entity) -> Result<Q::Item<'_>, QueryOneError>;
    fn contains(&self, entity: Entity) -> bool;
    fn add_default_camera(&mut self) -> Entity;
}

#[derive(Default)]
pub struct GameData {
    pub(crate) subworld: SubWorld,
    pub(crate) resources: Resources,
}

impl GameData {
    pub fn split(&mut self) -> (&mut SubWorld, &mut Resources) {
        (&mut self.subworld, &mut self.resources)
    }

    pub fn contains_resource<T: Resource>(&self) -> bool {
        self.resources.internal_resources.storage.contains_key(&ResourceTypeId::of::<T>())
    }

    pub fn insert_resource<T: Resource>(&mut self, resource: T) {
        self.resources
            .internal_resources
            .storage
            .insert(ResourceTypeId::of::<T>(), AtomicResourceCell::new(Box::new(resource)));
    }

    pub fn remove_resource<T: Resource>(&mut self) -> Option<T> {
        let resource = self
            .resources
            .internal_resources
            .remove_internal(&ResourceTypeId::of::<T>())?
            .downcast::<T>()
            .ok()?;
        Some(*resource)
    }

    pub fn get_resource<T: Resource>(&self) -> Option<AtomicRef<T>> {
        let type_id = &ResourceTypeId::of::<T>();
        self.resources.internal_resources.storage.get(type_id).map(|x| x.get::<T>())
    }

    pub fn get_resource_mut<T: Resource>(&self) -> Option<AtomicRefMut<T>> {
        let type_id = &ResourceTypeId::of::<T>();
        self.resources.internal_resources.storage.get(type_id).map(|x| x.get_mut::<T>())
    }

    /// retrieves the game_state from the resources.
    pub fn game_state(&self) -> AtomicRef<GameState> {
        self.get_resource::<GameState>()
            .expect("The engine is missing the mandatory game state resource")
    }

    /// retrieves the game_state mutable from the resources.
    pub fn game_state_mut(&self) -> AtomicRefMut<GameState> {
        self.get_resource_mut::<GameState>()
            .expect("The engine is missing the mandatory game state resource")
    }

    /// retrieves the asset manager from the resources.
    pub fn assets(&self) -> AtomicRef<AssetManager> {
        self.get_resource::<AssetManager>()
            .expect("The engine is missing the mandatory asset manager resource")
    }

    /// retrieves the asset manager from the resources.
    pub fn assets_mut(&self) -> AtomicRefMut<AssetManager> {
        self.get_resource_mut::<AssetManager>()
            .expect("The engine is missing the mandatory asset manager resource")
    }

    /// retrieves the timers resource from the resources.
    pub fn timers(&self) -> AtomicRefMut<Timers> {
        self.get_resource_mut::<Timers>()
            .expect("The engine is missing the mandatory timers resource")
    }

    /// retrieves the inputs resource from the resources
    pub fn inputs(&self) -> AtomicRefMut<InputsController> {
        self.get_resource_mut::<InputsController>()
            .expect("The engine is missing the mandatory inputs controller resource")
    }

    /// retrieves the events resource from the resources
    pub fn events(&self) -> AtomicRefMut<Events> {
        self.get_resource_mut::<Events>()
            .expect("The engine is missing the mandatory events resource")
    }

    /// retrieves the audio player from the resources
    pub fn audio(&self) -> AtomicRefMut<Audio> {
        self.get_resource_mut::<Audio>()
            .expect("The engine is missing the mandatory audio player resource")
    }

    /// retrieves the window from the resources
    pub fn window(&self) -> AtomicRefMut<Window> {
        self.get_resource_mut::<Window>()
            .expect("The engine is missing the mandatory window resource")
    }

    /// retrieves the window from the resources
    pub fn scene_controller(&self) -> AtomicRefMut<SceneController> {
        self.get_resource_mut::<SceneController>()
            .expect("The engine is missing the mandatory scene controller resource")
    }

    /// retrieves the font_atlas from the resources.
    pub(crate) fn font_atlas(&self) -> AtomicRefMut<FontAtlas> {
        self.get_resource_mut::<FontAtlas>()
            .expect("The engine is missing the mandatory font_atlas resource")
    }

    /// retrieves the focus manager from the resources.
    #[allow(dead_code)]
    pub(crate) fn focus_manager(&self) -> AtomicRefMut<FocusManager> {
        self.get_resource_mut::<FocusManager>()
            .expect("The engine is missing the mandatory focus manager resource")
    }
}

impl World for GameData {
    fn entities(&self) -> HashSet<Entity> {
        self.subworld
            .internal_world
            .iter()
            .map(|entity_ref| entity_ref.entity())
            .collect::<HashSet<_>>()
    }

    fn clear(&mut self) {
        self.subworld.internal_world.clear();
    }

    fn push(&mut self, components: impl DynamicBundle) -> Entity {
        self.subworld.internal_world.spawn(components)
    }

    fn remove(&mut self, entity: Entity) -> Result<(), NoSuchEntity> {
        self.subworld.internal_world.despawn(entity)
    }

    fn add_components(
        &mut self,
        entity: Entity,
        components: impl DynamicBundle,
    ) -> Result<(), NoSuchEntity> {
        self.subworld.internal_world.insert(entity, components)
    }

    fn remove_component<T: Component>(&mut self, entity: Entity) -> Result<T, ComponentError> {
        self.subworld.internal_world.remove_one::<T>(entity)
    }

    fn query<Q: Query>(&self) -> QueryBorrow<'_, Q> {
        self.subworld.internal_world.query::<Q>()
    }

    fn query_mut<Q: Query>(&mut self) -> QueryMut<'_, Q> {
        self.subworld.internal_world.query_mut::<Q>()
    }

    fn entry<Q: Query>(&self, entity: Entity) -> Result<QueryOne<'_, Q>, NoSuchEntity> {
        self.subworld.internal_world.query_one::<Q>(entity)
    }

    fn entry_mut<Q: Query>(&mut self, entity: Entity) -> Result<Q::Item<'_>, QueryOneError> {
        self.subworld.internal_world.query_one_mut::<Q>(entity)
    }

    fn contains(&self, entity: Entity) -> bool {
        self.subworld.internal_world.contains(entity)
    }

    fn add_default_camera(&mut self) -> Entity {
        self.push((DefaultCamera,))
    }
}

#[derive(Default)]
pub struct SubWorld {
    internal_world: hecs::World,
}

#[derive(Default)]
pub struct Resources {
    internal_resources: InternalResources,
}

impl World for SubWorld {
    fn entities(&self) -> HashSet<Entity> {
        self.internal_world.iter().map(|entity_ref| entity_ref.entity()).collect::<HashSet<_>>()
    }

    fn clear(&mut self) {
        self.internal_world.clear();
    }

    fn push(&mut self, components: impl DynamicBundle) -> Entity {
        self.internal_world.spawn(components)
    }

    fn remove(&mut self, entity: Entity) -> Result<(), NoSuchEntity> {
        self.internal_world.despawn(entity)
    }

    fn add_components(
        &mut self,
        entity: Entity,
        components: impl DynamicBundle,
    ) -> Result<(), NoSuchEntity> {
        self.internal_world.insert(entity, components)
    }

    fn remove_component<T: Component>(&mut self, entity: Entity) -> Result<T, ComponentError> {
        self.internal_world.remove_one::<T>(entity)
    }

    fn query<Q: Query>(&self) -> QueryBorrow<'_, Q> {
        self.internal_world.query::<Q>()
    }

    fn query_mut<Q: Query>(&mut self) -> QueryMut<'_, Q> {
        self.internal_world.query_mut::<Q>()
    }

    fn entry<Q: Query>(&self, entity: Entity) -> Result<QueryOne<'_, Q>, NoSuchEntity> {
        self.internal_world.query_one::<Q>(entity)
    }

    fn entry_mut<Q: Query>(&mut self, entity: Entity) -> Result<Q::Item<'_>, QueryOneError> {
        self.internal_world.query_one_mut::<Q>(entity)
    }

    fn contains(&self, entity: Entity) -> bool {
        self.internal_world.contains(entity)
    }

    fn add_default_camera(&mut self) -> Entity {
        self.push((DefaultCamera,))
    }
}

impl Resources {
    pub fn contains_resource<T: Resource>(&self) -> bool {
        self.internal_resources.storage.contains_key(&ResourceTypeId::of::<T>())
    }

    pub fn insert_resource<T: Resource>(&mut self, resource: T) {
        self.internal_resources
            .storage
            .insert(ResourceTypeId::of::<T>(), AtomicResourceCell::new(Box::new(resource)));
    }

    pub fn remove_resource<T: Resource>(&mut self) -> Option<T> {
        let resource = self
            .internal_resources
            .remove_internal(&ResourceTypeId::of::<T>())?
            .downcast::<T>()
            .ok()?;
        Some(*resource)
    }

    pub fn get_resource<T: Resource>(&self) -> Option<AtomicRef<T>> {
        let type_id = &ResourceTypeId::of::<T>();
        self.internal_resources.storage.get(type_id).map(|x| x.get::<T>())
    }

    pub fn get_resource_mut<T: Resource>(&self) -> Option<AtomicRefMut<T>> {
        let type_id = &ResourceTypeId::of::<T>();
        self.internal_resources.storage.get(type_id).map(|x| x.get_mut::<T>())
    }

    /// retrieves the asset manager from the resources.
    pub fn game_state(&self) -> AtomicRef<GameState> {
        self.get_resource::<GameState>()
            .expect("The engine is missing the mandatory game state resource")
    }

    /// retrieves the asset manager from the resources.
    pub fn game_state_mut(&self) -> AtomicRefMut<GameState> {
        self.get_resource_mut::<GameState>()
            .expect("The engine is missing the mandatory game state resource")
    }

    /// retrieves the asset manager from the resources.
    pub fn assets(&self) -> AtomicRef<AssetManager> {
        self.get_resource::<AssetManager>()
            .expect("The engine is missing the mandatory asset manager resource")
    }

    /// retrieves the asset manager from the resources.
    pub fn assets_mut(&self) -> AtomicRefMut<AssetManager> {
        self.get_resource_mut::<AssetManager>()
            .expect("The engine is missing the mandatory asset manager resource")
    }

    /// retrieves the timers resource from the resources.
    pub fn timers(&self) -> AtomicRefMut<Timers> {
        self.get_resource_mut::<Timers>()
            .expect("The engine is missing the mandatory timers resource")
    }

    /// retrieves the inputs resource from the resources
    pub fn inputs(&self) -> AtomicRefMut<InputsController> {
        self.get_resource_mut::<InputsController>()
            .expect("The engine is missing the mandatory inputs controller resource")
    }

    /// retrieves the events resource from the resources
    pub fn events(&self) -> AtomicRefMut<Events> {
        self.get_resource_mut::<Events>()
            .expect("The engine is missing the mandatory events resource")
    }

    /// retrieves the audio player from the resources
    pub fn audio(&self) -> AtomicRefMut<Audio> {
        self.get_resource_mut::<Audio>()
            .expect("The engine is missing the mandatory audio player resource")
    }

    /// retrieves the window from the resources
    pub fn window(&self) -> AtomicRefMut<Window> {
        self.get_resource_mut::<Window>()
            .expect("The engine is missing the mandatory window resource")
    }

    /// retrieves the window from the resources
    pub fn scene_controller(&self) -> AtomicRefMut<SceneController> {
        self.get_resource_mut::<SceneController>()
            .expect("The engine is missing the mandatory scene controller resource")
    }

    /// retrieves the font_atlas from the resources.
    pub(crate) fn font_atlas(&self) -> AtomicRefMut<FontAtlas> {
        self.get_resource_mut::<FontAtlas>()
            .expect("The engine is missing the mandatory font_atlas resource")
    }

    /// retrieves the focus manager from the resources.
    pub(crate) fn focus_manager(&self) -> AtomicRefMut<FocusManager> {
        self.get_resource_mut::<FocusManager>()
            .expect("The engine is missing the mandatory focus manager resource")
    }
}

#[derive(Default)]
pub struct InternalResources {
    storage: HashMap<ResourceTypeId, AtomicResourceCell>,
}

unsafe impl Send for InternalResources {}

unsafe impl Sync for InternalResources {}

impl InternalResources {
    fn remove_internal(&mut self, type_id: &ResourceTypeId) -> Option<Box<dyn Resource>> {
        self.storage.remove(type_id).map(|cell| cell.into_inner())
    }
}

pub trait Resource: 'static + Downcast {}

impl<T> Resource for T where T: 'static {}
impl_downcast!(Resource);

#[derive(Copy, Clone, Debug, Eq, PartialOrd, Ord)]
pub struct ResourceTypeId {
    type_id: TypeId,
    name: &'static str,
}

impl ResourceTypeId {
    /// Returns the resource type ID of the given resource type.
    pub fn of<T: Resource>() -> Self {
        Self { type_id: TypeId::of::<T>(), name: std::any::type_name::<T>() }
    }
}

impl std::hash::Hash for ResourceTypeId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.type_id.hash(state);
    }
}

impl PartialEq for ResourceTypeId {
    fn eq(&self, other: &Self) -> bool {
        self.type_id.eq(&other.type_id)
    }
}

impl Display for ResourceTypeId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

pub struct AtomicResourceCell {
    data: AtomicRefCell<Box<dyn Resource>>,
}

impl AtomicResourceCell {
    fn new(resource: Box<dyn Resource>) -> Self {
        Self { data: AtomicRefCell::new(resource) }
    }

    fn into_inner(self) -> Box<dyn Resource> {
        self.data.into_inner()
    }

    pub fn get<T: Resource>(&self) -> AtomicRef<T> {
        let borrow = self.data.borrow(); // panics if this is borrowed already
        AtomicRef::map(borrow, |inner| inner.downcast_ref::<T>().unwrap())
    }

    pub fn get_mut<T: Resource>(&self) -> AtomicRefMut<T> {
        let borrow = self.data.borrow_mut(); // panics if this is borrowed already
        AtomicRefMut::map(borrow, |inner| inner.downcast_mut::<T>().unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple_read_write_test() {
        struct TestOne {
            value: String,
        }

        struct TestTwo {
            value: String,
        }

        let mut world = GameData::default();
        world.insert_resource(TestOne { value: "one".to_string() });
        world.insert_resource(TestTwo { value: "two".to_string() });

        world.push((1, TestOne { value: "".to_string() }));

        let (subworld, resources) = world.split();

        let res1 = resources.get_resource_mut::<TestOne>().unwrap();
        let res2 = resources.get_resource_mut::<TestTwo>().unwrap();

        for (_e, _i) in subworld.query_mut::<&mut TestOne>() {
            println!("hello");
            println!("{:?}", res1.value);
            println!("{:?}", res2.value);
        }

        assert_eq!(res1.value, "one");
        assert_eq!(res2.value, "two");
    }
}