tx2_core/
world.rs

1use std::collections::HashMap;
2use crate::entity::{Entity, EntityId, create_entity_id};
3use crate::component::{Component, ComponentStore, ComponentId};
4use crate::query::{Query, QueryBuilder, QueryCache, QueryDescriptor};
5use serde::{Serialize, Deserialize};
6
7pub struct World {
8    pub(crate) entities: HashMap<EntityId, Entity>,
9    pub(crate) component_store: ComponentStore,
10    pub(crate) query_cache: QueryCache,
11    time: f64,
12    fixed_time: f64,
13    accumulator: f64,
14    running: bool,
15    paused: bool,
16}
17
18impl World {
19    pub fn new() -> Self {
20        Self {
21            entities: HashMap::new(),
22            component_store: ComponentStore::new(),
23            query_cache: QueryCache::new(),
24            time: 0.0,
25            fixed_time: 0.0,
26            accumulator: 0.0,
27            running: false,
28            paused: false,
29        }
30    }
31
32    pub fn create_entity(&mut self) -> Entity {
33        let entity = Entity::new();
34        self.entities.insert(entity.id, entity);
35        self.query_cache.mark_all_dirty();
36        entity
37    }
38
39    pub fn create_entity_with_id(&mut self, id: EntityId) -> Entity {
40        if self.entities.contains_key(&id) {
41            panic!("Entity with id {} already exists", id);
42        }
43        let entity = Entity::with_id(id);
44        self.entities.insert(id, entity);
45        self.query_cache.mark_all_dirty();
46        entity
47    }
48
49    pub fn destroy_entity(&mut self, entity_id: EntityId) -> bool {
50        if self.entities.remove(&entity_id).is_some() {
51            self.component_store.remove_all_components(entity_id);
52            self.query_cache.mark_all_dirty();
53            return true;
54        }
55        false
56    }
57
58    pub fn get_entity(&self, entity_id: EntityId) -> Option<&Entity> {
59        self.entities.get(&entity_id)
60    }
61
62    pub fn has_entity(&self, entity_id: EntityId) -> bool {
63        self.entities.contains_key(&entity_id)
64    }
65
66    pub fn get_all_entities(&self) -> Vec<&Entity> {
67        self.entities.values().collect()
68    }
69
70    pub fn add_component(&mut self, entity_id: EntityId, component: Box<dyn Component>) {
71        if !self.entities.contains_key(&entity_id) {
72            panic!("Entity {} does not exist", entity_id);
73        }
74        let component_id = component.component_id();
75        self.component_store.add(entity_id, component);
76        self.query_cache.mark_dirty_for_component(&component_id);
77    }
78
79    pub fn remove_component(&mut self, entity_id: EntityId, component_id: &str) -> bool {
80        let removed = self.component_store.remove(entity_id, component_id);
81        if removed {
82            self.query_cache.mark_dirty_for_component(component_id);
83        }
84        removed
85    }
86
87    pub fn get_component<T: Component>(&self, entity_id: EntityId) -> Option<&T> {
88        self.component_store.get::<T>(entity_id)
89    }
90
91    pub fn get_all_components(&self, entity_id: EntityId) -> Vec<&Box<dyn Component>> {
92        self.component_store.get_all(entity_id)
93    }
94
95    pub fn has_component(&self, entity_id: EntityId, component_id: &str) -> bool {
96        self.component_store.has(entity_id, component_id)
97    }
98
99    pub fn query(&mut self, descriptor: QueryDescriptor) -> std::collections::HashSet<EntityId> {
100        let query = self.query_cache.get(descriptor);
101        query.execute(&self.component_store)
102    }
103
104    pub fn query_builder(&self) -> QueryBuilder {
105        QueryBuilder::new()
106    }
107
108    pub fn clear(&mut self) {
109        self.entities.clear();
110        self.component_store.clear();
111        self.query_cache.clear();
112    }
113}