-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentManager.cpp
More file actions
154 lines (129 loc) · 3.67 KB
/
ComponentManager.cpp
File metadata and controls
154 lines (129 loc) · 3.67 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
#include "ComponentManager.h"
typedef ComponentManager::GameObject GameObject;
ComponentManager::ComponentManager()
{
// Nothing to do
}
// Component Manager
// 1. creates and houses all gameobjects
// 2. houses all components
// This should be the centre of freeing their memory too
ComponentManager::~ComponentManager()
{
cout << "ComponentManager now will deallocate lists." << endl;
typeToListMap::iterator lists = componentLists.begin();
while (lists != componentLists.end())
{
// Please check if this works
ComponentList* currentList = (*lists).second;
delete currentList;
lists++;
}
}
// Game-Object related things
int ComponentManager::newGameObject()
{
GameObject* g = new GameObject();
return gameObjects.addComponent(g);
}
// For convenience
int ComponentManager::newGameObject(vector<Component*> components)
{
int objID = newGameObject();
cout << "Constructing new GameObject at ID " << objID << "." << endl;
for (int i = 0; i < components.size(); i++)
{
Component* c = components[i];
c->setParentObjectID(objID);
gameObjAddComponent(objID, c);
}
return objID;
}
// Utility method
GameObject* ComponentManager::getGameObject(int objID)
{
return (GameObject*)gameObjects.getComponent(objID);
}
void ComponentManager::gameObjAddComponent
(int objID, Component* c)
{
// You should validate objID first.
// Get the gameobject being referenced
cout << ".." << endl;
GameObject* object = getGameObject(objID);
cout << "Obj id " << objID << endl;
cout << ",," << endl;
for (int i = 0; i < GameObject::SIZE_LIMIT; i++)
{
Component* current = object->components[i];
if (current == NULL)
{
object->components[i] = c;
return;
}
}
// Announce error if we ran out of space for new components
}
bool ComponentManager::gameObjHasComponentOfType(int objID, ComponentType type)
{
return gameObjGetComponentOfType(objID, type) != NULL;
}
Component* ComponentManager::gameObjGetComponentOfType(int objID, ComponentType type)
{
GameObject* object = getGameObject(objID);
if (object == NULL) cout << "We have a problem." << endl;
for (int i = 0; i < GameObject::SIZE_LIMIT; i++)
{
Component* current = object->components[i];
if (current != NULL)
{
if (current->getType() == type)
return current;
}
}
return NULL;
}
// Counting the number of dependencies
int ComponentManager::gameObjDependencyCount(int objID)
{
GameObject* object = getGameObject(objID);
return object->dependencyCount;
}
void ComponentManager::gameObjRegisterDependency(int objID)
{
GameObject* object = getGameObject(objID);
object->dependencyCount++;
}
void ComponentManager::gameObjRemoveDependency(int objID) {
GameObject* object = getGameObject(objID);
if (object->dependencyCount > 0) object->dependencyCount--;
}
// ComponentList-related things:
int ComponentManager::addComponent(Component* myComponent, ComponentType type)
{
typeToListMap::const_iterator iter;
iter = componentLists.find(type);
if (iter == componentLists.end())
{
cout << "Adding new type of component" << endl;
ComponentList* newList = new ComponentList();
componentLists[type] = newList;
return newList->addComponent(myComponent);
// Not found, make new ones
}
else
{
cout << "Adding existing type of component" << endl;
// Find existing list, and return the index
ComponentList* existingList = iter->second;
return existingList->addComponent(myComponent);
}
}
ComponentList* ComponentManager::getComponents(ComponentType type)
{
// the componentlists[type] before apparently initialized it prematurely
// that took 15 minutes for me to figure out.
typeToListMap::iterator iter = componentLists.find(type);
if (iter == componentLists.end()) return NULL;
else return iter->second;
}