-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_manager.cpp
More file actions
108 lines (96 loc) · 2.96 KB
/
entity_manager.cpp
File metadata and controls
108 lines (96 loc) · 2.96 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
#include "entity_manager.hpp"
void EntityManager::render(sf::RenderTarget* screen) {
// Single run of a bubble sort while we go through and render
for (std::vector<Entity*>::iterator it = ents.begin() ; it != ents.end(); ++it) {
Entity* ent = *it;
if ((it+1) != ents.end()) {
Entity* nextEnt = *(it + 1);
if (ent->realY > nextEnt->realY) {
*it = (*(it+1));
(*(it+1)) = ent;
ent = nextEnt;
}
}
ent->render(screen);
}
}
void EntityManager::flushRequests() {
while (!RequestQueues::entityRequests.empty()) {
// parse this request
entRequest request = RequestQueues::entityRequests.back();
if (request.entRequestType == ENT_REQUEST_NEW_ENT) {
addNewEntByType(request.entName, request.entType, request.X, request.Y);
} else if (request.entRequestType == ENT_REQUEST_DEL_ENT) {
removeEnt(request.uid, request.entType);
} else {
std::cerr << "Unknown request found of type " << request.entRequestType << " and ent type " << request.entType << std::endl;
}
RequestQueues::entityRequests.pop_back();
}
}
void EntityManager::update(float dt) {
unitManager->update(dt);
doodadManager->update(dt);
itemManager->update(dt);
flushRequests();
}
void EntityManager::addToEntList(Entity* newEnt) {
bool entered = false;
for (typename std::vector<Entity*>::iterator it = ents.begin() ; it != ents.end(); ++it) {
if ((*it)->realY >= newEnt->realY) {
ents.insert(it, newEnt);
entered = true;
break;
}
}
if (!entered) {
ents.push_back(newEnt);
}
}
Entity* EntityManager::addNewEntByType(std::string name, int type, float x, float y) {
Entity* newEnt = NULL;
if (type == ENT_TYPE_UNIT) {
newEnt = unitManager->addNewEntByType(name, x, y);
} else if (type == ENT_TYPE_DOODAD) {
newEnt = doodadManager->addNewEntByType(name, x, y);
} else if (type == ENT_TYPE_ITEM) {
newEnt = itemManager->addNewEntByType(name, x, y);
}
if (newEnt != NULL) {
addToEntList(newEnt);
}
return newEnt;
};
void EntityManager::addEnt(Entity* ent, int type) {
if (type == ENT_TYPE_UNIT) {
unitManager->addEnt((AI*) ent);
} else if (type == ENT_TYPE_DOODAD) {
doodadManager->addEnt((Doodad*) ent);
} else if (type == ENT_TYPE_ITEM) {
itemManager->addEnt((Item*) ent);
} else {
std::cerr << "addEnt called on unknown entity" << std::endl;
return;
}
addToEntList(ent);
};
// The in-place swap children do may mess up for loops that are iterating over ents
// But then, there are always problems with removals during iteration
void EntityManager::removeEnt(std::string uid, int type) {
if (type == ENT_TYPE_UNIT) {
unitManager->removeEnt(uid);
} else if (type == ENT_TYPE_DOODAD) {
doodadManager->removeEnt(uid);
} else if (type == ENT_TYPE_ITEM) {
itemManager->removeEnt(uid);
} else {
std::cerr << "removeEnt called on unknown entity" << std::endl;
}
for (typename std::vector<Entity*>::iterator it = ents.begin() ; it != ents.end(); ++it) {
if ((*it)->uid == uid) {
delete *it;
*it = ents[ents.size()-1];
ents.pop_back();
}
}
};