-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectManager.cpp
More file actions
177 lines (149 loc) · 4 KB
/
GameObjectManager.cpp
File metadata and controls
177 lines (149 loc) · 4 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
#include "GameObjectManager.h"
#include "EngineTime.h"
#include "Cube.h"
#include "Plane.h"
#include "MathUtils.h"
#include "AGameObject.h"
#include "PhysicsCube.h"
#include "PhysicsPlane.h"
#include "TexturedCube.h"
GameObjectManager* GameObjectManager::sharedInstance = NULL;
GameObjectManager* GameObjectManager::getInstance()
{
return sharedInstance;
}
void GameObjectManager::initialize()
{
sharedInstance = new GameObjectManager();
}
void GameObjectManager::destroy()
{
sharedInstance->gameObjectMap.clear();
sharedInstance->gameObjectList.clear();
delete sharedInstance;
}
AGameObject* GameObjectManager::findObjectByName(String name)
{
if (this->gameObjectMap[name] != NULL) {
return this->gameObjectMap[name];
}
else {
std::cout << "Object " << name << " not found!";
return NULL;
}
}
GameObjectManager::List GameObjectManager::getAllObjects()
{
return this->gameObjectList;
}
int GameObjectManager::activeObjects()
{
return this->gameObjectList.size();
}
void GameObjectManager::updateAll()
{
for (int i = 0; i < this->gameObjectList.size(); i++) {
//replace with component update
if (this->gameObjectList[i]->isEnabled()) {
this->gameObjectList[i]->update(EngineTime::getDeltaTime());
}
}
}
void GameObjectManager::renderAll(int viewportWidth, int viewportHeight)
{
for (int i = 0; i < this->gameObjectList.size(); i++) {
//replace with component update
if (this->gameObjectList[i]->isEnabled()) {
this->gameObjectList[i]->draw(viewportWidth, viewportHeight);
}
}
}
void GameObjectManager::addObject(AGameObject* gameObject)
{
if (this->gameObjectMap[gameObject->getName()] != NULL) {
int count = 1;
String revisedString = gameObject->getName() + " " + "(" + std::to_string(count) + ")";
while (this->gameObjectMap[revisedString] != NULL) {
count++;
revisedString = gameObject->getName() + " " + "(" + std::to_string(count) + ")";
}
//std::cout << "Duplicate found. New name is: " << revisedString << "\n";
gameObject->name = revisedString;
this->gameObjectMap[revisedString] = gameObject;
}
else {
this->gameObjectMap[gameObject->getName()] = gameObject;
}
this->gameObjectList.push_back(gameObject);
}
void GameObjectManager::createObject(PrimitiveType type)
{
if (type == PrimitiveType::CUBE) {
Cube* cube = new Cube("Cube");
cube->setPosition(0.0f, 0.0f, 0.0f);
cube->setScale(1.0f, 1.0f, 1.0f);
this->addObject(cube);
}
else if (type == PrimitiveType::PLANE) {
Plane* plane = new Plane("Plane");
this->addObject(plane);
}
else if (type == PrimitiveType::TEXTURED_CUBE) {
TexturedCube* cube = new TexturedCube("Cube_Textured");
cube->setPosition(0.0f, 0.0f, 0.0f);
cube->setScale(1.0f, 1.0f, 1.0f);
this->addObject(cube);
}
else if (type == PrimitiveType::PHYSICS_CUBE) {
PhysicsCube* cube = new PhysicsCube("Cube_Physics");
this->addObject(cube);
}
else if (type == PrimitiveType::PHYSICS_PLANE) {
PhysicsPlane* plane = new PhysicsPlane("Plane_Physics");
this->addObject(plane);
}
}
void GameObjectManager::deleteObject(AGameObject* gameObject)
{
this->gameObjectMap.erase(gameObject->getName());
int index = -1;
for (int i = 0; i < this->gameObjectList.size(); i++) {
if (this->gameObjectList[i] == gameObject) {
index = i;
break;
}
}
if (index != -1) {
this->gameObjectList.erase(this->gameObjectList.begin() + index);
}
if (gameObject == this->selectedObject)
this->selectedObject = nullptr;
delete gameObject;
}
void GameObjectManager::deleteObjectByName(String name)
{
AGameObject* object = this->findObjectByName(name);
if (object != NULL) {
this->deleteObject(object);
}
}
void GameObjectManager::setSelectedObject(String name)
{
if (this->gameObjectMap[name] != NULL) {
this->setSelectedObject(this->gameObjectMap[name]);
}
}
void GameObjectManager::setSelectedObject(AGameObject* gameObject)
{
this->selectedObject = gameObject;
}
AGameObject* GameObjectManager::getSelectedObject()
{
return this->selectedObject;
}
GameObjectManager::GameObjectManager()
{
}
GameObjectManager::~GameObjectManager()
{
}