-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentitymanager.cpp
More file actions
133 lines (118 loc) · 4.13 KB
/
entitymanager.cpp
File metadata and controls
133 lines (118 loc) · 4.13 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
/** cpp file for extra stuff entitymanager doesn't
* need to have in it's header.
*/
#include "entitymanager.h"
void EntityManager::addTransformPos(unsigned int eID, const gsl::vec3 &pos)
{
// Iterative version. Easier to read, but I think treeiterator is slower.
// for (auto it{treeBegin(eID)}; it != treeEnd(); ++it)
// {
// it->addPosition(pos);
// }
// Recursive version. Should be faster, actually.
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
comp->addPosition(pos);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformPos(*it, pos);
}
}
void EntityManager::addTransformRot(unsigned int eID, const gsl::quat &rot)
{
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
comp->addRotation(rot);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformRot(*it, rot);
}
}
void EntityManager::addTransformScale(unsigned int eID, const gsl::vec3 &scale)
{
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
comp->addScale(scale);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformScale(*it, scale);
}
}
void EntityManager::setTransformPos(unsigned int eID, const gsl::vec3 &pos)
{
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
auto delta = pos - getTransformPos(eID);
comp->addPosition(delta);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformPos(*it, delta);
}
}
void EntityManager::setTransformRot(unsigned int eID, const gsl::quat &rot)
{
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
// Temporary: Needs to find diff instead and add that.
auto delta = gsl::quat::diff(getTransformRot(eID), rot);
comp->addRotation(delta);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformRot(*it, delta);
}
}
void EntityManager::setTransformScale(unsigned int eID, const gsl::vec3 &scale)
{
auto comp = getComponent<TransformComponent>(eID);
if (comp != nullptr)
{
auto delta = scale - getTransformScale(eID);
comp->addScale(delta);
for (auto it{comp->children.begin()}; it != comp->children.end(); ++it)
addTransformScale(*it, delta);
}
}
gsl::vec3 EntityManager::getTransformPos(unsigned int eID)
{
auto comp = getComponent<TransformComponent>(eID);
return (comp != nullptr) ? comp->position : gsl::vec3{};
}
gsl::quat EntityManager::getTransformRot(unsigned int eID)
{
auto comp = getComponent<TransformComponent>(eID);
return (comp != nullptr) ? comp->rotation : gsl::quat{};
}
gsl::vec3 EntityManager::getTransformScale(unsigned int eID)
{
auto comp = getComponent<TransformComponent>(eID);
return (comp != nullptr) ? comp->scale : gsl::vec3{};
}
void EntityManager::UpdateBounds()
{
if (mTransformComponents.empty() || mMeshComponents.empty())
return;
auto meshIt = mMeshComponents.begin();
for (auto transIt = mTransformComponents.begin(); transIt != mTransformComponents.end(); ++transIt)
{
if (transIt->meshBoundsOutdated)
{
while (transIt->entityId > meshIt->entityId)
++meshIt;
// Transform doesn't have mesh
if (transIt->entityId != meshIt->entityId)
{
transIt->meshBoundsOutdated = false;
continue;
}
else
{
// Find which axis it scales the most in and use it as a uniform scale.
float biggestScale = 0.f;
for (auto i{0}; i < 3; ++i)
biggestScale = (biggestScale < *(&transIt->scale.x + i)) ? *(&transIt->scale.x + i) : biggestScale;
meshIt->bounds = {meshIt->meshData.bounds.centre, meshIt->meshData.bounds.radius * biggestScale};
transIt->meshBoundsOutdated = false;
}
}
}
}