-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtilemap_collisions.cpp
More file actions
67 lines (57 loc) · 2.05 KB
/
Copy pathtilemap_collisions.cpp
File metadata and controls
67 lines (57 loc) · 2.05 KB
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
#include "tilemap_collisions.hpp"
#include <algorithm>
namespace jt {
void TilemapCollisions::add(jt::Rectf const& r) { m_rects.push_back(r); }
std::vector<jt::Rectf> const& TilemapCollisions::getRects() const { return m_rects; }
void TilemapCollisions::refineColliders()
{
while (true) {
auto changes = refineCollidersOneStep();
if (!changes) {
return;
}
}
}
bool TilemapCollisions::refineCollidersOneStep()
{
std::vector<jt::Rectf> rects;
// Note if this becomes a performance bottleneck, make treated a set instead of a vector.
std::vector<jt::Rectf> treated;
for (auto it1 = m_rects.cbegin(); it1 != m_rects.cend(); ++it1) {
if (std::find(treated.cbegin(), treated.cend(), *it1) != treated.cend()) {
continue;
}
for (auto it2 = it1; it2 != m_rects.cend(); ++it2) {
if (it1 == it2) {
continue;
}
if (std::find(treated.cbegin(), treated.cend(), *it2) != treated.cend()) {
continue;
}
auto& r1 = *it1;
auto& r2 = *it2;
if (r1.width == r2.width && r1.left == r2.left && r2.top == r1.top + r1.height) {
treated.push_back(r1);
treated.push_back(r2);
rects.push_back(jt::Rectf { r1.left, r1.top, r1.width, r1.height + r2.height });
break;
}
if (r1.height == r2.height && r1.top == r2.top && r2.left == r1.left + r1.width) {
treated.push_back(r1);
treated.push_back(r2);
rects.push_back(jt::Rectf { r1.left, r1.top, r1.width + r2.width, r1.height });
break;
}
}
}
for (auto it1 = m_rects.cbegin(); it1 != m_rects.cend(); ++it1) {
if (std::find(treated.cbegin(), treated.cend(), *it1) != treated.cend()) {
continue;
}
rects.push_back(*it1);
}
bool const retval = (m_rects.size() != rects.size());
m_rects = rects;
return retval;
}
} // namespace jt