Chicken的代码解剖 :4 ChickenPawn

  该类继承了GamePawn意味着简化,对于iOS的性能是最好不过了。

class ChickenPawn extends GamePawn implements(Interface_DraggableItem);

  实现了一个接口。

enum PawnType
{
 CHICKEN_PEN,
 ROOSTER_PEN,
 FOX_PEN  
};
var PawnType myType;

  老规矩,从活体编译器开始看看代码的执行。

event PostBeginPlay()
{
 super.PostBeginPlay();

 //生成一个controller并且该controller占有他
 if(Controller==none)
 SpawnDefaultController();

 //调用一个动画状态
 SetAnimState(STATE_MOVEMENT);

 //设置碰撞
 SetCollisionType(COLLIDE_TouchAll);

 //0.3s后重新设置碰撞,为什么呢。后面将其碰撞设为COLLIDE_BlockAll,因为才生成的生物害怕和别的撞在一起。给一定的时间Bump
 SetTimer(0.3f,false,'ResetCollision');
 //该函数只不过在这个类中声明,后边会定义。目前不去看
 BeginTimeOut();
}

  进入动画树状态SetAnimState(STATE_MOVEMENT);让我们更清晰的看看他们的动画树,无论是狐狸,小鸡和公鸡都是这种动画树。    

PrimaryBlender是一个AnimNodeBlendList在其NodeName中命名其为PrimaryBlender。在使用该节点前先进行初始化。

//初始化节点
simulated function CacheAnimNodes()
{
 if(PrimaryBlender==none)
 PrimaryBlender==AnimNodeBlendList(Mesh.FindAnimNode('PrimaryBlender'));
}

接下来就是动画的主要执行了,非常的一目了然

//该节点应该可以实现持有枪支的方式
function SetAnimState(AnimState newAnimState)
{
    local int newActiveChild;

    if(PrimaryBlender==none)
    CacheAnimNodes();

    switch(newAnimState)
    {
     case  STATE_CARRY:
     //此时将BlendList的newActiveChild的节点赋予
     newActiveChild=PrimaryBlender.Children.Find('Name','Carry');
     break;

     case STATE_DRAGGED:
     newActiveChild=PrimaryBlendr.Children.Find('Name','Drag');
     break;

     default:
     //传统情况是运动状态
     newActiveChild=PrimaryBlender.Children.Find('Name','Movement');
    }
    PrimaryBlender.SetActiveChild(newActiveChild,0.1f);
}

 我在pawn中注意了一个变量,然后再UnCodeX中队该变量进行了搜索,现在分析一下该变量的运用地方。包括其他Controller中

var Controller TargetedBy;

下面有一个使用该变量的函数,随后我们看看该函数都在哪些地方被调用。

function SetTargeted(Controller newTargeter)
{
 TargetedBy=newTargeter;
}

想一想,好像在狐狸的controller中我们用到过。以及什么地方调用该函数。好吧我回忆到在ChickenAIController_Fox中使用function bool FindPrey()在这里面

回头查查这个函数,遍历了所有的actor然后查找距离最近的一个ChickenPawn选为目标,然后将其controller设为狐狸,这样他将被狐狸的controller所控制,造成的假象是鸡被狐狸叼着走。

if(currentBestTarget!=none)
{
 ChickenPawn(currentBestTarget).SetTargeted(self);
}

后边还有一段在HuntPrey的状态EndState

ChickenPawn(currentPrey)!=none

咱们下节ChickenPawn_Fox和ChickenPawn_Chicken见

  

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