示例#1
0
/**\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();
}
示例#2
0
/**\brief SpriteManager update function.
 * \details Update the sprites inside each quadrant
 * \param lowFps If true, forces the wave-update method to be used rather than the full-update
 */
void SpriteManager::Update( lua_State *L, bool lowFps) {
	//this will contain every quadrant that we will potentially want to update
	list<QuadTree*> quadList;
	
	//if update-all is given then we update every quadrant
	//we do the same if tickCount == 0 even if update-all is not given
	// (in wave update mode, tickCount == 0 is when we want to update all quadrants)
	if( ! lowFps || tickCount == 0) {
		//need to get all of the quadrants in our map
		GetAllQuadrants(&quadList);
	}
	else
	{
		//wave update mode with tickCount != 0 -- update some quadrants
		Camera* camera = Simulation_Lua::GetSimulation(L)->GetCamera();
		Coordinate currentPoint (camera->GetFocusCoordinate());	//always update centered on where we're at

		quadList.push_back (GetQuadrant (currentPoint)); //we ALWAYS update the current quadrant

		//we also ALWAYS update the 'regular' bands
		//	the first band is at index 1 - index 0 would be the single quadrant in the middle
		//	when we get the list of quadrants back we splice them onto the end of our overall list
		for (int i = 1; i <= numRegularBands; i ++) {
			list<QuadTree*> tempBandList = GetQuadrantsInBand (currentPoint, i);
			quadList.splice (quadList.end(), tempBandList);
		}

		//now - we SOMETIMES update the semi-regular bands
		//   the ticks that each band is updated in is stored in the map
		//   so we get our semiRegular update modulus of the ticks and then check the map
		//    - the map has the tick index as the key and the band to update as the value
		int semiRegularTick = tickCount % semiRegularPeriod;
		map<int,int>::iterator findBand = ticksToBandNum.find (semiRegularTick);
		if (findBand != ticksToBandNum.end()) {		//found the key
			//cout << "tick = " << tickCount << ", semiRegularTick = " << semiRegularTick << ", band = " << findBand->second << endl;
			list<QuadTree*> tempBandList = GetQuadrantsInBand (currentPoint, findBand->second);
			quadList.splice (quadList.end(), tempBandList);
		}
		else {
			//no semi-regular bands to update at this tick, do nothing
		}
	}

	// Find and Fix any Sprites that have moved out of bounds.
	list<Sprite *> all_oob;
	list<QuadTree*>::iterator iter;
	for ( iter = quadList.begin(); iter != quadList.end(); ++iter ) {
		(*iter)->Update(L);
		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.sort(); // The list has to be sorted or unique doesn't work correctly.
		spritesToDelete.unique();
	
		// Tell the AI that they've been killed
		for( i = spritesToDelete.begin(); i != spritesToDelete.end(); ++i ) {
			if( (*i)->GetDrawOrder() == DRAW_ORDER_SHIP ) {
				((AI*)(*i))->Killed(L);
			}
		}

		for( i = spritesToDelete.begin(); i != spritesToDelete.end(); ++i ) {
			DeleteSprite(*i);
		}
		spritesToDelete.clear();
	}

	for ( iter = quadList.begin(); iter != quadList.end(); ++iter ) {
		(*iter)->ReBallance();
	}

	DeleteEmptyQuadrants();

	// Update the tick count after all updates for this tick are done
	UpdateTickCount ();
}