forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.cpp
More file actions
182 lines (159 loc) · 5.44 KB
/
InputSystem.cpp
File metadata and controls
182 lines (159 loc) · 5.44 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
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
#include "InputSystem.hpp"
InputSystem::InputSystem(EntitySystem& ents, OIS::Keyboard& key, Ogre::Camera& cam)
: entities_{ents}, first_person_{false}, first_person_id_{Component::NO_ENTITY}, keyboard_{key},
KEY_UP{OIS::KC_W}, KEY_DOWN{OIS::KC_S}, KEY_LEFT{OIS::KC_A}, KEY_RIGHT{OIS::KC_D},
cam_{cam}, cam_position_{}, cam_orientation_{}, ai_backup_{nullptr},
task_backup_{nullptr}, delete_input_{false}
{ /* DUMMY BODY */ }
void InputSystem::update(Ogre::Real delta)
{
if(first_person_)
{
bool moved{false}, rotated{false};
lpp::Script& script = lpp::Script::get_singleton();
auto& in_comp = *entities_.get_component<InputComponent>(first_person_id_);
if(keyboard_.isKeyDown((OIS::KeyCode)KEY_UP))
{
script.call<void, std::size_t, int>(in_comp.input_handler + ".handle", first_person_id_, KEY_UP);
moved = true;
}
if(keyboard_.isKeyDown((OIS::KeyCode)KEY_DOWN))
{
script.call<void, std::size_t, int>(in_comp.input_handler + ".handle", first_person_id_, KEY_DOWN);
moved = true;
}
if(keyboard_.isKeyDown((OIS::KeyCode)KEY_LEFT))
{
script.call<void, std::size_t, int>(in_comp.input_handler + ".handle", first_person_id_, KEY_LEFT);
rotated = true;
}
if(keyboard_.isKeyDown((OIS::KeyCode)KEY_RIGHT))
{
script.call<void, std::size_t, int>(in_comp.input_handler + ".handle", first_person_id_, KEY_RIGHT);
rotated = true;
}
auto& graph_comp = *entities_.get_component<GraphicsComponent>(first_person_id_);
if(rotated)
{ // TODO: Due to the fact that ogrehead.mesh is facing backwards, this will make the view right, but the
// controlls get swapped, research!
cam_.setOrientation(
graph_comp.node->getOrientation() * Ogre::Quaternion{Ogre::Degree{180.f}, Ogre::Vector3::UNIT_Y}
);
}
if(moved)
{
cam_.setPosition(graph_comp.node->getPosition());
}
}
}
bool InputSystem::is_first_person() const
{
return first_person_;
}
void InputSystem::set_first_person(bool on_off, std::size_t id)
{
if(on_off == first_person_)
return; // Only one first person mode entity at a time.
if(id != Component::NO_ENTITY)
first_person_id_ = id; // Allows for escaping first person mode without specifying the entity ID.
first_person_ = on_off;
if(first_person_ && !entities_.has_component<GraphicsComponent>(first_person_id_))
{
first_person_ = false;
return;
}
if(first_person_)
{
cam_position_ = cam_.getPosition();
cam_orientation_ = cam_.getOrientation();
// Adds the InputComponent if possible.
bool ai = entities_.has_component<AIComponent>(first_person_id_);
if(!entities_.has_component<InputComponent>(first_person_id_) && ai)
{
delete_input_ = true; // Delete it afterwards.
entities_.add_component<InputComponent>(first_person_id_); // The entity has to have InputComponent blueprint.
auto& ai_comp = *entities_.get_component<AIComponent>(first_person_id_);
auto& in_comp = *entities_.get_component<InputComponent>(first_person_id_);
auto& script = lpp::Script::get_singleton();
if(!script.is_nil(ai_comp.blueprint + ".InputComponent.input_handler"))
in_comp.input_handler = script.get<std::string>(ai_comp.blueprint + ".InputComponent.input_handler");
}
// AIComponent backup.
if(ai)
{
ai_backup_.reset(new AIComponent{std::move(*entities_.get_component<AIComponent>(first_person_id_))});
entities_.delete_component<AIComponent>(first_person_id_);
}
auto task_handler = entities_.get_component<TaskHandlerComponent>(first_person_id_);
if(task_handler)
{
task_backup_.reset(new TaskHandlerComponent{*task_handler});
entities_.delete_component<TaskHandlerComponent>(first_person_id_);
}
auto& graph_comp = *entities_.get_component<GraphicsComponent>(first_person_id_);
cam_.setOrientation(
graph_comp.node->getOrientation() * Ogre::Quaternion{Ogre::Degree{180.f}, Ogre::Vector3::UNIT_Y}
);
cam_.setPosition(graph_comp.node->getPosition());
}
else
{ // Restore 3rd person view.
cam_.setPosition(cam_position_);
cam_.setOrientation(cam_orientation_);
// AIComponent restore.
if(ai_backup_)
{
entities_.add_component<AIComponent>(first_person_id_);
auto ai_comp = entities_.get_component<AIComponent>(first_person_id_);
if(ai_comp)
{
*ai_comp = std::move(*ai_backup_);
ai_backup_.reset(nullptr);
}
}
if(task_backup_)
{
entities_.add_component<TaskHandlerComponent>(first_person_id_);
auto task_comp = entities_.get_component<TaskHandlerComponent>(first_person_id_);
if(task_comp)
{
task_comp->busy = task_backup_->busy;
task_comp->curr_task = task_backup_->curr_task;
task_comp->possible_tasks = task_backup_->possible_tasks;
task_comp->task_queue.swap(task_backup_->task_queue);
task_backup_.reset(nullptr);
}
}
if(delete_input_)
{
delete_input_ = false;
entities_.delete_component<InputComponent>(first_person_id_);
}
}
entities_.get_component<GraphicsComponent>(first_person_id_)->node->setVisible(!first_person_);
}
void InputSystem::rebind(int key, int new_key)
{
lpp::Script& script = lpp::Script::get_singleton();
switch(key)
{
case OIS::KC_W:
KEY_UP = new_key;
script.set("game.enum.input.key_up", KEY_UP);
break;
case OIS::KC_S:
KEY_DOWN = new_key;
script.set("game.enum.input.key_down", KEY_DOWN);
break;
case OIS::KC_A:
KEY_LEFT = new_key;
script.set("game.enum.input.key_left", KEY_LEFT);
break;
case OIS::KC_D:
KEY_RIGHT = new_key;
script.set("game.enum.input.key_right", KEY_RIGHT);
break;
default:
break;
}
}