Example #1
0
/*
 * Animate explosion sprites. If their animation is finished remove them from the list.
 * If the list is empty, this states is finished.
 */
void ExplosionBState::think()
{
	for (std::set<Explosion*>::const_iterator i = _parent->getMap()->getExplosions()->begin(), inext = i; i != _parent->getMap()->getExplosions()->end(); i = inext)
	{
		++inext;
		if(!(*i)->animate())
		{
			_parent->getMap()->getExplosions()->erase((*i));
			if (_parent->getMap()->getExplosions()->empty())
			{
				SavedBattleGame *save = _parent->getGame()->getSavedGame()->getBattleGame();
				// after the animation is done, the real explosion takes place
				if (_item)
				{
					save->getTerrainModifier()->explode(_center, _item->getRules()->getPower(), _item->getRules()->getDamageType(), 100, _unit);
				}
				if (_tile)
				{
					save->getTerrainModifier()->explode(_center, _tile->getExplosive(), DT_HE, 100, _unit);
				}

				// now check for new casualties
				_parent->checkForCasualties(_item, _unit);

				// if this explosion was caused by a unit shooting, now it's the time to put the gun down and put the camera back on the shooter
				if (_unit && !_unit->isOut())
				{
					_unit->aim(false);
					if (_parent->getMap()->didCameraFollow())
					{
						_parent->getMap()->centerOnPosition(_unit->getPosition());
					}
				}
				_parent->getMap()->cacheUnits();
				_parent->popState();

				// check for terrain explosions
				Tile *t = save->getTerrainModifier()->checkForTerrainExplosions();
				if (t)
				{
					Position p = Position(t->getPosition().x * 16, t->getPosition().y * 16, t->getPosition().z * 24);
					_parent->statePushNext(new ExplosionBState(_parent, p, 0, _unit, t));
				}

				return;
			}
		}
	}
}