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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::collections::HashMap;
use amethyst::ecs::world::Index;
use super::component_prelude::*;
use crate::geo::Side;
/// The different states of collision.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum State {
/// `Enter` means, this collision has _just_ occured in the previous frame.
Enter,
/// `Leave` means, these entities are _no longer in collision_, since the previous frame.
/// This collision entry will be removed in the next frame.
Leave,
/// `Steady` means, these entities have already been in collision in the previous frame,
/// and still are.
Steady,
/// `None` is a marker, which means this collision should be removed.
/// You should never be able to have a collision in this state, as it is removed
/// in the same frame as it is set to this state.
None,
}
impl State {
/// Returns `true` when state is `Enter`.
pub fn is_entering(&self) -> bool {
self == &State::Enter
}
/// Returns `true` when state is `Leaving`.
pub fn is_leaving(&self) -> bool {
self == &State::Leave
}
/// Returns `true` when state is `Steady`.
pub fn is_steady(&self) -> bool {
self == &State::Steady
}
}
/// Collision data. Holds information on in which _state_ of collision these entities are,
/// and from which _side_ they have collided. It does _not_ know which entities are colliding.
#[derive(Debug, Serialize, Deserialize)]
pub struct Data {
pub side: Side,
pub state: State,
set_collision_this_frame: bool,
}
impl Data {
fn should_remove(&self) -> bool {
if let State::None = self.state {
true
} else {
false
}
}
/// Set state of _NOT_ colliding entity to `Leave` if it was previously
/// in collision and not `Leave`, otherwise remove the entity from the HashMap.
#[rustfmt::skip]
pub fn unset(&mut self) {
match self.state {
State::Leave => self.state = State::None, // Stage for removal
_ => self.state = State::Leave,
}
}
}
/// Entities with `CheckCollision` perform collision detection against
/// all other entities with `Collision`, every frame.
/// Depending on if they are in collision, data will be set.
#[derive(Debug, Serialize, Deserialize)]
pub struct Collision {
pub(crate) collisions: HashMap<Index, Data>,
}
impl Collision {
pub fn new() -> Self {
Self {
collisions: HashMap::new(),
}
}
/// Returns `true` if in collision with _any_ other entity.
/// NOTE: I'm pretty sure this will also return `true` for leaving collisions (`State::Leave`).
/// Should probably return `false` if all collisions are leaving.
pub fn in_collision(&self) -> bool {
!self.collisions.is_empty()
}
/// Returns `Some` with the collision data, if this entity is in collision with another
/// entity with the given entity ID. Returns `None` when not in collision with given entity ID.
pub fn collision_with(&self, entity_id: Index) -> Option<&Data> {
self.collisions.get(&entity_id)
}
/// Returns `true` if this entity is in collision with the given entity ID,
/// and the collision is not _leaving_ (`State::Leave`).
#[rustfmt::skip]
pub fn in_collision_with(&self, entity_id: Index) -> bool {
if let Some(data) = self.collisions.get(&entity_id) {
match data.state {
State::Leave => false,
_ => true,
}
} else {
false
}
}
/// Is called when an entity is colliding with this entity.
pub(crate) fn set_collision_with(&mut self, entity_id: Index, side: Side) {
if let Some(data) = self.collisions.get_mut(&entity_id) {
// Set state of colliding entity to ...
data.state = match data.state {
// `Enter` if it was `Leave` previously
State::Leave => State::Enter,
// `Steady` if it was any other state previously
_ => State::Steady,
};
data.set_collision_this_frame = true;
} else {
self.collisions.insert(entity_id, Data {
side,
state: State::Enter,
set_collision_this_frame: true,
});
}
}
/// Should be called every time data changes.
/// This is handled by the appropriate system.
pub(crate) fn update(&mut self) {
let mut to_remove = Vec::new();
for (&id, collision) in self.collisions.iter_mut() {
if collision.set_collision_this_frame {
// Entity collision data was modified this frame, stage for deletion next frame
collision.set_collision_this_frame = false;
} else {
// Entity collision data was NOT modified this frame, set State to `Leave` or remove
// self.unset_collision_with_entry((id, collision));
collision.unset();
if collision.should_remove() {
to_remove.push(id);
}
}
}
for id in to_remove {
self.collisions.remove(&id);
}
}
}
impl Component for Collision {
type Storage = DenseVecStorage<Self>;
}