00001 #include <iostream>
00002 #include <list>
00003 #include <vector>
00004 #include "object.h"
00005 #include "graphics.h"
00006 #include "objmgr.h"
00007 #include "bear.h"
00008
00009 ObjectManager::ObjectManager()
00010 {
00011 crosshair = NULL;
00012 mainchar = NULL;
00013 }
00014
00015 ObjectManager::~ObjectManager()
00016 {
00017 cout << "ObjectManager List: (count = " << object_list.size() << ")" << endl;
00018
00019 for(list<Object *>::iterator it = object_list.begin();
00020 it != object_list.end(); ++it) {
00021
00022 cout << "\t" << (*it)->obj->getName() << ": Layer " << (*it)->obj->GetLayer()
00023 << " at Y = " << (*it)->obj->GetY() << endl;
00024
00025 delete (*it)->obj;
00026 delete (*it);
00027 }
00028 object_list.clear();
00029 }
00030
00031 void ObjectManager::AddToAnyObjectList(list<Object *>& ol, Object *obj)
00032 {
00036 if ( !(obj->obj) ) {
00037 cout << "THIS SHOULD NEVER HAPPEN " << endl;
00038 ol.insert (ol.end(), obj);
00039 return;
00040 }
00041
00042 list<Object *>::iterator beg = ol.begin(), it;
00043
00044
00045 if (ol.size() == 0) {
00046 ol.push_back(obj);
00047 return;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 it = ol.end();
00069 it--;
00070
00071 int sortlayer = obj->obj->GetLayer();
00072 double sorty = obj->obj->GetY();
00073
00074 while ( (it != beg) &&
00075 (( sortlayer < (*it)->obj->GetLayer()) ||
00076 (( sortlayer == (*it)->obj->GetLayer()) &&
00077 ( sorty < (*it)->obj->GetY()))) )
00078 it--;
00079
00080 if ( it == beg ) {
00081 bool before = false;
00082
00083 if ( ( sortlayer < (*it)->obj->GetLayer()) )
00084 before = true;
00085
00086 else if ( (sortlayer == (*it)->obj->GetLayer()) &&
00087 (sorty < (*it)->obj->GetY()))
00088 before = true;
00089
00090 if (!before)
00091 it++;
00092
00093 ol.insert (it, obj);
00094 } else {
00095 it++;
00096
00097 ol.insert (it, obj);
00098 }
00099
00100 return;
00101 }
00102
00103 void ObjectManager::AddToObjectList(Object *obj)
00104 {
00105 AddToAnyObjectList (object_list, obj);
00106 }
00107
00108 void ObjectManager::AddObject(GameObject *go, objecttype ot)
00109 {
00110 Object *n = new Object;
00111 n->obj = go;
00112 n->type = ot;
00113
00114 AddToObjectList(n);
00115 }
00116
00117 GameObject* ObjectManager::AddObject(objecttype ot)
00118 {
00119 Object *n = new Object;
00120
00121 switch(ot) {
00122 case OT_MAINCHAR: n->obj = mainchar = new MainChar(*this); break;
00123 case OT_CROSSHAIR: n->obj = crosshair = new Crosshair(*this); break;
00124 case OT_UFO: n->obj = new UFO(*this); break;
00125 case OT_EXPLODE: n->obj = new ExplodeObject(*this); break;
00126 case OT_BEAR: n->obj = new Bear(*this); break;
00127 case OT_BEARSPAWN: n->obj = new BearSpawnPoint(*this); break;
00128 default: break;
00129 }
00130
00131 n->type = ot;
00132
00133 AddToObjectList(n);
00134
00135 return n->obj;
00136 }
00137
00138 vector<Rect> &ObjectManager::draw(Graphics &GD)
00139 {
00140
00141
00142 list<Object *>::iterator it = object_list.begin();
00143
00144
00145
00146 for (; it != object_list.end(); ++it) {
00147
00148 if ( (*it)->obj )
00149 object_draw_list.push_back ( (*it)->obj->draw (GD) );
00150 }
00151
00152 return object_draw_list;
00153 }
00154
00155 void ObjectManager::think()
00156 {
00157 list<Object *>::iterator begin = object_list.begin(),
00158 end = object_list.end(),
00159 itnext,
00160 it,
00161 tmp;
00162
00163
00164 for (it = begin; it != end; it = itnext) {
00165 itnext = it;
00166 itnext ++;
00167
00168
00169 Object *obj = (*it);
00170 GameObject *go = obj->obj;
00171
00172 if (go) {
00173
00174 go->think();
00175
00176
00177 double ysort = go->GetY();
00178 double layer = go->GetLayer();
00179
00180
00181 if (it != begin) {
00182 tmp = it--;
00183 object_list.erase (tmp);
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 for(; it != begin; --it) {
00214
00215
00216 if ( (*it)->obj->GetLayer() == layer ) {
00217
00218 if ( (*it)->obj->GetY() <= ysort )
00219 break;
00220
00221
00222 if ( (*it)->obj->GetY() > ysort )
00223 continue;
00224 }
00225
00226
00227 if ( (*it)->obj->GetLayer() < layer )
00228 break;
00229
00230
00231 if ( (*it)->obj->GetLayer() > layer )
00232 continue;
00233 }
00234
00235
00236
00237 if (it == begin) {
00238 double before = false;
00239
00240 if ( (layer < (*it)->obj->GetLayer()) )
00241 before = true;
00242 else if ( (layer == (*it)->obj->GetLayer()) &&
00243 (ysort < (*it)->obj->GetY()) )
00244 before = true;
00245
00246 if (!before)
00247 it++;
00248
00249 } else if (it != end)
00250 it++;
00251
00252 object_list.insert (it, obj);
00253 }
00254 }
00255 }
00256 }
00257
00258 void ObjectManager::animate()
00259 {
00260 list<Object *>::iterator it = object_list.begin();
00261
00262 for(; it != object_list.end(); ++it) {
00263 if ( (*it)->obj )
00264 (*it)->obj->animate();
00265 }
00266 }
00267
00268 void ObjectManager::recenter()
00269 {
00270 list<Object *>::iterator it = object_list.begin();
00271
00272 for(; it != object_list.end(); ++it) {
00273 if ( (*it)->obj )
00274 (*it)->obj->Recenter();
00275 }
00276 }
00277
00278 GameObject *ObjectManager::GetObject(objecttype ot)
00279 {
00280 switch(ot) {
00281 case OT_MAINCHAR:
00282 return mainchar;
00283 case OT_CROSSHAIR:
00284 return crosshair;
00285 default:
00286 list<Object *>::iterator it = object_list.begin();
00287
00288 for(; it != object_list.end(); ++it) {
00289 if ( (*it)->type == ot )
00290 return (*it)->obj;
00291 }
00292 break;
00293 }
00294 return NULL;
00295 }
00296
00297 void ObjectManager::update()
00298 {
00299 list<Object *>::iterator tmp, it = object_list.begin();
00300
00301 for(; it != object_list.end();) {
00302 tmp = it++;
00303
00304 if ( (*tmp)->obj && (*tmp)->obj->IsDead() ) {
00305 delete (*tmp)->obj;
00306 delete (*tmp);
00307 object_list.erase(tmp);
00308 }
00309 }
00310 }
00311
00312 GameObject* ObjectManager::FindFirstCollision(Rect &R, bool exclude)
00313 {
00314 list<Object *>::iterator it = object_list.begin();
00315
00316
00317
00318 for (; it != object_list.end(); it++) {
00319 if(exclude) {
00320 if ( (*it)->type == OT_CROSSHAIR ) continue;
00321 if ( (*it)->type == OT_MAINCHAR ) continue;
00322 }
00323
00324 if ( (*it)->obj && (*it)->obj->IsCollidable() ) {
00325 Rect S = (*it)->obj->GetBoundingRect();
00326 if (Util_Intersect(R, S)) {
00327 return (*it)->obj;
00328 }
00329 }
00330 }
00331
00332 return (GameObject *)NULL;
00333 }