tx2_core/
entity.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4pub type EntityId = u32;
5
6static mut NEXT_ENTITY_ID: u32 = 1;
7
8pub fn create_entity_id() -> EntityId {
9    unsafe {
10        let id = NEXT_ENTITY_ID;
11        NEXT_ENTITY_ID += 1;
12        id
13    }
14}
15
16pub fn reset_entity_id_counter(start: u32) {
17    unsafe {
18        NEXT_ENTITY_ID = start;
19    }
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
23pub struct Entity {
24    pub id: EntityId,
25}
26
27impl Entity {
28    pub fn new() -> Self {
29        Self {
30            id: create_entity_id(),
31        }
32    }
33
34    pub fn with_id(id: EntityId) -> Self {
35        Self { id }
36    }
37}
38
39impl fmt::Display for Entity {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "Entity({})", self.id)
42    }
43}