/**\brief SpriteManager update function. */ void SpriteManager::Update() { // Update the sprites inside each quadrant // TODO: Update only the sprites that are in nearby Quadrants list<Sprite *> all_oob; list<QuadTree*> nearby = GetQuadrantsNear( Camera::Instance()->GetFocusCoordinate(), QUADRANTSIZE*4 ); list<QuadTree*>::iterator iter; for ( iter = nearby.begin(); iter != nearby.end(); ++iter ) { (*iter)->Update(); list<Sprite *>* oob = (*iter)->FixOutOfBounds(); all_oob.splice(all_oob.end(), *oob ); delete oob; } // Move sprites to adjacent Quadrants as they cross boundaries list<Sprite *>::iterator i; for( i = all_oob.begin(); i != all_oob.end(); ++i ) { GetQuadrant( (*i)->GetWorldPosition() )->Insert( *i ); } //Delete all sprites queued to be deleted if (!spritesToDelete.empty()) { spritesToDelete.unique(); for( i = spritesToDelete.begin(); i != spritesToDelete.end(); ++i ) { DeleteSprite(*i); } spritesToDelete.clear(); } for ( iter = nearby.begin(); iter != nearby.end(); ++iter ) { (*iter)->ReBallance(); } DeleteEmptyQuadrants(); }
/**\brief Returns a list of sprites that are near coordinate. * \param c Coordinate * \param r Radius * \return std::list of Sprite pointers. */ list<Sprite*> *SpriteManager::GetSpritesNear(Coordinate c, float r, int type) { list<Sprite*> *sprites = new list<Sprite*>(); // Search the possible quadrants list<QuadTree*> nearbyQuadrants = GetQuadrantsNear(c,r); list<QuadTree*>::iterator it; for(it = nearbyQuadrants.begin(); it != nearbyQuadrants.end(); ++it) { list<Sprite *>* nearby = new list<Sprite*>; (*it)->GetSpritesNear(c,r,nearby,type); sprites->splice(sprites->end(), *nearby); delete nearby; } // Sort sprites by their distance from the coordinate c sprites->sort(compareSpriteDistFromPoint(c)); return( sprites ); }
/**\brief Get a Sprite nearest to another Sprite. * \details Rather than just accept a Coordinate, this requires another Sprite * because the common usage is to look for a nearby enemy or * collision. Since a Sprite is by definition the closest thing to * its own position, this function needs to know what sprite to ignore. * * The Usual use for this is to find the nearest sprite of a certain type. \verbatim Sprite* found = GetNearestSprite(mySprite, 1000, DRAW_ORDER_SHIP); \endverbatim * * Rather than re-implement this code for when the caller truely * doesn't care about a Sprite, the caller can supply a dummy Sprite like: \verbatim Sprite* found = GetNearestSprite(Effect( Coordinate(0,0),"",0), 1000); \endverbatim * * \todo Build the Dummy example above into the SpriteManager. * */ Sprite* SpriteManager::GetNearestSprite(Sprite* obj, float r, int type) { float tmpdist; Sprite* closest=NULL; Sprite* possible=NULL; if(obj==NULL) return (Sprite*)NULL; list<QuadTree*> nearbyQuadrants = GetQuadrantsNear(obj->GetWorldPosition(),r); list<QuadTree*>::iterator it; for(it = nearbyQuadrants.begin(); it != nearbyQuadrants.end(); ++it) { possible = (*it)->GetNearestSprite(obj,r, type); if(possible!=NULL) { tmpdist = (obj->GetWorldPosition()-possible->GetWorldPosition()).GetMagnitude(); if(tmpdist<r) { r = tmpdist; closest = possible; } } } return closest; }