Advertisement
EagleOwle

ModulRotateController

Sep 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. public class ModulRotateController : ModulBase
  7. {
  8.     [Header("Обьекты вращения")]
  9.     [Tooltip("Обьект вращения по оси Х")]
  10.     public Transform transformXRotation;
  11.  
  12.     [Tooltip("Обьект вращения по оси Y")]
  13.     public Transform transformYRotation;
  14.  
  15.     public ConfigController config;
  16.  
  17.     private float tempAngle = 0;
  18.     private float currentSpeedRotation;
  19.     private Vector3 _gunTarget;
  20.     public Vector3 GunTarget
  21.     {
  22.         get { return _gunTarget; }
  23.         set { _gunTarget = value; }
  24.     }
  25.  
  26.     protected override void Start()
  27.     {
  28.         base.Start();
  29.         CreateNextModul(config);
  30.     }
  31.  
  32.     protected override void LoadConfig()
  33.     {
  34.         switch (botController.classBot)
  35.         {
  36.             case BotControllerBase.ClassBot.StormTrooper:
  37.                 {
  38.                     ConfigListSTModul tmp = Resources.Load("Config/ConfigListSTModul") as ConfigListSTModul;
  39.                     if (modulType == ConfigModulBase.ModulType.ControllerL)
  40.                     {
  41.                         config = tmp.configControllerL[modulLvl];
  42.                         return;
  43.                     }
  44.  
  45.                     if (modulType == ConfigModulBase.ModulType.ControllerR)
  46.                     {
  47.                         config = tmp.configControllerR[modulLvl];
  48.                         return;
  49.                     };
  50.                     break;
  51.                 }
  52.  
  53.             case BotControllerBase.ClassBot.Scout:
  54.                 {
  55.                     ConfigListScoutModul tmp = Resources.Load("Config/ConfigListScoutModul") as ConfigListScoutModul;
  56.                     if (modulType == ConfigModulBase.ModulType.ControllerM)
  57.                     {
  58.                         config = tmp.configController[modulLvl];
  59.                         return;
  60.                     }
  61.                     break;
  62.                 }
  63.         }
  64.     }
  65.  
  66.     public override ModulRotateController GetModulRotateController()
  67.     {
  68.         return this;
  69.     }
  70.  
  71.     public override Socket[] GetSocketArray()
  72.     {
  73.         return config.socketArray;
  74.     }
  75.  
  76.     public override Socket GetCurrentSocket()
  77.     {
  78.         return null;
  79.     }
  80.  
  81.     public override GameObject GetUIPrefab()
  82.     {
  83.         return config.UIPrefab;
  84.     }
  85.  
  86.     public override ConfigModulBase.ModulType GetModulType()
  87.     {
  88.         return config.modulType;
  89.     }
  90.  
  91.  
  92.     protected override void OnEnable()
  93.     {
  94.         base.OnEnable();
  95.         GunTarget = transform.TransformPoint(Vector3.forward * 100);
  96.     }
  97.  
  98.     private void LateUpdate()
  99.     {
  100.         if (ModulStatus == Status.On)
  101.         {
  102.             switch (config.axisRotation)
  103.             {
  104.                 case AxisRotation.AxisX:
  105.                     {
  106.                         if (transformXRotation != null)
  107.                         {
  108.                             RotationAxisX(transformXRotation, GetDirection(transformXRotation), SpeedRotation());
  109.                         }
  110.                         break;
  111.                     }
  112.  
  113.                 case AxisRotation.AxisY:
  114.                     {
  115.                         if (transformYRotation != null)
  116.                         {
  117.                             RotationAxisY(transformYRotation, GetDirection(transformYRotation), SpeedRotation());
  118.                         }
  119.                         break;
  120.                     }
  121.  
  122.                 case AxisRotation.AxisXY:
  123.                     {
  124.                         RotationAxisX(transformXRotation, GetDirection(transformXRotation), SpeedRotation());
  125.                         RotationAxisY(transformYRotation, GetDirection(transformYRotation), SpeedRotation());
  126.                         break;
  127.                     }
  128.             }
  129.  
  130.             botController.CmdSetLookDirection(config.modulType, GunTarget, config.speedRotation);
  131.             return;
  132.         }
  133.  
  134.         /*
  135.         if (transformXRotation != null)
  136.         {
  137.             RotationAxisX(transformXRotation, GetDirection(transformXRotation), currentSpeedRotation);
  138.         }
  139.  
  140.         if (transformYRotation != null)
  141.         {
  142.             RotationAxisY(transformYRotation, GetDirection(transformYRotation), currentSpeedRotation);
  143.         }
  144.         */
  145.     }
  146.  
  147.     private float SpeedRotation()
  148.     {
  149.         return config.speedRotation * Time.deltaTime;
  150.     }
  151.  
  152.     private Quaternion GetDirection(Transform tempTransform)
  153.     {
  154.         //вектор, куда нужно вращать
  155.         //Debug.DrawLine(GunTarget, tempTransform.position, Color.cyan);
  156.         return Quaternion.LookRotation(GunTarget - tempTransform.position);
  157.     }
  158.  
  159.     public override void SetLookDirection(Vector3 pointTarget)
  160.     {
  161.         GunTarget = pointTarget;
  162.     }
  163.  
  164.     public override void SetLookDirection(Vector3 valueDirection, float valueSpeed)
  165.     {
  166.         GunTarget = valueDirection;
  167.         currentSpeedRotation = valueSpeed;
  168.     }
  169.  
  170.     private void RotationAxisY(Transform rotateTransform, Quaternion direction, float speed)
  171.     {
  172.         rotateTransform.rotation = Quaternion.RotateTowards(rotateTransform.rotation, direction, speed);
  173.  
  174.         tempAngle = rotateTransform.localEulerAngles.y;
  175.  
  176.         if (config.maxAngleY > 0)
  177.         {
  178.  
  179.             if (rotateTransform.localEulerAngles.y > 180f)
  180.             {
  181.                 //если значение Y больше 180, то используем ограничение между 360 и максимальним ограничением
  182.                 tempAngle = Mathf.Clamp(tempAngle, 360f - config.maxAngleY, 360f);
  183.             }
  184.             else
  185.             {
  186.                 if (rotateTransform.localEulerAngles.y < 180f)
  187.                 {
  188.                     //если значение Y меньше 180, то ограничение используем между 0 и минимальным ограничением
  189.                     tempAngle = Mathf.Clamp(tempAngle, 0f, config.maxAngleY);
  190.                 }
  191.             }
  192.         }
  193.  
  194.         transformYRotation.localEulerAngles = new Vector3(0, tempAngle, 0);
  195.     }
  196.  
  197.     private void RotationAxisX(Transform rotateTransform, Quaternion direction, float speed)
  198.     {
  199.         rotateTransform.rotation = Quaternion.RotateTowards(rotateTransform.rotation, direction, speed);
  200.  
  201.         tempAngle = rotateTransform.localEulerAngles.x;
  202.  
  203.         if (config.maxAngleX > 0)
  204.         {
  205.             if (rotateTransform.localEulerAngles.x > 180f)
  206.             {
  207.                 tempAngle = Mathf.Clamp(tempAngle, 360f - config.maxAngleX, 360f);
  208.             }
  209.             else
  210.             {
  211.                 if (rotateTransform.localEulerAngles.x < 180f)
  212.                 {
  213.                     tempAngle = Mathf.Clamp(tempAngle, 0f, config.maxAngleX);
  214.                 }
  215.             }
  216.         }
  217.  
  218.         transformXRotation.localEulerAngles = new Vector3(tempAngle, 0, 0);
  219.     }
  220.  
  221.    
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement