1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
use glam::{IVec2, Vec2};
use rapier2d::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle},
geometry::{Collider, ColliderHandle},
};
use crate::{
config::CELLS_TO_METRES,
sim::{
cell::{cell::Cell, materials::MaterialId},
lib::marching_squares::Marchable,
rb_manager::RbManager,
sim_manager::SimManager,
},
};
pub mod entities;
pub struct EntityCells {
pub size: IVec2,
pub cells: Vec<Cell>,
}
impl EntityCells {
#[inline]
pub fn get_cell_at_local_position(&self, pos: IVec2) -> Cell {
self.cells[pos.x as usize + pos.y as usize * self.size.x as usize]
}
#[inline]
pub fn set_cell_at_local_position(&mut self, pos: IVec2, cell: Cell) {
self.cells[pos.x as usize + pos.y as usize * self.size.x as usize] = cell;
}
}
impl Marchable for EntityCells {
fn occupied(&self, pos: IVec2) -> bool {
if pos.x < 0 || pos.x >= self.size.x || pos.y < 0 || pos.y >= self.size.y {
false
} else {
self.get_cell_at_local_position(pos).material != MaterialId::Void
}
}
fn size(&self) -> IVec2 {
self.size
}
}
pub trait EntityBehaviour {
fn update(&mut self, sim: &mut SimManager, delta_time: f32) -> ();
fn physics_update(&mut self, sim: &mut SimManager, delta_time: f32) -> ();
}
pub struct EntityDef {
pub position: Vec2,
pub rb: Option<RigidBody>,
pub collider: Option<Collider>,
pub cells: Option<EntityCells>,
pub behaviour: Option<Box<dyn EntityBehaviour>>,
}
impl EntityDef {
pub fn from_cells(
position: Vec2,
cells: EntityCells,
behaviour: Option<Box<dyn EntityBehaviour>>,
) -> Self {
let rb = RigidBodyBuilder::dynamic()
.translation(position / CELLS_TO_METRES)
.build();
let collider = RbManager::convex_hull_collider_from_marchable(&cells, None).build();
EntityDef {
position,
rb: Some(rb),
collider: Some(collider),
cells: Some(cells),
behaviour,
}
}
}
pub struct Entity {
pub id: u32,
pub rb_h: Option<RigidBodyHandle>,
pub collider_h: Option<ColliderHandle>,
pub cells: Option<EntityCells>,
behaviour: Option<Box<dyn EntityBehaviour>>,
}
impl Entity {
pub fn transform(&self, sim: &SimManager) -> Option<(Vec2, (f32, f32))> {
self.rb_h
.map(|rb_h| {
sim.rb_manager
.physics_manager
.rigid_body_set
.get(rb_h)
.map(|rb| {
(
rb.translation() * CELLS_TO_METRES,
(rb.rotation().cos(), rb.rotation().sin()),
)
})
})
.flatten()
}
// TODO use drop?
pub fn destroy(&mut self, sim: &mut SimManager) -> () {}
pub fn update(&mut self, sim: &mut SimManager, delta_time: f32) -> () {
if let Some(behaviour) = &mut self.behaviour {
behaviour.update(sim, delta_time);
}
}
pub fn physics_update(&mut self, sim: &mut SimManager, delta_time: f32) -> () {
if let Some(behaviour) = &mut self.behaviour {
behaviour.physics_update(sim, delta_time);
}
}
pub fn new(
id: u32,
rb_h: Option<RigidBodyHandle>,
collider_h: Option<ColliderHandle>,
cells: Option<EntityCells>,
behaviour: Option<Box<dyn EntityBehaviour>>,
) -> Self {
Entity {
id,
rb_h,
collider_h,
cells,
behaviour,
}
}
}
|