Now that we have a complete weapon and ammo system, we can shoot and cause some damage. However, how do we know when an object has been hit? If we don't know this information, then how can we know if someone is supposed to take damage or if an object is supposed to break? The answer to this problem, like many, is events. We will add a couple of methods to our game's rules that get called when a piece of ammunition hits any object in the game word. From there, it will determine what should happen. Let's get started:
Add an
OnAmmoHit()
method to theCGasGameRules
class and implement it as follows:void CGASGameRules::OnAmmoHit( IAmmo* const pAmmo, IEntity*const pVictim ) { if( "Player" == pVictim->GetClass()->GetName() ) { auto pActor = static_cast< CPlayer* >( gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor( pVictim->GetId() ) ); if( pActor ) pActor->ApplyDamage( pAmmo->GetPower() ); } }
When any object in...