Example #1
0
void ProjectileFlyBState::init()
{
	_parent->setStateInterval(DEFAULT_BULLET_SPEED);
	_unit = _parent->getGame()->getSavedGame()->getBattleGame()->getSelectedUnit();
	// create a new projectile
	Projectile *projectile = new Projectile(_parent->getGame()->getResourcePack(),
									_parent->getGame()->getSavedGame()->getBattleGame(),
									_unit->getPosition(),
									_parent->getTarget(),
									_parent->getSelectedItem()->getRules()->getBulletSprite()
									);
	// add the projectile on the map
	_parent->getMap()->setProjectile(projectile);
	// let it calculate a trajectory
	if (projectile->calculateTrajectory())
	{
		// set the soldier in an aiming position
		_unit->aim(true);
		_parent->getMap()->cacheUnits();
		// and we have a lift-off
		_parent->getGame()->getResourcePack()->getSoundSet("BATTLE.CAT")->getSound(_parent->getSelectedItem()->getRules()->getFireSound())->play();
	}
	else
	{
		// no line of fire
		delete projectile;
		_parent->getMap()->setProjectile(0);
		_parent->popState();
	}

}
/**
 * - create a projectile sprite & add it to the map
 * - calculate it's trajectory
 * @return whether it succeeded
 */
bool ProjectileFlyBState::createNewProjectile()
{
	// create a new projectile
	Projectile *projectile = new Projectile(_parent->getResourcePack(), _parent->getSave(), _action, _origin);

	_autoshotCounter++;
	// add the projectile on the map
	_parent->getMap()->setProjectile(projectile);
	_parent->setStateInterval(Options::getInt("battleFireSpeed"));

	// let it calculate a trajectory
	_projectileImpact = -1;
	if (_action.type == BA_THROW)
	{
		if (projectile->calculateThrow(_unit->getThrowingAccuracy()))
		{
			_projectileItem->moveToOwner(0);
			_unit->setCache(0);
			_parent->getMap()->cacheUnit(_unit);
			_parent->getResourcePack()->getSoundSet("BATTLE.CAT")->getSound(39)->play();
			_unit->addThrowingExp();
		}
		else
		{
			// unable to throw here
			delete projectile;
			_parent->getMap()->setProjectile(0);
			_action.result = "STR_UNABLE_TO_THROW_HERE";
			_parent->popState();
			return false;
		}
	}
	else if (_unit->getType() == "CELATID") // special code for the "spit" trajectory
	{
		if (projectile->calculateThrow(_unit->getFiringAccuracy(_action.type, _action.weapon)))
		{
			// set the soldier in an aiming position
			_unit->aim(true);
			_parent->getMap()->cacheUnit(_unit);
			// and we have a lift-off
			if (_action.weapon->getRules()->getFireSound() != -1)
				_parent->getResourcePack()->getSoundSet("BATTLE.CAT")->getSound(_action.weapon->getRules()->getFireSound())->play();
		}
		else
		{
			// no line of fire
			delete projectile;
			_parent->getMap()->setProjectile(0);
			_action.result = "STR_NO_LINE_OF_FIRE";
			_parent->popState();
			return false;
		}
	}
	else
	{
		_projectileImpact = projectile->calculateTrajectory(_unit->getFiringAccuracy(_action.type, _action.weapon));
		if (_projectileImpact != -1 || _action.type == BA_LAUNCH)
		{
				// set the soldier in an aiming position
				_unit->aim(true);
				_parent->getMap()->cacheUnit(_unit);
				// and we have a lift-off
				if (_action.weapon->getRules()->getFireSound() != -1)
					_parent->getResourcePack()->getSoundSet("BATTLE.CAT")->getSound(_action.weapon->getRules()->getFireSound())->play();
				if (!_parent->getSave()->getDebugMode() && _action.type != BA_LAUNCH && _ammo->spendBullet() == false)
				{
					_parent->getSave()->removeItem(_ammo);
					_action.weapon->setAmmoItem(0);
				}
		}
		else
		{
			// no line of fire
			delete projectile;
			_parent->getMap()->setProjectile(0);
			_action.result = "STR_NO_LINE_OF_FIRE";
			_parent->popState();
			return false;
		}
	}

	return true;
}