官术网_书友最值得收藏!

  • CRYENGINE Game Development Blueprints
  • Richard Gerard Marcoux III Chris Goodswen Riham Toulan Sam Howels
  • 439字
  • 2021-07-16 20:21:18

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.

主站蜘蛛池模板: 高唐县| 同仁县| 清河县| 广丰县| 荃湾区| 克山县| 灌南县| 肇庆市| 阿瓦提县| 甘南县| 大宁县| 南城县| 嘉祥县| 佛坪县| 兴城市| 临颍县| 博白县| 石城县| 饶阳县| 北流市| 洪泽县| 通州市| 突泉县| 周至县| 肇东市| 涡阳县| 宝兴县| 壶关县| 丹棱县| 宁安市| 宝丰县| 桃园县| 木兰县| 禹州市| 南华县| 昌吉市| 涞源县| 于田县| 宝应县| 万安县| 璧山县|