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
//! Entity Component System based on sparse sets.
//!
//! ```rust,no_test
//! use sparsey::prelude::*;
//!
//! struct Position(f32);
//! struct Velocity(f32);
//!
//! fn main() {
//!     let mut entities = EntityStorage::default();
//!     entities.register::<Position>();
//!     entities.register::<Velocity>();
//!
//!     entities.create((Position(0.0),));
//!     entities.create((Position(0.0), Velocity(1.0)));
//!     entities.create((Position(0.0), Velocity(2.0)));
//!
//!     entities.run(|mut positions: CompMut<Position>, velocities: Comp<Velocity>| {
//!         (&mut positions, &velocities).for_each(|(position, velocity)| {
//!             position.0 += velocity.0;
//!         });
//!    });
//! }
//! ```

pub mod entity;
pub mod query;
pub mod resource;
pub mod system;
pub mod util;

/// Re-exports the most commonly used items.
pub mod prelude {
    pub use crate::entity::{Comp, CompMut, Entities, Entity, EntityStorage, GroupLayout};
    pub use crate::query::{BuildCompoundQuery, IntoEntityIter, Query};
    pub use crate::resource::{Res, ResMut, ResourceStorage};
    pub use crate::system::{IntoSystem, Run, System};
    pub use crate::World;
}

use crate::entity::{EntityStorage, GroupLayout};
use crate::resource::ResourceStorage;

/// Storage for entities and resources.
#[derive(Default, Debug)]
pub struct World {
    /// Storage for entities.
    pub entities: EntityStorage,
    /// Storage for resources.
    pub resources: ResourceStorage,
}

impl World {
    /// Creates a new world with the given group layout.
    #[inline]
    #[must_use]
    pub fn new(layout: &GroupLayout) -> Self {
        Self {
            entities: EntityStorage::new(layout),
            resources: ResourceStorage::default(),
        }
    }

    /// Returns whether the world contains no entities and no resources.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entities.is_empty() && self.resources.is_empty()
    }

    /// Removes all entities and all resources from the storage.
    #[inline]
    pub fn clear(&mut self) {
        self.entities.clear();
        self.resources.clear();
    }

    /// Removes all entities and all resources from the storage and resets the entity allocator.
    ///
    /// After this call, the storage is allowed to return previously allocated entities.
    #[inline]
    pub fn reset(&mut self) {
        self.entities.reset();
        self.resources.clear();
    }
}