use super::*;
impl Default for EntityTransform {
fn default() -> Self {
Self {
position: Point::new(0.0, 0.0),
rotation_rad: 0.0,
scale: Size::new(1.0, 1.0),
}
}
}
impl EntityTransform {
#[must_use]
pub fn lerp(self, other: Self, alpha: f32) -> Self {
let k: f32 = alpha.clamp(0.0, 1.0);
let p0: Point = self.get_position();
let p1: Point = other.get_position();
let s0: Size = self.get_scale();
let s1: Size = other.get_scale();
let raw_delta: f32 = (other.get_rotation_rad() - self.get_rotation_rad()).rem_euclid(TAU);
let shortest_delta: f32 = if raw_delta > PI {
raw_delta - TAU
} else {
raw_delta
};
Self {
position: Point::new(
p0.get_x() + (p1.get_x() - p0.get_x()) * k,
p0.get_y() + (p1.get_y() - p0.get_y()) * k,
),
rotation_rad: self.get_rotation_rad() + shortest_delta * k,
scale: Size::new(
s0.get_width() + (s1.get_width() - s0.get_width()) * k,
s0.get_height() + (s1.get_height() - s0.get_height()) * k,
),
}
}
}
impl RigidBody2D {
#[must_use]
pub fn dynamic(mass: f32, restitution: f32, linear_drag: f32) -> Self {
Self {
kind: BodyKind::Dynamic,
inv_mass: 1.0 / mass.max(0.001),
restitution: restitution.clamp(0.0, 1.0),
linear_drag: linear_drag.max(0.0),
gravity_scale: 1.0,
}
}
#[must_use]
pub fn static_body(restitution: f32) -> Self {
Self {
kind: BodyKind::Static,
inv_mass: 0.0,
restitution: restitution.clamp(0.0, 1.0),
linear_drag: 0.0,
gravity_scale: 0.0,
}
}
#[must_use]
pub fn kinematic() -> Self {
Self {
kind: BodyKind::Kinematic,
inv_mass: 0.0,
restitution: 0.5,
linear_drag: 0.0,
gravity_scale: 0.0,
}
}
}
impl Collider2D {
#[must_use]
pub fn circle(radius: f32, layer: u32, mask: u32, is_trigger: bool) -> Self {
Self {
shape: ColliderShape::Circle {
radius: radius.max(0.5),
},
layer,
mask,
is_trigger,
}
}
#[must_use]
pub fn aabb(width: f32, height: f32, layer: u32, mask: u32, is_trigger: bool) -> Self {
Self {
shape: ColliderShape::Aabb {
width: width.max(1.0),
height: height.max(1.0),
},
layer,
mask,
is_trigger,
}
}
#[must_use]
pub fn world_aabb(&self, center: Point) -> Rect {
match self.get_shape() {
ColliderShape::Aabb { width, height } => Rect::new(
center.get_x() - width * 0.5,
center.get_y() - height * 0.5,
width,
height,
),
ColliderShape::Circle { radius } => Rect::new(
center.get_x() - radius,
center.get_y() - radius,
radius * 2.0,
radius * 2.0,
),
}
}
}
impl EntityRecord {
#[must_use]
pub fn body_component(&self) -> Option<RigidBody2D> {
let Self { body, .. } = self;
*body
}
#[must_use]
pub fn collider_component(&self) -> Option<Collider2D> {
let Self { collider, .. } = self;
*collider
}
#[must_use]
pub fn sprite_component(&self) -> Option<Sprite2D> {
let Self { sprite, .. } = self;
*sprite
}
#[must_use]
pub fn ttl_value(&self) -> Option<f32> {
let Self { ttl_secs, .. } = self;
*ttl_secs
}
#[must_use]
pub fn world_bounds(&self) -> Rect {
let pos: Point = self.get_transform().get_position();
if let Some(sp) = self.sprite_component() {
let w: f32 = sp.get_size().get_width() * self.get_transform().get_scale().get_width();
let h: f32 = sp.get_size().get_height() * self.get_transform().get_scale().get_height();
Rect::new(pos.get_x() - w * 0.5, pos.get_y() - h * 0.5, w, h)
} else if let Some(col) = self.collider_component() {
col.world_aabb(pos)
} else {
Rect::new(pos.get_x(), pos.get_y(), 1.0, 1.0)
}
}
}
impl GameWorld {
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
entities: Vec::with_capacity(capacity),
free_list: Vec::with_capacity(capacity / 4),
live_count: 0,
tick: 0,
}
}
pub fn spawn(&mut self, tag: EntityTag, position: Point) -> EntityId {
let transform: EntityTransform = EntityTransform::new(position, 0.0, Size::new(1.0, 1.0));
let spawn_tick: u64 = self.get_tick();
let id: EntityId = if let Some(index) = self.get_mut_free_list().pop() {
let slot: &mut EntityRecord = &mut self.get_mut_entities()[index as usize];
let next_gen: u32 = slot.get_id().get_generation().wrapping_add(1).max(1);
let next_id: EntityId = EntityId::new(index, next_gen);
*slot = EntityRecord {
id: next_id,
alive: true,
tag,
transform,
velocity: Velocity2D::default(),
body: None,
collider: None,
sprite: None,
health: 100.0,
max_health: 100.0,
ttl_secs: None,
user_data: 0,
teleport_epoch: 0,
spawn_tick,
};
next_id
} else {
let index: u32 = self.get_entities().len() as u32;
let next_id: EntityId = EntityId::new(index, 1);
self.get_mut_entities().push(EntityRecord {
id: next_id,
alive: true,
tag,
transform,
velocity: Velocity2D::default(),
body: None,
collider: None,
sprite: None,
health: 100.0,
max_health: 100.0,
ttl_secs: None,
user_data: 0,
teleport_epoch: 0,
spawn_tick,
});
next_id
};
let next_live: usize = self.get_live_count().saturating_add(1);
self.set_live_count(next_live);
id
}
pub fn teleport_entity(&mut self, id: EntityId, position: Point) -> bool {
let Some(ent) = self.get_entity_mut(id) else {
return false;
};
let mut tr: EntityTransform = ent.get_transform();
tr.set_position(position);
ent.set_transform(tr);
let next_epoch: u32 = ent.get_teleport_epoch().wrapping_add(1);
ent.set_teleport_epoch(next_epoch);
true
}
pub fn despawn(&mut self, id: EntityId) -> bool {
let idx: usize = id.get_index() as usize;
let removed: bool = match self.get_mut_entities().get_mut(idx) {
Some(slot) if slot.get_alive() && slot.get_id() == id => {
slot.set_alive(false);
true
}
_ => false,
};
if removed {
self.get_mut_free_list().push(id.get_index());
let next_live: usize = self.get_live_count().saturating_sub(1);
self.set_live_count(next_live);
}
removed
}
#[must_use]
pub fn is_alive(&self, id: EntityId) -> bool {
self.get_entity(id).is_some()
}
#[must_use]
pub fn get_entity(&self, id: EntityId) -> Option<&EntityRecord> {
let slot: &EntityRecord = self.get_entities().get(id.get_index() as usize)?;
if slot.get_alive() && slot.get_id() == id {
Some(slot)
} else {
None
}
}
pub fn get_entity_mut(&mut self, id: EntityId) -> Option<&mut EntityRecord> {
let slot: &mut EntityRecord = self.get_mut_entities().get_mut(id.get_index() as usize)?;
if slot.get_alive() && slot.get_id() == id {
Some(slot)
} else {
None
}
}
pub fn tick_lifetimes(&mut self, dt_secs: f32) -> usize {
let Self {
entities,
free_list,
live_count,
tick,
..
} = self;
let mut removed: usize = 0;
for (idx, slot) in entities.iter_mut().enumerate() {
if !slot.get_alive() {
continue;
}
if let Some(rem) = slot.ttl_value() {
let next: f32 = rem - dt_secs;
if next <= 0.0 {
slot.set_alive(false);
free_list.push(idx as u32);
removed += 1;
} else {
slot.set_ttl_secs(Some(next));
}
}
}
*live_count = live_count.saturating_sub(removed);
*tick = tick.wrapping_add(1);
removed
}
pub fn clear(&mut self) {
self.get_mut_entities().clear();
self.get_mut_free_list().clear();
self.set_live_count(0);
}
#[must_use]
pub fn count_by_tag(&self, tag: EntityTag) -> usize {
self.get_entities()
.iter()
.filter(|e: &&EntityRecord| e.get_alive() && e.get_tag() == tag)
.count()
}
}