Example #1
0
bool AiEntity::useWeapon() {
  double time = mGameModel->physics->getLastUpdateTime();
  if (mInventory[0] == 0) return false;
  if (mNextShotTime > time) return false;

  item_t item = mGameModel->itemManager->getItem(mInventory[0]);
  if (item.type != ITEMTYPE_GUN_ONE_HANDED) {
    return false;
  }

  v3d_t targetPosition = mTargetPhysicsEntity->boundingBox.getCenterPosition();
  v3d_t vecToTarget = v3d_sub(targetPosition, mWorldPosition);
  double distanceToTarget = v3d_mag(vecToTarget);
  if (!isTargetInRange(item.gunType, distanceToTarget)) {
    return false;
  }

  shot_info_t shotInfo;
  shotInfo.angle = v3d_normalize(vecToTarget);
  shotInfo.ownerPhysicsHandle = mPhysicsHandle;
  shotInfo.position = mWorldPosition;
  shotInfo.time = time;

  mNextShotTime = mGameModel->itemManager->useGun(mInventory[0], shotInfo);

  return true;
}
Example #2
0
bool UnitFiring::launch(osg::Vec3 from, osg::Vec3 direction) {
	bool loaded = isLoaded();
	bool inRange = isTargetInRange(mTargetPosition);
	if (loaded && inRange) {
		//create and launch projectile
		Projectile *projectile = new Projectile(mProjectileRadius, mProjectileMass, mProjectileYield, mProjectileType);
		projectile->init();
		projectile->setPosition(from);
		projectile->setTarget(mTargetPosition);
		projectile->setOwner(this);
		projectile->launch(direction, mProjectileVelocity);

		theApp->BroadcastEvent(Event::FIRE, this,  const_cast<Unit*>(mTargetUnit));

		/*//debug
		osg::Vec3 t = p->getPosition();
		std::cout << "Launching projectile from " << t[0] << "," << t[1] << "," << t[2] << std::endl;
		std::cout << "With direction " << direction[0] << "," << direction[1] << "," << direction[2] << std::endl;
		direction.normalize();
		std::cout << "With (normalized) direction " << direction[0] << "," << direction[1] << "," << direction[2] << std::endl;*/

		reload();

		return true;

	} else if (!loaded) {
		//std::cout << "Reloading! You must wait " << getRemainingReloadTime() << "secs!" << std::endl;
		//theApp->hudGUI->setReload(mReloadWindow, getRemainingReloadTime());
	} else if (!inRange) {
		//std::cout << "Target is out of range! Target is " << (mTargetPosition - getPosition()).length() << " away, max range is " << getProjectileRange() << std::endl;
	}
	return false;
}