-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjectmanager.cpp
235 lines (213 loc) · 6.89 KB
/
objectmanager.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "objectmanager.h"
#include "object.h"
#include "unit.h"
#include "units.h"
#include "building.h"
#include "buildings.h"
#include "resource.h"
#include "resources.h"
#include "gridmap.h"
#include "input.h"
#include <set>
#include <map>
#include <iostream>
#include <fstream>
using namespace std;
ObjectManager::ObjectManager(){
next_unused_ref = 1;
}
void ObjectManager::UpdateAll(int ticks, GridMap *gridMap, ModelManager *modelManager){
for(map<ObjectRef,Object*>::iterator i = objects.begin(); i != objects.end(); ++i){
if(i->second != NULL){
i->second->Update(ticks,gridMap,modelManager);
}
}
}
void ObjectManager::DrawObjects(ModelManager *modelManager, TextureManager *textureManager, set<ObjectRef> refs){
for(set<ObjectRef>::iterator ref = refs.begin(); ref != refs.end(); ref++){
if(*ref != 0){ // there is no object 0
map<ObjectRef,Object*>::iterator obj;
obj = objects.find(*ref);
if(obj != objects.end() && obj->second != NULL){
obj->second->Draw(modelManager,textureManager);
} else {
cout << "Can't find object " << *ref << "." << endl;
}
}
}
}
void ObjectManager::DrawShadows(ModelManager *modelManager, set<ObjectRef> refs){
for(set<ObjectRef>::iterator ref = refs.begin(); ref != refs.end(); ref++){
if(*ref != 0){ // there is no object 0
map<ObjectRef,Object*>::iterator obj;
obj = objects.find(*ref);
if(obj != objects.end() && obj->second != NULL){
obj->second->DrawShadow(modelManager);
} else {
cout << "Can't find object " << *ref << "." << endl;
}
}
}
}
ObjectManager::~ObjectManager(){
for(map<ObjectRef,Object*>::iterator i = objects.begin(); i != objects.end(); ++i){
if(i->second != NULL){
delete i->second;
}
}
}
ObjectRef ObjectManager::Add(Object* obj){
objects[next_unused_ref] = obj;
++next_unused_ref;
return next_unused_ref - 1;
}
ObjectRef ObjectManager::Add(Unit *unit, GridMap *gridMap){
objects[next_unused_ref] = unit;
PointI size;
size.x = size.y = 1;
gridMap->AddObject(next_unused_ref,PT_PASSABLE,unit->pos,size);
++next_unused_ref;
return next_unused_ref - 1;
}
void ObjectManager::AddRef(ObjectRef ref){
// rather than incrementing using the [] notation, we find the refcount then inc, saving us some linear time searching
map<ObjectRef,int>::iterator refcount;
if((refcount = refcounts.find(ref)) != refcounts.end()){
refcount->second += 1;
}
}
void ObjectManager::RemoveRef(ObjectRef ref){
map<ObjectRef,int>::iterator refcount;
if(ref != 0 && (refcount = refcounts.find(ref)) != refcounts.end()){
refcount->second -= 1;
map<ObjectRef, Object*>::iterator obj = objects.find(ref);
if(refcount->second <= 0 && obj->second->expired == true){
cout << "Object " << ref << " removed." << endl;
refcounts.erase(refcount);
objects.erase(ref);
}
}
}
Object* ObjectManager::GetObject(ObjectRef ref){
map<ObjectRef,Object*>::iterator obj = objects.find(ref);
if(obj != objects.end()){
return obj->second;
}
return NULL;
}
void ObjectManager::LoadObjectMap(string mapFileName, GridMap *gridMap, ModelManager* modelManager, TextureManager *textureManager)
{
fstream mapFile;
mapFile.open(mapFileName.c_str(), fstream::in);
if(mapFile.fail()){
cout << "Error: Could not load object map file \"" << mapFileName << "\"." << endl;
}
int width, height;
mapFile >> width;
mapFile >> height;
TileState empty;
empty.objRef = 0;
empty.passType = PT_EMPTY;
vector<vector<TileState> > object_map(height,vector<TileState>(width,empty));
*gridMap = GridMap(object_map);
PointI p;
PointI lmSize; // dont keep...
lmSize.x = 2;
lmSize.y = 3;
PointI unitSize; // dont keep...
unitSize.x = unitSize.y = 1;
for (p.y = 0; p.y < height; p.y++)
{
for (p.x = 0; p.x < width; p.x++)
{
string s;
mapFile >> s;
switch(s[0])
{
case '*': //gold
gridMap->AddObject(Add(new Resource_Gold(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize);
break;
case '2': //player2 start
case '1': //player1 start
gridMap->AddObject(Add(new Unit_Worker(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case '3': //player1 start
gridMap->AddObject(Add(new Unit_Swordsman(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case '4': //player1 start
gridMap->AddObject(Add(new Unit_Archer(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case '5': //player1 start
gridMap->AddObject(Add(new Unit_Lancer(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case '6': //player1 start
gridMap->AddObject(Add(new Unit_Knight(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case '7': //player1 start
gridMap->AddObject(Add(new Unit_Catapult(modelManager,textureManager,p.x,p.y)),PT_PASSABLE,p,unitSize);
break;
case 'k':
gridMap->AddObject(Add(new Building_Keep(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize*4);
break;
case 'f':
gridMap->AddObject(Add(new Building_Farm(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize*2);
break;
case 'l':
gridMap->AddObject(Add(new Building_Lumbermill(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,lmSize);
break;
case 'w':
gridMap->AddObject(Add(new Building_Wall(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize*1);
break;
case 's':
gridMap->AddObject(Add(new Building_Blacksmith(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize*2);
break;
case 'b':
gridMap->AddObject(Add(new Building_Barracks(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize*3);
break;
case '#': //non-walkable (trees for now)
case '^': //trees
gridMap->AddObject(Add(new Resource_Tree(modelManager,textureManager,p.x,p.y)),PT_IMPASSABLE,p,unitSize);
break;
case '.':
default:
break;
}
}
}
mapFile.close();
}
void ObjectManager::HandleClick(VertexF location, ButtonName buttonName, GridMap *gridMap){
PointI pos;
pos.x = (int)(location.x/TILE_SIZE);
pos.y = (int)(location.z/TILE_SIZE);
ObjectRef ref = gridMap->GetObjectRefAt(pos);
/*if(ref != 0){
map<ObjectRef,Object*>::iterator obj = objects.find(ref);
if(obj != objects.end()){
if(obj->second->type&OBJ_UNIT){
Unit* unit = (Unit*)obj->second;
cout << unit->name << endl;
}
}
}*/
/*for(int y = -1; y <= 1; y++){
for(int x = -1; x <= 1; x++){
if(x != 0 || y != 0){ // we don't want to check center again
PointI temp;
temp.x = (int)location.x + x;
temp.y = (int)location.y + y;
if((ref = gridMap->GetObjectRefAt(temp)) != 0){ // there is an object at this point on the map
map<ObjectRef,Object*>::iterator obj = objects.find(ref);
if(obj != objects.end()){
if(obj->second->type&OBJ_UNIT){
Unit* unit = (Unit*)obj->second;
if(unit->lastPos == pos){
cout << unit->name << endl;
}
}
}
}
}
}
}*/
}