4:《地牢守卫者》代码分析:Enemy

  Enemy也是继承自pawn。先从PostBeginPlay中说起,首先得为Enemy创建一个Controller,这里当然要从PostBeginPlay中调用了。官方提供了SpawnDefautlController

event PostBeginPlay()

{

  super.PostBeginPlay();

  if(Controller==none)

  {

    SpawnDefaultController();

  }
}



function SpawnDefaultController()

{

  if(AIControllerTemplate!=none)

  Spawn(AIControllerTemplate.class,,,Location,Rotation,AIControllerTemplate,true).Possess(self,false); //生成直接占有该pawn
}

前面一直忽略了一个pawn死亡将会调用的函数Died。当pawn死亡时将会调用该函数,我将会在该函数内添加自己的一些动作。比如让尸体消失的粒子效果和生成AntManaToken:

function bool Died(Controller killer,class<DamageType>DamageType,vector HitLocation)

{

  if(!Died(killer,DamageType,HitLocation)) //如果执行不了死不掉,说明已经死了

  return false;

  

  if(AntController(Kill)!=none)

  AntController(Kill).AddToScore(ScorePoint); //首先要给controller加分

  SpawnManaToken(); //生成ManaToken金币



  LifeSpan=4; //给几秒的尸体存在时间然后干掉尸体

  SetTimer(3,false,'PlayDestructionEffects');
  return true;     //挂了
}

 接下来就是看里面的SpawnManaToken了

有几个变量得要声明:

var float Magnitude;     //一会要给生成的金币产生一个往外飞和自身转动的效果,这是他的权重数值

var float GoblinTokenNum; //这种类型的怪物生成多少个金币



function SpawnManaToken()

{

  local AntManaToken Tokens;

  local int i;

  for(i=0;i<GoblinTokenNum;i++)

  {
      Tokens=Spawn(AntManaTokenTemplate.class,,,Location+90*VRand(),RotRand(),AntManaTokenTemplate,true); //随机的方向和位置产生

      Tokens.StaticMeshComponent.AddImpulse(VRand()*Magnitude,,,true);

      Tokens.StaticMeshComponent.AddTorque(VRand()*Magnitude);
  }  


}

PlayDestructionEffects是一种粒子效果表演

var bool ScaleDownByDestruction;    
function PlayDestructionEffects()

{
  ScaleDownByDestruction=true;

  if(DeathEffectTemplate!=none)

  Spawn(DeathEffectTemplate.class,,,Location,Rotation,DeathEffectTemplate,true);
}


 发现有一个bool变量ScaleDownByDestruction,该变量将会促使Dying状态的Tick函数做出生化危机4中敌人尸体逐渐变小的效果

state Dying

{

  event Tick(float DeltaTime)

  {

    super.Tick(DeltaTime);

    if(ScaleDownByDestruction)

    {

      SetDrawScale(FMAX(DrawScale-DeltaTime,0.01));

      Mesh.SetTranslation(OriginalTraslation/DrawScale); //OriginalTranslation=Mesh.Translation; 在PostBeginPlay中赋予
    }
  }

}

SetDrawScale和SetTranslation都是Actor类提供的,非常有用,DrawScale是Actor的一个属性。

另外值得一提的是,如果在子类中要实现一些函数在这里可以进行一些声明

event StartedAttack();

event EndedAttack();

function vector GetMeleeSwingLocation(); //获取近战插槽攻击位置



 

原文地址:https://www.cnblogs.com/NEOCSL/p/2367411.html