Chicken的代码解剖:6 PlayerController

  我知道研究别人的工程代码在前期是非常恼人的,不提供UML更是如此。因为你不知道当前看到这个函数的声明处和调用地方,唯一的方法就是系统的熟悉你看到的每一段代码。这可真真应证了万事开头难那句话。

在PlayerController中有很多函数。

  在这里最重要的问题之一便是处理触屏操作。先进行一些该方面的学习。

var MobileInputZone GamePlayZone;
var MobilePlayerInput myInput;

 1. MobilePlayerInput是移动设备上从玩家获得输入的核心。他能从屏幕上或感应上获得输入,然后通过访问属性和函数或者代理OnProcessTouch来提供这些输入的访问权。

   MobilePlayerInput定义为Within(GamePlayerController)意味着他能直接或间接继承GamePlayerController的类使用。在DefaultProperty中将MobilePlayerInput分配给InputClass。

 2.   MobileInputZone可以从用户获得触摸输入转换为UE3中的传统操作。

     在这里面能定义滑块等操作,这是替换摇杆的绝佳地方。

 3.   每个输入区域都在DefaultGame.ini的配置文件中。

  在MobilePlayerInput类中有一个OnInputTouch()代理,正是这个代理来处理触摸信息。如果我们写了一个函数PickActor用于处理触摸事件,那么得要有OnInputTouch这个代理将触摸信息传递到PickActor中。可以为这个代理分配一个函数HandleInputTouch,代理的概念简单理解可以将函数像变量一样赋值。例如上边的定义。

event InitInputSystem()
{
    super.InitInputSystem();
    
    MPI=MobilePlayerInput(PlayerInput);

//代理赋值 MPI.OnInputTouch
=HandleInputTouch; LocalPlayer(Player).ViewportClient.GetViewportSize(ViewportSize); }

  OnInputTouch的函数我们自己写,但是参数和HandleInputTouch是一样的。主要是将TouchLocation传递给物件PickActor,庆幸的是很容易。

PickedActor = PickActor(TouchLocation);
         
         //Check if actor is touchable and set it as selected; clear current selected if not
         if(ITouchable(PickedActor) != none)
         {
            SelectedActor = PickedActor;
         }

   我们在PlayerController中进行触屏操作的初始化。

event InitInputSystem()
{
      super.InitInputSystem();
      SetupZones();
}



function SetupZones()
{
   local vector2D ViewportSize;
   myInput=MobilePlayerInput(PlayerInput);

   GamePlayZone=myInput.FindZone("MainView");
   if(myInput!=none&&WorldInfo.GRI.GameClass!=none)
  {
     LocalPlayer(Player).ViewPortClient.GetViewPortSize(ViewportSize);
     if(GamePlayZone!=none)
    {
       GamePlayZone.SizeX=ViewportSize.X;
       GamePlayZone.SizeY=ViewportSize.Y;
     
       GamePlayZone.ActiveSizeX=GameplayZone.SizeX;
       GamePlayZone.ActiveSizeY=GameplayZone.SizeY;

       GameplayZone.OnProcessInputDelegate=ProcessInput;
    }
  }
}

  

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