forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridNodeHelper.cpp
More file actions
121 lines (107 loc) · 3.01 KB
/
GridNodeHelper.cpp
File metadata and controls
121 lines (107 loc) · 3.01 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
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
#include "GridNodeHelper.hpp"
#include "EntitySystem.hpp"
#include "Grid.hpp"
#include "SelectionBox.hpp"
const std::array<std::size_t, GridNodeComponent::neighbour_count>& GridNodeHelper::get_neighbours(EntitySystem& ents, std::size_t id)
{
static std::array<std::size_t, GridNodeComponent::neighbour_count> NO_NEIGHBOURS{};
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
return comp->neighbours;
else
return NO_NEIGHBOURS;
}
bool GridNodeHelper::is_free(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
return comp->free;
else
return true;
}
bool GridNodeHelper::area_free(EntitySystem& ents, std::size_t center, std::size_t radius)
{
std::size_t x, y;
std::tie(x, y) = get_board_coords(ents, center);
x = x - radius;
y = y - radius;
radius = radius * 2 + 1;
for(std::size_t i = 0; i < radius; ++i)
{
for(std::size_t j = 0; j < radius; ++j)
{
auto id = Grid::instance().get_node(x + i, y + j);
if(!is_free(ents, id) || get_resident(ents, id) != Component::NO_ENTITY)
return false;
}
}
return true;
}
void GridNodeHelper::set_free(EntitySystem& ents, std::size_t id, bool val)
{
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
{
comp->free = val;
if(val)
{
Grid::instance().add_freed(id);
comp->resident = Component::NO_ENTITY;
}
else
Grid::instance().add_unfreed(id);
}
}
void GridNodeHelper::set_free_selected(EntitySystem& ents, SelectionBox& box, bool val)
{
for(auto& id : box.get_selected_entities())
set_free(ents, id, val);
}
std::tuple<std::size_t, std::size_t> GridNodeHelper::get_board_coords(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
return std::make_tuple(comp->x, comp->y);
else
return std::make_tuple(Component::NO_ENTITY, Component::NO_ENTITY);
}
void GridNodeHelper::set_resident(EntitySystem& ents, std::size_t id, std::size_t val)
{
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
{
if(comp->resident == Component::NO_ENTITY)
{
comp->resident = val;
comp->free = false;
}
}
}
std::size_t GridNodeHelper::get_resident(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GridNodeComponent>(id);
if(comp)
return comp->resident;
else
return Component::NO_ENTITY;
}
std::size_t GridNodeHelper::get_manhattan_distance(EntitySystem& ents, std::size_t id1, std::size_t id2)
{
std::size_t x1, y1;
std::tie(x1, y1) = get_board_coords(ents, id1);
std::size_t x2, y2;
std::tie(x2, y2) = get_board_coords(ents, id2);
return util::abs(int(x1 - x2)) + util::abs(int(y1 - y2));
}
std::size_t GridNodeHelper::get_node_in_dir(EntitySystem& ents, std::size_t id, int dir)
{
auto pos = PhysicsHelper::get_position(ents, id);
std::size_t node = Grid::instance().get_node_from_position(pos.x, pos.z);
if(dir == DIRECTION::NONE)
return node;
auto comp = ents.get_component<GridNodeComponent>(node);
if(comp)
return comp->neighbours[dir];
else
return Component::NO_ENTITY;
}