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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use amethyst::ecs::world::Index;
use super::super::{Rect, Vector};
use super::CollisionRect;
use crate::components::solid::SolidTag;
/// A collection of `CollisionRect`, can perform collision detection.
pub struct CollisionGrid<STag, T>
where
STag: SolidTag,
{
pub rects: Vec<CollisionRect<STag, T>>,
}
impl<STag, T> CollisionGrid<STag, T>
where
STag: SolidTag,
{
/// Create a new `CollisionGrid` by passing in a vector of `CollisionRect`s.
pub fn new(rects: Vec<CollisionRect<STag, T>>) -> Self {
Self { rects }
}
/// Returns a new `CollisionGrid` with no `CollisionRect`s.
pub fn empty() -> Self {
Self { rects: Vec::new() }
}
/// Adds a new `CollisionRect` to the grid.
pub fn push(&mut self, rect: CollisionRect<STag, T>) {
self.rects.push(rect);
}
/// Clears all `CollisionRect`s from the `rects` field.
pub fn clear(&mut self) {
self.rects.clear();
}
/// Get a stored `CollisionRect` by its entity ID.
pub fn rect_by_id(&self, id: Index) -> Option<&CollisionRect<STag, T>> {
self.rects.iter().find(|rect| {
if let Some(other_id) = rect.id {
id == other_id
} else {
false
}
})
}
/// Returns `true` if the passed `CollisionRect` is colliding with any other
/// `CollisionRect` stored in this `CollisionGrid`.
pub fn collides_any(&self, target_rect: &CollisionRect<STag, T>) -> bool {
self.rects
.iter()
.any(|rect| Self::do_rects_collide(&target_rect, rect))
}
/// Returns a vector of all `CollisionRect`s, that are in collision
/// with the passed `CollisionRect` (which may or may not exist in this `CollisionGrid`).
pub fn colliding_with(
&self,
target_rect: &CollisionRect<STag, T>,
) -> Vec<&CollisionRect<STag, T>> {
self.rects
.iter()
.filter(|rect| Self::do_rects_collide(&target_rect, rect))
.collect()
}
/// Similar to the `colliding_with` method, but instead of passing
/// a `CollisionRect` (which may or may not exist in this `CollisionGrid`),
/// you pass in an entity ID to a `CollisionRect` which is stored inside this `CollisionGrid`.
/// Note that, if you pass in an ID, which does not exist as a `CollisionRect` in this
/// `CollisionGrid`, then you will simply receive an empty vector.
pub fn colliding_with_id(&self, id: Index) -> Vec<&CollisionRect<STag, T>> {
if let Some(target_rect) = self.rect_by_id(id) {
self.colliding_with(target_rect)
} else {
Vec::new()
}
}
/// Returns `true` if the two passed `CollisionRect`s are in collision;
/// also checks, that their entity IDs are not the same,
/// and that their tags allow them to collide with each other.
/// TODO: Maybe make this a standalone function, not associated with the `CollisionGrid` struct?
pub fn do_rects_collide<U, V>(
rect_one: &CollisionRect<STag, U>,
rect_two: &CollisionRect<STag, V>,
) -> bool {
!Self::do_rect_ids_match(&rect_one.id, &rect_two.id)
&& Self::do_rect_tags_match(&rect_one.tag, &rect_two.tag)
&& Self::do_rects_intersect(&rect_one.rect, &rect_two.rect)
}
/// Returns `true` if the two passed `Option<Index>` CollisionRect IDs are equal.
/// Both arguments need to be `Some` for `true` to be returned;
/// if any of the arguments is `None`, then `false` is returned.
pub fn do_rect_ids_match(
id_one_opt: &Option<Index>,
id_two_opt: &Option<Index>,
) -> bool {
// TODO: I'm pretty sure that I can replace this logic with a pure equality check.
if let (Some(id_one), Some(id_two)) = (id_one_opt, id_two_opt) {
id_one == id_two
} else {
false
}
}
/// Returns `true` if the two passed `Option<STag>` Solid Tags may collide with each other.
/// Returns `true` if any of the passed arguments is `None`.
pub fn do_rect_tags_match(
tag_one_opt: &Option<STag>,
tag_two_opt: &Option<STag>,
) -> bool {
if let (Some(tag_one), Some(tag_two)) = (tag_one_opt, tag_two_opt) {
tag_one.collides_with(tag_two)
} else {
true
}
}
/// Returns `true` if the two passed `Rect`s intersect with each other.
#[rustfmt::skip]
pub fn do_rects_intersect(rect_one: &Rect, rect_two: &Rect) -> bool {
(
(
rect_one.left >= rect_two.left
&& rect_one.left < rect_two.right
) || (
rect_one.left <= rect_two.left
&& rect_one.right > rect_two.left
)
) && (
(
rect_one.top <= rect_two.top
&& rect_one.top > rect_two.bottom
) || (
rect_one.top >= rect_two.top
&& rect_one.bottom < rect_two.top
)
)
}
}
impl<STag, T> From<Vec<(Index, Vector, Option<Vector>)>>
for CollisionGrid<STag, T>
where
STag: SolidTag,
{
fn from(data: Vec<(Index, Vector, Option<Vector>)>) -> Self {
Self::new(
data.iter()
.map(|&rect_data| CollisionRect::from(rect_data))
.collect(),
)
}
}
impl<STag, T> From<Vec<(Index, Vector, Option<Vector>, STag, Option<T>)>>
for CollisionGrid<STag, T>
where
STag: SolidTag,
T: Clone,
{
fn from(
data: Vec<(Index, Vector, Option<Vector>, STag, Option<T>)>,
) -> Self {
Self::new(
data.into_iter()
.map(|rect_data| CollisionRect::from(rect_data))
.collect(),
)
}
}