forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.cpp
185 lines (151 loc) · 4.26 KB
/
Grid.cpp
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
177
178
179
180
181
182
183
184
185
#include "Grid.hpp"
bool Grid::in_board(std::size_t id) const
{
return std::find(nodes_.begin(), nodes_.end(), id) != nodes_.end();
}
const std::set<std::size_t>& Grid::get_freed() const
{
return freed_;
}
const std::set<std::size_t>& Grid::get_unfreed() const
{
return unfreed_;
}
void Grid::clear_freed()
{
freed_.clear();
}
void Grid::clear_unfreed()
{
unfreed_.clear();
}
std::size_t Grid::add_node(EntitySystem& ents, Ogre::Vector2 pos)
{
if(nodes_.size() < width_ * height_)
{
auto id = ents.create_entity();
ents.add_component<GridNodeComponent>(id);
ents.add_component<PhysicsComponent>(id);
PhysicsHelper::set_2d_position(ents, id, pos);
nodes_.push_back(id);
return id;
}
else
return Component::NO_ENTITY;
}
void Grid::add_freed(std::size_t id)
{
if(in_board(id))
{
freed_.insert(id);
free_nodes_.emplace_back(id);
}
}
void Grid::add_unfreed(std::size_t id)
{
if(in_board(id))
{
unfreed_.insert(id);
std::remove(free_nodes_.begin(), free_nodes_.end(), id);
}
}
void Grid::remove_node(std::size_t id)
{
std::remove(nodes_.begin(), nodes_.end(), id);
}
std::size_t Grid::get_node(std::size_t x, std::size_t y) const
{
if(x < width_ && y < height_ && (x + y * width_) < nodes_.size())
return nodes_[x + y * width_];
else
return Component::NO_ENTITY;
}
std::size_t Grid::get_node_from_position(Ogre::Real x, Ogre::Real y) const
{
x = (x - start_.x) / distance_;
y = (y - start_.y) / distance_;
std::size_t res_x = (std::size_t)x;
std::size_t res_y = (std::size_t)y;
Ogre::Real off_x = x - res_x;
Ogre::Real off_y = y - res_y;
// Finds the closest.
if(off_x > distance_ / 200)
++res_x;
if(off_y > distance_ / 200)
++res_y;
return get_node(res_x, res_y);
}
void Grid::create_graph(EntitySystem& ents, Ogre::Vector2 start, std::size_t w, std::size_t h, Ogre::Real d)
{
for(const auto& node : nodes_)
DestructorHelper::destroy(ents, node, true);
nodes_.clear();
nodes_.reserve(w * h);
ents.cleanup(); // Create special cleanup only for entities with a given component?
start_ = start;
width_ = w;
height_ = h;
distance_ = d;
starting_index_ = Component::NO_ENTITY;
std::vector<GridNodeComponent*> comps(width_ * height_); // Keep pointers to components for fast access.
Ogre::Vector2 pos{start_};
auto node_count = w * h;
for(std::size_t i = 0; i < node_count; ++i)
{
pos.x = (i % width_) * distance_;
pos.y = (i / width_) * distance_;
auto id = add_node(ents, pos);
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp) // Should never be false though as the component is added 2 lines above...
{
comps[i] = comp;
comps[i]->x = i % width_;
comps[i]->y = i / width_;
}
}
// Link nodes.
for(std::size_t i = 0; i < node_count; ++i)
link_(i, comps);
free_nodes_ = nodes_;
}
Ogre::Real Grid::get_distance() const
{
return distance_;
}
Grid& Grid::instance()
{
static Grid inst{};
return inst;
}
std::size_t Grid::get_random_free_node() const
{
return free_nodes_[util::get_random(0, free_nodes_.size() - 1)];
}
Ogre::Vector2 Grid::get_center_position(EntitySystem& ents) const
{
auto center_node = get_node(width_ / 2, height_ / 2);
return PhysicsHelper::get_2d_position(ents, center_node);
}
void Grid::link_(std::size_t index, std::vector<GridNodeComponent*>& comps)
{
if(!in_board(index) || !comps[index])
return;
std::size_t x{comps[index]->x}, y{comps[index]->y};
bool bottom{y == height_ - 1}, top{y == 0}, left{x == 0}, right = {x == width_ - 1};
if(!right && comps[index + 1])
comps[index]->neighbours[DIRECTION::RIGHT] = index + 1;
if(!left && comps[index - 1])
comps[index]->neighbours[DIRECTION::LEFT] = index - 1;
if(!bottom && comps[index + width_])
comps[index]->neighbours[DIRECTION::DOWN] = index + width_;
if(!top && comps[index - width_])
comps[index]->neighbours[DIRECTION::UP] = index - width_;
if(!bottom && !left && comps[index + width_ - 1])
comps[index]->neighbours[DIRECTION::DOWN_LEFT] = index + width_ - 1;
if(!top && !left && comps[index - width_ - 1])
comps[index]->neighbours[DIRECTION::UP_LEFT] = index - width_ - 1;
if(!bottom && !right && comps[index + width_ + 1])
comps[index]->neighbours[DIRECTION::DOWN_RIGHT] = index + width_ + 1;
if(!top && !right && comps[index - width_ + 1])
comps[index]->neighbours[DIRECTION::UP_RIGHT] = index - width_ + 1;
}