Creating ammo events

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:

  1. Add an OnAmmoHit() method to the CGasGameRules 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 the world gets hit by a piece of ammunition, this method is called; pVictim is the object that's hit. We simply check whether pVictim is of the class type CPlayer (which the player and AI are) and if so, we apply damage to that object.

  2. Add an OnAmmoPickupRequest() method to the CGasGameRules class and implement it as follows:
    void CGASGameRules::OnAmmoPickupRequest( IAmmo* const pAmmo, IEntity*const pRequester )
    {
      if( "Player" == pRequester->GetClass()->GetName() )
      {
        //Get The Actor From The Requester.
        auto pActor = static_cast< CPlayer* >( gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor( pRequester->GetId() ) );
        if( pActor )
        {
          //Get The Requester's Weapon.
          auto pWeapon = pActor->GetWeapon();
          if( pWeapon )
          {
            //If The Requester's Weapon Is Compatible With The Specified Ammo Then Pick It Up.
            if( pWeapon->GetAmmoClass() == pAmmo->GetAmmoClass() )
              pActor->PickupAmmo( pAmmo );
          }
        }
      }
    }

    If ammunition is touched when it has not been launched, this method will be called; pRequester is the object that touched the ammo. We simply check whether pRequester is of the class type CPlayer (which the player and AI are) and if so, we allow them to pick it up.

If you remember correctly, I told you to figure out what the AAmmo class' ProcessEvent() method did. In the AAmmo class' ProcessEvent() method, we check whether the event that was raised was a collision event (our ammo has collided with another object in the world) and if so, we check our launch state. If we were launched, then the object should possibly take damage. So, we call our game rules' OnAmmoHit() method. If we were not launched, then that means that the ammo was in the game world just waiting to be picked up and the object should attempt to pick it up. Hence, the OnAmmoPickupRequest() method is called.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.23.181