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}
145
146/// High-performance generational ECS arena managing game entities with O(1) spawn, despawn, and cache-friendly iteration.
147#[derive(Clone, Data, Debug, Default)]
148pub struct GameWorld {
149    /// Contiguous slot array of entity records.
150    pub(crate) entities: Vec<EntityRecord>,
151    /// Free slot index stack for O(1) reuse without reallocation.
152    #[get(pub(crate))]
153    #[get_mut(pub(crate))]
154    #[set(pub(crate))]
155    pub(crate) free_list: Vec<u32>,
156    /// Cached count of live entities.
157    #[get(type(copy))]
158    pub(crate) live_count: usize,
159    /// Monotonic world tick counter.
160    #[get(type(copy))]
161    pub(crate) tick: u64,
162}