pico_ecs/
world.rs

1// ============================================================================
2// WORLD MODULE
3// ============================================================================
4// World manages entities and provides system execution
5
6#![allow(unused)]
7
8use heapless::Vec;
9use crate::camera::Camera;
10
11use super::{entity::Entity, MAX_ENTITIES};
12use super::component::Component;
13use super::component_storage::ComponentStorage;
14use super::query::{Query1, Query2, Query3, Query4, Query1Mut, Query1Mut1, Query1Mut2, Query1Mut3, Query2Mut1, Query2Mut, Query3Mut, Query4Mut};
15
16/// World - manages entities and provides system execution
17pub struct World {
18    /// Next entity ID to allocate
19    next_id: u8,
20    pub camera: Camera,
21    /// Active entities (up to MAX_ENTITIES)
22    active_entities: Vec<Entity, MAX_ENTITIES>,
23}
24
25impl World {
26    /// Create a new world
27    pub fn new() -> Self {
28        World {
29            next_id: 0,
30            camera: Camera { x: 0, y: 0, width: 0, height: 0 },
31            active_entities: Vec::new(),
32        }
33    }
34    
35    /// Create a new entity
36    pub fn create_entity(&mut self) -> Option<Entity> {
37        if self.active_entities.len() < MAX_ENTITIES {
38            let entity = Entity::new(self.next_id);
39            self.next_id = self.next_id.wrapping_add(1);
40            self.active_entities.push(entity).ok()?;
41            Some(entity)
42        } else {
43            None
44        }
45    }
46    
47    /// Destroy an entity (note: components must be manually removed)
48    pub fn destroy_entity(&mut self, entity: Entity) {
49        if let Some(pos) = self.active_entities.iter().position(|&e| e == entity) {
50            self.active_entities.swap_remove(pos);
51        }
52    }
53    
54    /// Get all active entities
55    pub fn entities(&self) -> &[Entity] {
56        &self.active_entities
57    }
58    
59    /// Get count of active entities
60    pub fn entity_count(&self) -> usize {
61        self.active_entities.len()
62    }
63    
64    /// Query entities with one component type
65    pub fn query<'a, T: Component>(&'a self, storage: &'a ComponentStorage<T>) -> Query1<'a, T> {
66        Query1::new(storage, &self.active_entities)
67    }
68    
69    /// Query entities with two component types
70    pub fn query2<'a, T1: Component, T2: Component>(
71        &'a self,
72        storage1: &'a ComponentStorage<T1>,
73        storage2: &'a ComponentStorage<T2>
74    ) -> Query2<'a, T1, T2> {
75        Query2::new(storage1, storage2, &self.active_entities)
76    }
77    
78    /// Query entities with three component types
79    pub fn query3<'a, T1: Component, T2: Component, T3: Component>(
80        &'a self,
81        storage1: &'a ComponentStorage<T1>,
82        storage2: &'a ComponentStorage<T2>,
83        storage3: &'a ComponentStorage<T3>
84    ) -> Query3<'a, T1, T2, T3> {
85        Query3::new(storage1, storage2, storage3, &self.active_entities)
86    }
87    
88    /// Query entities with four component types
89    pub fn query4<'a, T1: Component, T2: Component, T3: Component, T4: Component>(
90        &'a self,
91        storage1: &'a ComponentStorage<T1>,
92        storage2: &'a ComponentStorage<T2>,
93        storage3: &'a ComponentStorage<T3>,
94        storage4: &'a ComponentStorage<T4>
95    ) -> Query4<'a, T1, T2, T3, T4> {
96        Query4::new(storage1, storage2, storage3, storage4, &self.active_entities)
97    }
98    
99    /// Query entities with one mutable component
100    pub fn query_mut<'a, T: Component>(&'a self, storage: &'a mut ComponentStorage<T>) -> Query1Mut<'a, T> {
101        Query1Mut::new(storage, &self.active_entities)
102    }
103    
104    /// Query: 1 mutable, 1 immutable
105    pub fn query_1mut_1<'a, T1: Component, T2: Component>(
106        &'a self,
107        storage1: &'a mut ComponentStorage<T1>,
108        storage2: &'a ComponentStorage<T2>
109    ) -> Query1Mut1<'a, T1, T2> {
110        Query1Mut1::new(storage1, storage2, &self.active_entities)
111    }
112    
113    /// Query: 1 mutable, 2 immutable
114    pub fn query_1mut_2<'a, T1: Component, T2: Component, T3: Component>(
115        &'a self,
116        storage1: &'a mut ComponentStorage<T1>,
117        storage2: &'a ComponentStorage<T2>,
118        storage3: &'a ComponentStorage<T3>
119    ) -> Query1Mut2<'a, T1, T2, T3> {
120        Query1Mut2::new(storage1, storage2, storage3, &self.active_entities)
121    }
122    
123    /// Query: 1 mutable, 3 immutable
124    pub fn query_1mut_3<'a, T1: Component, T2: Component, T3: Component, T4: Component>(
125        &'a self,
126        storage1: &'a mut ComponentStorage<T1>,
127        storage2: &'a ComponentStorage<T2>,
128        storage3: &'a ComponentStorage<T3>,
129        storage4: &'a ComponentStorage<T4>
130    ) -> Query1Mut3<'a, T1, T2, T3, T4> {
131        Query1Mut3::new(storage1, storage2, storage3, storage4, &self.active_entities)
132    }
133    
134    /// Query: 2 mutable, 1 immutable
135    pub fn query_2mut_1<'a, T1: Component, T2: Component, T3: Component>(
136        &'a self,
137        storage1: &'a mut ComponentStorage<T1>,
138        storage2: &'a mut ComponentStorage<T2>,
139        storage3: &'a ComponentStorage<T3>
140    ) -> Query2Mut1<'a, T1, T2, T3> {
141        Query2Mut1::new(storage1, storage2, storage3, &self.active_entities)
142    }
143    
144    /// Query: 2 mutable components
145    pub fn query_2mut<'a, T1: Component, T2: Component>(
146        &'a self,
147        storage1: &'a mut ComponentStorage<T1>,
148        storage2: &'a mut ComponentStorage<T2>
149    ) -> Query2Mut<'a, T1, T2> {
150        Query2Mut::new(storage1, storage2, &self.active_entities)
151    }
152    
153    /// Query: 3 mutable components
154    pub fn query_3mut<'a, T1: Component, T2: Component, T3: Component>(
155        &'a self,
156        storage1: &'a mut ComponentStorage<T1>,
157        storage2: &'a mut ComponentStorage<T2>,
158        storage3: &'a mut ComponentStorage<T3>
159    ) -> Query3Mut<'a, T1, T2, T3> {
160        Query3Mut::new(storage1, storage2, storage3, &self.active_entities)
161    }
162    
163    /// Query: 4 mutable components
164    pub fn query_4mut<'a, T1: Component, T2: Component, T3: Component, T4: Component>(
165        &'a self,
166        storage1: &'a mut ComponentStorage<T1>,
167        storage2: &'a mut ComponentStorage<T2>,
168        storage3: &'a mut ComponentStorage<T3>,
169        storage4: &'a mut ComponentStorage<T4>
170    ) -> Query4Mut<'a, T1, T2, T3, T4> {
171        Query4Mut::new(storage1, storage2, storage3, storage4, &self.active_entities)
172    }
173}