Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

objmgr.cpp

Go to the documentation of this file.
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) ) {    // Let's hope this never happens!
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         // If the list is empty, don't insert sort
00045         if (ol.size() == 0) {
00046                 ol.push_back(obj);
00047                 return;
00048         }
00049 
00050 //      The following peice of code used to sort only based on
00051 //      the object's Y value. The new code sorts based on a layer,
00052 //      then the object's Y value.
00053 //      if (obj->obj->GetLayer() == 0) {
00054 //              // Put the object into the list in increasing Y order
00055 //              while ( (it != ol.end()) && 
00056 //                      (obj->obj->GetY() > (*it)->obj->GetY()) )
00057 //                      it++;
00058 //
00059 //              // Insert wherever our iterator is
00060 //              if ( it == ol.end() )
00061 //                      ol.push_back(obj);
00062 //              else
00063 //                      ol.insert(it, obj);
00064 //
00065 //      } else {
00066 
00067         // Start at the end..
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);    // Insert inserts "before" the iterator
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         // beginning
00142         list<Object *>::iterator it = object_list.begin();
00143 
00144         // Loop through all objects. This list should be
00145         // sorted in the way we want objects to be drawn on screen
00146         for (; it != object_list.end(); ++it) {
00147                 // otherwise, draw object
00148                 if ( (*it)->obj )       // if it contains a game object, draw it
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(),   // for quick access
00158                                  end   = object_list.end(),     // for quick access
00159                                  itnext,                // Used for incrementing
00160                                  it,                    // Temporary iterator
00161                                  tmp;                   // Used once for removing elements
00162 
00163         // Loop from the beginning of the list to the end       
00164         for (it = begin; it != end; it = itnext) {
00165                 itnext = it;
00166                 itnext ++;      // the it iterator changes, save it's next ptr
00167 
00168                 // Some Quick access variables
00169                 Object *obj = (*it);
00170                 GameObject *go = obj->obj;
00171         
00172                 if (go) {
00173                         // Make our object think
00174                         go->think();
00175 
00176                         // Store its Y value for quick access
00177                         double ysort = go->GetY();
00178                         double layer = go->GetLayer();
00179 
00180                         // Sort this guy based on his Y going downward.
00181                         if (it != begin) {      // If we're not at the head...
00182                                 tmp = it--;     // make a temporary iterator
00183                                 object_list.erase (tmp);        // so we can delete and reinsert
00184 
00185 
00186 //      The following peice of code used to sort only based on
00187 //      the object's Y value. The new code sorts based on a layer,
00188 //      then the object's Y value.
00189 //                              if (layer == 0) {
00190 //                                      // Find where it belongs in the list
00191 //                                      for(; it != begin; --it) {
00192 //                                              // If the previous is less than, don't go any farther
00193 //                                              if ( (*it)->obj->GetY() <= ysort )
00194 //                                                      break;
00195 //      
00196 //                                              // If the previous is more, go farther
00197 //                                              if ( (*it)->obj->GetY() >  ysort )
00198 //                                                      continue;
00199 //                                      }
00200 //
00201 //                                      // If the previous is more than, insert before it
00202 //                                      if ( (*it)->obj->GetY() > ysort )
00203 //                                              object_list.insert (it, obj);
00204 //
00205 //                                      // otherwise insert after it.
00206 //                                      else if ( (*it)->obj->GetY() <= ysort ) {
00207 //                                              it++;
00208 //                                              object_list.insert (it, obj);
00209 //                                      }
00210 //                              } else {
00211 
00212                                 // Find where it belongs in the list
00213                                 for(; it != begin; --it) {
00214                                         // If the previous is the same,
00215                                         // sort by Y value
00216                                         if ( (*it)->obj->GetLayer() == layer ) {
00217                                                 // If the previous Y is less than, don't go any farther
00218                                                 if ( (*it)->obj->GetY() <= ysort )
00219                                                         break;
00220 
00221                                                 // If the previus is more, go farther
00222                                                 if ( (*it)->obj->GetY() > ysort )
00223                                                         continue;
00224                                         }
00225 
00226                                         // If the previous layer is less than, don't go any farther
00227                                         if ( (*it)->obj->GetLayer() < layer )
00228                                                 break;
00229 
00230                                         // If the previous is more, go farther
00231                                         if ( (*it)->obj->GetLayer() > layer )
00232                                                 continue;
00233                                 }
00234 
00235                                 // "it" should be at begin, so compare
00236                                 // against begin and order with begin.
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) // always insert after the iterator
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         // exclude excludes cross hair and main character
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 }

Generated on Sun Dec 8 12:02:20 2002 for nnc by doxygen1.3-rc1