-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_manager.cpp
More file actions
67 lines (57 loc) · 1.78 KB
/
unit_manager.cpp
File metadata and controls
67 lines (57 loc) · 1.78 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
#include "unit_manager.hpp"
void UnitManager::update(float dt) {
for (Unit* thisUnit : ents) {
thisUnit->update(dt);
// Handle kinetics
float new_x = thisUnit->realX + thisUnit->dx;
float new_y = thisUnit->realY + thisUnit->dy;
point new_point = TexXYToTileXY(new_x, new_y);
point cur_point = TexXYToTileXY(thisUnit->realX, thisUnit->realY);
if (curMap->isWalkable(new_point.tileX,new_point.tileY) || !curMap->isWalkable(cur_point.tileX, cur_point.tileY)) {
thisUnit->realX += thisUnit->dx;
thisUnit->realY += thisUnit->dy;
}
}
for (AI* thisAI : ais) {
thisAI->update(dt);
}
}
Unit* UnitManager::addNewEntByType(std::string type, float x, float y) {
// Ensure that the type exists
if (ent_library.find(type) == ent_library.end()) {
std::cerr << "ERROR: attempted to create unit of unknown type " << type << std::endl;
return NULL;
}
// Construct a name for the unit
std::ostringstream stream;
stream << type << "_" << num_ents_created[type]++;
Unit* newUnit = ent_library[type].clone(stream.str());
AI* newAI = new AI(newUnit, curMap);
newUnit->moveToRealXY(x, y);
addEnt(newAI);
lastCreatedEnt = newUnit;
return newUnit;
}
void UnitManager::addEnt(AI* ai) {
ais.push_back(ai);
ents.push_back(ai->controlled);
}
Unit* UnitManager::removeEnt(std::string uid) {
// Find the AI and do an in-place swap with the final element, then shrink
for (std::vector<AI*>::iterator it = ais.begin() ; it != ais.end(); ++it) {
if ((*it)->uid == uid) {
*it = ais[ais.size()-1];
ais.pop_back();
}
}
// Do the same for the connected unit
for (std::vector<Unit*>::iterator it = ents.begin() ; it != ents.end(); ++it) {
if ((*it)->uid == uid) {
Unit* deletedUnit = *it;
*it = ents[ents.size()-1];
ents.pop_back();
return deletedUnit;
}
}
return NULL;
}