forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovementSystem.cpp
125 lines (103 loc) · 3.46 KB
/
MovementSystem.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
#include "MovementSystem.hpp"
MovementSystem::MovementSystem(EntitySystem& ents)
: entities_{ents}, last_delta_{}
{ /* DUMMY BODY */ }
void MovementSystem::update(Ogre::Real delta)
{ // TODO: Fix this...
last_delta_ = delta;
for(auto& ent : entities_.get_component_container<PathfindingComponent>())
{
auto& path_comp = ent.second;
if(path_comp.path_queue.empty())
continue;
auto move_comp = entities_.get_component<MovementComponent>(ent.first);
auto phys_comp = entities_.get_component<PhysicsComponent>(ent.first);
if(!move_comp || !phys_comp)
continue;
auto next = path_comp.path_queue.front();
auto dir_to_next = MovementHelper::dir_to(entities_, ent.first, next);
dir_to_next.y = 0; // Will prohibit the entity from going under the ground.
dir_to_next.normalise();
if(!move(ent.first, dir_to_next))
{
// TODO: perform a*? Or wait and then perform a*?
}
auto pos_next = PhysicsHelper::get_position(entities_, next);
pos_next.y = phys_comp->half_height; // Ignore the Y distance.
if(pos_next.distance(phys_comp->position) < move_comp->speed_modifier * delta)
{
PhysicsHelper::move_to(entities_, ent.first, pos_next);
path_comp.last_id = next;
path_comp.path_queue.pop_front();
if(!path_comp.path_queue.empty())
GraphicsHelper::look_at(entities_, ent.first, path_comp.path_queue.front());
}
}
}
bool MovementSystem::can_move_to(std::size_t id, Ogre::Vector3 pos)
{
auto graph_comp = entities_.get_component<GraphicsComponent>(id);
auto phys_comp = entities_.get_component<PhysicsComponent>(id);
if(!graph_comp)
return true; // Invisible objects will be able to move anywhere.
if(phys_comp)
{
if(!phys_comp->solid)
return true;
graph_comp->node->setPosition(pos); // Old position backed up in phys_comp.position.
auto& ents = entities_.get_component_container<PhysicsComponent>(); // Only need to check entities with PhysicsComponent.
for(const auto& ent : ents)
{
if(id == ent.first)
continue;
if(PhysicsHelper::is_solid(entities_, ent.first) &&
GraphicsHelper::collide(entities_, id, ent.first))
{
graph_comp->node->setPosition(phys_comp->position);
return false;
}
}
// Reverts the change, because of possible checks without actual movement.
graph_comp->node->setPosition(phys_comp->position);
return true;
}
else
return false;
}
bool MovementSystem::checked_move(std::size_t id, Ogre::Vector3 dir_vector)
{
auto phys_comp = entities_.get_component<PhysicsComponent>(id);
auto mov_comp = entities_.get_component<MovementComponent>(id);
if(phys_comp && mov_comp)
{
auto new_pos = phys_comp->position;
auto dir = dir_vector * mov_comp->speed_modifier;
new_pos += dir;
if(can_move_to(id, new_pos))
{
phys_comp->position = new_pos;
auto graph_comp = entities_.get_component<GraphicsComponent>(id);
if(graph_comp)
graph_comp->node->setPosition(phys_comp->position);
return true;
}
}
return false;
}
bool MovementSystem::move(std::size_t id, Ogre::Vector3 dir_vector)
{
auto phys_comp = entities_.get_component<PhysicsComponent>(id);
auto mov_comp = entities_.get_component<MovementComponent>(id);
if(phys_comp && mov_comp)
{
auto new_pos = phys_comp->position;
auto dir = dir_vector * mov_comp->speed_modifier * last_delta_;
new_pos += dir;
phys_comp->position = new_pos;
auto graph_comp = entities_.get_component<GraphicsComponent>(id);
if(graph_comp)
graph_comp->node->setPosition(phys_comp->position);
return true;
}
return false;
}