forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicsHelper.cpp
More file actions
204 lines (176 loc) · 5.23 KB
/
Copy pathGraphicsHelper.cpp
File metadata and controls
204 lines (176 loc) · 5.23 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "GraphicsHelper.hpp"
#include "Components.hpp"
#include "EntitySystem.hpp"
void GraphicsHelper::set_mesh(EntitySystem& ents, std::size_t id, const std::string& mesh)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
comp->mesh = mesh;
}
const std::string& GraphicsHelper::get_mesh(EntitySystem& ents, std::size_t id)
{
static const std::string NO_MESH{"ERROR.mesh"}; // TODO: Create error mesh.
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->mesh;
else
return NO_MESH;
}
void GraphicsHelper::set_material(EntitySystem& ents, std::size_t id, const std::string& material)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
comp->material = material;
}
const std::string& GraphicsHelper::get_material(EntitySystem& ents, std::size_t id)
{
static const std::string NO_MATERIAL{"colour/pink"}; // TODO: Create pink error material.
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->material;
else
return NO_MATERIAL;
}
void GraphicsHelper::set_visible(EntitySystem& ents, std::size_t id, bool val)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
{
comp->visible = val;
if(comp->node)
comp->node->setVisible(val);
}
}
bool GraphicsHelper::is_visible(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->visible;
else
return false;
}
void GraphicsHelper::set_manual_scaling(EntitySystem& ents, std::size_t id, bool val)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
comp->manual_scaling = val;
}
bool GraphicsHelper::get_manual_scaling(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->manual_scaling;
else
return false;
}
void GraphicsHelper::set_scale(EntitySystem& ents, std::size_t id, const Ogre::Vector3& val)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
comp->scale = val;
}
const Ogre::Vector3& GraphicsHelper::get_scale(EntitySystem& ents, std::size_t id)
{
static const Ogre::Vector3 NO_SCALE{0.f, 0.f, 0.f};
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->scale;
else
return NO_SCALE;
}
void GraphicsHelper::look_at(EntitySystem& ents, std::size_t id1, std::size_t id2)
{
auto comp1 = ents.get_component<GraphicsComponent>(id1);
auto comp2 = ents.get_component<PhysicsComponent>(id2);
if(comp1 && comp2)
{
auto& target_pos = comp2->position;
comp1->node->lookAt(
Ogre::Vector3{target_pos.x, comp1->node->getPosition().y, target_pos.z},
Ogre::Node::TransformSpace::TS_WORLD,
Ogre::Vector3::UNIT_Z
);
}
}
void GraphicsHelper::rotate(EntitySystem& ents, std::size_t id, Ogre::Real delta, PLANE plane)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
{
Ogre::Vector3 plane_vector{0.f, 0.f, 0.f};
switch(plane)
{
case PLANE::X:
plane_vector.x = 1.f;
break;
case PLANE::Y:
plane_vector.y = 1.f;
break;
case PLANE::Z:
plane_vector.z = 1.f;
break;
}
comp->node->rotate(plane_vector, Ogre::Radian{delta});
}
}
const Ogre::AxisAlignedBox& GraphicsHelper::get_bounds(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp)
return comp->entity->getWorldBoundingBox();
else
return Ogre::AxisAlignedBox::BOX_NULL; // TODO: Test this vs BOX_INFINITE.
}
bool GraphicsHelper::collide(EntitySystem& ents, std::size_t id1, std::size_t id2)
{
return get_bounds(ents, id1).intersects(get_bounds(ents, id2));
}
void GraphicsHelper::init_graphics_component(EntitySystem& ents, Ogre::SceneManager& scene, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(!comp)
return;
if(comp->node && comp->entity)
{
comp->node->detachObject(comp->entity);
scene.destroyEntity(comp->entity);
}
if(!comp->node)
comp->node = scene.getRootSceneNode()->createChildSceneNode("entity_" + std::to_string(id));
comp->entity = scene.createEntity(comp->mesh);
comp->node->attachObject(comp->entity);
comp->node->setVisible(comp->visible);
if(comp->manual_scaling)
comp->node->setScale(comp->scale);
if(comp->material != "NO_MAT")
comp->entity->setMaterialName(comp->material);
auto half_height = comp->entity->getWorldBoundingBox(true).getHalfSize().y;
auto phys_comp = ents.get_component<PhysicsComponent>(id);
if(phys_comp)
{
phys_comp->half_height = half_height;
phys_comp->position = Ogre::Vector3{phys_comp->position.x,
half_height, phys_comp->position.z};
comp->node->setPosition(phys_comp->position);
}
}
void GraphicsHelper::set_query_flags(EntitySystem& ents, std::size_t id, std::size_t val)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp && comp->entity)
comp->entity->setQueryFlags(val);
}
std::size_t GraphicsHelper::get_query_flags(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp && comp->entity)
return comp->entity->getQueryFlags();
else
return std::size_t{};
}
void GraphicsHelper::apply_scale(EntitySystem& ents, std::size_t id)
{
auto comp = ents.get_component<GraphicsComponent>(id);
if(comp && comp->manual_scaling && comp->node)
comp->node->setScale(comp->scale);
}