ecs_lib_rs/
lib.rs

1mod custom_errors;
2mod entities;
3mod resources;
4
5use crate::entities::query::Query;
6use crate::entities::Entities;
7use crate::resources::Resources;
8use eyre::Result;
9use std::any::Any;
10
11#[derive(Default)]
12pub struct World {
13    resources: Resources,
14    entities: Entities,
15}
16
17impl World {
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Add a resource.
23    /// ```
24    /// use ecs_lib_rs::World;
25    /// let mut world = World::new();
26    /// world.add_resource(1_u32);
27    /// assert_eq!(world.get_resource::<u32>(), Some(&1));
28    /// ```
29    pub fn add_resource(&mut self, resource: impl Any) {
30        self.resources.add(resource)
31    }
32
33    /// Query for a resource and get a reference to it. The type of the resource must be added in so that it can find it.
34    /// ```
35    /// use ecs_lib_rs::World;
36    /// let mut world = World::new();
37    /// assert_eq!(world.get_resource::<u32>(), None);
38    /// world.add_resource(1_u32);
39    /// assert_eq!(world.get_resource::<u32>(), Some(&1));
40    /// ```
41    pub fn get_resource<T: Any>(&self) -> Option<&T> {
42        self.resources.get_ref::<T>()
43    }
44
45    /// Query for a resource and get a mutable reference to it. The type of the resource must be added in so that it can find it.
46    /// ```
47    /// use ecs_lib_rs::World;
48    /// let mut world = World::new();
49    /// assert_eq!(world.get_resource_mut::<u32>(), None);
50    /// world.add_resource(1_u32);
51    /// {
52    ///     let x = world.get_resource_mut::<u32>();
53    ///     assert_eq!(x, Some(&mut 1_u32));
54    ///     let x = x.unwrap();
55    ///     *x += 1;
56    /// }
57    /// assert_eq!(world.get_resource::<u32>(), Some(&2));
58    /// ```
59    pub fn get_resource_mut<T: Any>(&mut self) -> Option<&mut T> {
60        self.resources.get_mut::<T>()
61    }
62
63    /// Removes the resource from the world. Returns `None` if the resource wasn't present and hence was not deleted.
64    /// Otherwise, it returns `Some(data)`
65    /// ```   
66    /// use ecs_lib_rs::World;
67    /// let mut world = World::new();
68    /// world.add_resource(1_u32);
69    /// world.remove_resource::<u32>();
70    /// assert_eq!(world.get_resource::<u32>(), None);
71    /// ```
72    pub fn remove_resource<T: Any>(&mut self) -> Option<Box<dyn Any>> {
73        self.resources.remove::<T>()
74    }
75
76    /// Register a component. The type of the resource must be added in so that it can find it.
77    /// ```
78    /// use ecs_lib_rs::World;
79    /// let mut world = World::new();
80    /// world.register_component::<u32>();
81    ///
82    /// ```
83    pub fn register_component<T: Any>(&mut self) {
84        self.entities.register_component::<T>()
85    }
86
87    pub fn create_entity(&mut self) -> &mut Entities {
88        self.entities.create_entity()
89    }
90
91    pub fn query(&self) -> Query {
92        Query::new(&self.entities)
93    }
94
95    pub fn delete_component_by_entity_id<T: Any>(&mut self, id: usize) -> Result<()> {
96        self.entities.delete_component_by_entity_id::<T>(id)
97    }
98
99    pub fn add_component_to_entity_by_id(&mut self, id: usize, component: impl Any) -> Result<()> {
100        self.entities.add_component_by_entity_id(id, component)
101    }
102
103    pub fn delete_entity_by_id(&mut self, id: usize) -> Result<()> {
104        self.entities.delete_by_id(id)
105    }
106}