wecs_core/storage/
table.rs

1use std::{
2    any::{Any, TypeId},
3    collections::HashMap,
4    ops::Index,
5    pin::Pin,
6};
7
8use crate::{
9    component::{Component, ComponentId, ComponentList},
10    entity::EntityId,
11};
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct TableId(usize);
15
16impl TableId {
17    pub const UNINIT: TableId = TableId(usize::MAX);
18
19    pub fn is_init(&self) -> bool {
20        self.0 != TableId::UNINIT.0
21    }
22}
23
24#[derive(Debug)]
25pub struct Tables {
26    tables: Vec<Table>,
27}
28
29impl Default for Tables {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Tables {
36    pub fn new() -> Self {
37        Self { tables: Vec::new() }
38    }
39
40    pub fn init<CL>(&mut self) -> TableId
41    where
42        CL: ComponentList,
43    {
44        let index = self.tables.len();
45        let mut table = Table::new();
46        table.init_archetype_columns(CL::get_ids());
47        self.tables.insert(index, table);
48
49        TableId(index)
50    }
51
52    pub fn get(&self, table_id: &TableId) -> &Table {
53        self.tables.get(table_id.0).expect("")
54    }
55
56    pub fn get_mut(&mut self, table_id: &TableId) -> &mut Table {
57        self.tables.get_mut(table_id.0).expect("")
58    }
59}
60
61#[derive(Debug)]
62pub struct Table {
63    columns: HashMap<ComponentId, Column>,
64    entities: Vec<EntityId>,
65}
66
67impl Default for Table {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73impl Table {
74    pub fn new() -> Self {
75        Self {
76            columns: HashMap::new(),
77            entities: Vec::new(),
78        }
79    }
80
81    fn init_archetype_columns(&mut self, component_ids: Vec<ComponentId>) {
82        for ele in component_ids {
83            self.columns.insert(ele, Column::new());
84        }
85    }
86
87    pub fn insert_components(
88        &mut self,
89        entity_id: EntityId,
90        component_ids: Vec<ComponentId>,
91        component_values: Vec<Box<dyn Any>>,
92    ) {
93        let index = self.entities.len();
94        self.entities.insert(index, entity_id);
95        for (component_id, component_value) in component_ids.iter().zip(component_values) {
96            self.columns
97                .get_mut(component_id)
98                .expect("")
99                .data
100                .insert(index, component_value);
101        }
102    }
103
104    pub(crate) fn get_entity_index(&self, entity_id: &EntityId) -> usize {
105        self.entities
106            .iter()
107            .enumerate()
108            .find(|(_, id)| *id == entity_id)
109            .expect("")
110            .0
111    }
112
113    pub(crate) fn get_component_at<C>(&self, index: usize) -> &C
114    where
115        C: Component + 'static,
116    {
117        self.columns
118            .get(&TypeId::of::<C>())
119            .expect("")
120            .data
121            .get(index)
122            .expect("")
123            .downcast_ref::<C>()
124            .expect("")
125    }
126
127    pub(crate) fn get_component_at_mut<C>(&mut self, index: usize) -> &mut C
128    where
129        C: Component + 'static,
130    {
131        self.columns
132            .get_mut(&TypeId::of::<C>())
133            .expect("")
134            .data
135            .get_mut(index)
136            .expect("")
137            .downcast_mut::<C>()
138            .expect("")
139    }
140}
141
142#[derive(Debug)]
143pub struct Column {
144    data: Vec<Box<dyn Any>>,
145}
146
147impl Default for Column {
148    fn default() -> Self {
149        Self::new()
150    }
151}
152
153impl Column {
154    pub fn new() -> Self {
155        Self { data: Vec::new() }
156    }
157}