Skip to main content

euv_core/game/ecs/
struct.rs

1use super::*;
2
3/// Generation-checked O(1) handle referencing an entity inside `GameWorld`.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
5pub struct EntityId {
6    /// Dense slot index in the entity arena.
7    #[get(type(copy))]
8    pub(crate) index: u32,
9    /// Generation counter rejecting stale handles after despawn and reuse.
10    #[get(type(copy))]
11    pub(crate) generation: u32,
12}
13
14/// 2D spatial transform component in world coordinates.
15#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
16pub struct EntityTransform {
17    /// World-space center position.
18    #[get(type(copy))]
19    pub(crate) position: Point,
20    /// Orientation angle in radians (`0.0` points right along `+X`).
21    #[get(type(copy))]
22    pub(crate) rotation_rad: f32,
23    /// Non-uniform scale factors `(sx, sy)`.
24    #[get(type(copy))]
25    pub(crate) scale: Size,
26}
27
28/// Linear and angular velocity component in world units per second.
29#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
30pub struct Velocity2D {
31    /// Horizontal velocity in world units per second.
32    #[get(type(copy))]
33    pub(crate) vx: f32,
34    /// Vertical velocity in world units per second.
35    #[get(type(copy))]
36    pub(crate) vy: f32,
37    /// Angular spin velocity in radians per second.
38    #[get(type(copy))]
39    pub(crate) angular: f32,
40}
41
42/// Rigid-body dynamics parameters for 2D physics simulation.
43#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
44pub struct RigidBody2D {
45    /// Body dynamics category (`Static`, `Dynamic`, or `Kinematic`).
46    #[get(type(copy))]
47    pub(crate) kind: BodyKind,
48    /// Inverse mass (`0.0` for infinite-mass static/kinematic bodies).
49    #[get(type(copy))]
50    pub(crate) inv_mass: f32,
51    /// Coefficient of restitution (`0.0` inelastic .. `1.0` perfectly elastic bounce).
52    #[get(type(copy))]
53    pub(crate) restitution: f32,
54    /// Linear drag damping factor per second.
55    #[get(type(copy))]
56    pub(crate) linear_drag: f32,
57    /// Multiplier applied to global world gravity.
58    #[get(type(copy))]
59    pub(crate) gravity_scale: f32,
60}
61
62/// Collision volume and bitmask filtering component.
63#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
64pub struct Collider2D {
65    /// Geometric collision shape.
66    #[get(type(copy))]
67    pub(crate) shape: ColliderShape,
68    /// Layer bitmask this collider belongs to.
69    #[get(type(copy))]
70    pub(crate) layer: u32,
71    /// Bitmask of layers this collider interacts with.
72    #[get(type(copy))]
73    pub(crate) mask: u32,
74    /// When `true`, emits overlap events without applying physical impulse separation.
75    #[get(type(copy))]
76    pub(crate) is_trigger: bool,
77}
78
79/// Visual appearance component rendered by `GameRenderContext::render_world`.
80#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
81pub struct Sprite2D {
82    /// Render primitive shape.
83    #[get(type(copy))]
84    pub(crate) shape: EntityVisualShape,
85    /// Logical world-space width and height.
86    #[get(type(copy))]
87    pub(crate) size: Size,
88    /// Primary fill or tint color.
89    #[get(type(copy))]
90    pub(crate) color: Color,
91    /// Optional accent outline or core glow color.
92    #[get(type(copy))]
93    pub(crate) accent: Color,
94    /// Draw layer ordering (lower values render behind higher values).
95    #[get(type(copy))]
96    pub(crate) z_order: i32,
97    /// Corner radius for `EntityVisualShape::Rect`.
98    #[get(type(copy))]
99    pub(crate) corner_radius: f32,
100    /// Visibility toggle.
101    #[get(type(copy))]
102    pub(crate) visible: bool,
103}
104
105/// Complete entity record stored in the contiguous `GameWorld` arena.
106#[derive(Clone, Data, Debug, PartialEq)]
107pub struct EntityRecord {
108    /// Generational handle matching this slot.
109    #[get(type(copy))]
110    pub(crate) id: EntityId,
111    /// Whether the entity slot is currently active.
112    #[get(type(copy))]
113    pub(crate) alive: bool,
114    /// Gameplay tag category.
115    #[get(type(copy))]
116    pub(crate) tag: EntityTag,
117    /// Spatial transform.
118    #[get(type(copy))]
119    pub(crate) transform: EntityTransform,
120    /// Linear and angular velocity.
121    #[get(type(copy))]
122    pub(crate) velocity: Velocity2D,
123    /// Optional rigid-body physics dynamics.
124    #[get(skip)]
125    pub(crate) body: Option<RigidBody2D>,
126    /// Optional collision volume.
127    #[get(skip)]
128    pub(crate) collider: Option<Collider2D>,
129    /// Optional visual sprite component.
130    #[get(skip)]
131    pub(crate) sprite: Option<Sprite2D>,
132    /// Current hit points for combat/destructible entities.
133    #[get(type(copy))]
134    pub(crate) health: f32,
135    /// Maximum hit points.
136    #[get(type(copy))]
137    pub(crate) max_health: f32,
138    /// Remaining lifetime in seconds (`None` means immortal).
139    #[get(skip)]
140    pub(crate) ttl_secs: Option<f32>,
141    /// Optional user-defined state payload integer (e.g., AI phase or score value).
142    #[get(type(copy))]
143    pub(crate) user_data: u64,
144    /// Discontinuity epoch incremented on teleports so snapshot interpolation snaps instead of sweeping.
145    #[get(type(copy))]
146    pub(crate) teleport_epoch: u32,
147    /// Simulation tick on which this entity was spawned so fresh spawns are never interpolated.
148    #[get(type(copy))]
149    pub(crate) spawn_tick: u64,
150}
151
152/// Cold entity lifecycle and gameplay metadata split out of hot transform/velocity SoA streams.
153#[derive(Clone, Copy, Data, Debug, PartialEq)]
154pub struct EntityColdMeta {
155    /// Generational handle matching this slot.
156    #[get(type(copy))]
157    pub(crate) id: EntityId,
158    /// Whether the entity slot is currently active.
159    #[get(type(copy))]
160    pub(crate) alive: bool,
161    /// Gameplay tag category.
162    #[get(type(copy))]
163    pub(crate) tag: EntityTag,
164    /// Current hit points for combat/destructible entities.
165    #[get(type(copy))]
166    pub(crate) health: f32,
167    /// Maximum hit points.
168    #[get(type(copy))]
169    pub(crate) max_health: f32,
170    /// Remaining lifetime in seconds (`None` means immortal).
171    #[get(skip)]
172    pub(crate) ttl_secs: Option<f32>,
173    /// Optional user-defined state payload integer (e.g., AI phase or score value).
174    #[get(type(copy))]
175    pub(crate) user_data: u64,
176    /// Discontinuity epoch incremented on teleports so snapshot interpolation snaps instead of sweeping.
177    #[get(type(copy))]
178    pub(crate) teleport_epoch: u32,
179    /// Simulation tick on which this entity was spawned so fresh spawns are never interpolated.
180    #[get(type(copy))]
181    pub(crate) spawn_tick: u64,
182}
183
184/// High-performance generational ECS arena managing game entities with O(1) spawn, despawn, and Structure-of-Arrays hot streams.
185#[derive(Clone, Data, Debug, Default)]
186pub struct GameWorld {
187    /// Contiguous slot array of entity records kept synchronized with SoA hot arrays.
188    pub(crate) entities: Vec<EntityRecord>,
189    /// Structure-of-Arrays dense hot transform stream indexed identically to `entities`.
190    pub(crate) transforms: Vec<EntityTransform>,
191    /// Structure-of-Arrays dense hot velocity stream indexed identically to `entities`.
192    pub(crate) velocities: Vec<Velocity2D>,
193    /// Structure-of-Arrays dense cold lifecycle/tag metadata stream indexed identically to `entities`.
194    pub(crate) cold_meta: Vec<EntityColdMeta>,
195    /// Free slot index stack for O(1) reuse without reallocation.
196    #[get(pub(crate))]
197    #[get_mut(pub(crate))]
198    #[set(pub(crate))]
199    pub(crate) free_list: Vec<u32>,
200    /// Cached count of live entities.
201    #[get(type(copy))]
202    pub(crate) live_count: usize,
203    /// Monotonic world tick counter.
204    #[get(type(copy))]
205    pub(crate) tick: u64,
206}