Advertisement
EagleOwle

DeploedShipController

Apr 14th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DeploedShipController : MonoBehaviour
  5. {
  6.     public bool enableDeploed;
  7.     public TeamInGame team;
  8.     public int currentBot;
  9.     public int maxBot;
  10.     public bool autoCreateBot;
  11.     public int[] queueBot;
  12.     public int nextBotType;
  13.     public GameObject[] spawnObjPrefab;
  14.     public bool inDeploedPosition;
  15.  
  16.     public Vector3 deploedPosition;
  17.     public float speedMove;
  18.     public float speedRotate;
  19.     public float flyZoneRadius;
  20.  
  21.     public float maxDistanceToDeploed;
  22.     public float maxDistanceToStartDeploed;
  23.  
  24.     public float deploedRadius;
  25.     public float deploedHeight;
  26.     public float flyHeight;
  27.  
  28.     private GameObject deploedSprite;
  29.     private GameObject deploedEffect;
  30.     //private CaptureState captureState;
  31.     private Animator animator;
  32.     private Vector3 targetPosition;
  33.     private Quaternion direction;
  34.     private Vector2 shipPosition;
  35.     private Vector2 endPosition;
  36.     private Vector3 startPosition;
  37.     private Vector3 endPisition;
  38.     public float distanceToDeploed;
  39.     //private float distanceToGround;
  40.     public float timeCheck;
  41.  
  42.     public LayerMask layerMask;
  43.  
  44.     //Для дебага//
  45.     public TextMesh message;
  46.     public GameObject player;
  47.  
  48.     public void StartGame (Vector3 startPosition)
  49.     {
  50.         player = GameObject.Find("Player");
  51.  
  52.         deploedEffect = transform.GetChild(0).gameObject;
  53.         deploedSprite = deploedEffect.transform.GetChild(0).gameObject;
  54.        
  55.         deploedEffect.SetActive(false);
  56.         inDeploedPosition = true;
  57.         queueBot = new int[maxBot];
  58.         transform.parent = null;
  59.         transform.position = startPosition + Vector3.left * (flyZoneRadius) + Vector3.up * flyHeight;
  60.         deploedPosition = startPosition;
  61.         message.gameObject.SetActive(false);
  62.         enableDeploed = true;
  63.  
  64.         //if ()
  65.     }
  66.    
  67.     void LateUpdate ()
  68.     {
  69.         if (team == GlobalPlayerConfig.playerFriendTeam)
  70.         {
  71.             //autoCreateBot ==
  72.         }
  73.         else
  74.         {
  75.             autoCreateBot = true;
  76.         }
  77.  
  78.         DirectionFly();
  79.         Fly();
  80.     }
  81.  
  82.     void DirectionFly()
  83.     {
  84.         shipPosition = new Vector2(transform.position.x, transform.position.z);
  85.         endPosition = new Vector2(deploedPosition.x, deploedPosition.z);
  86.         distanceToDeploed = Vector2.Distance(shipPosition, endPosition);
  87.  
  88.         Debug.DrawLine(shipPosition, endPosition, Color.green);
  89.         Debug.DrawLine(transform.position, targetPosition);
  90.         Debug.DrawLine(deploedPosition, deploedPosition + Vector3.up * deploedHeight, Color.red);
  91.  
  92.         if (distanceToDeploed >= flyZoneRadius)
  93.         {
  94.             targetPosition = deploedPosition + Vector3.up * deploedHeight;
  95.             message.text = "Цель десантирования";
  96.         }
  97.         else
  98.         {
  99.             if (distanceToDeploed < maxDistanceToDeploed)
  100.             {
  101.                 deploedEffect.SetActive(true);
  102.                 message.text = "Снижаюсь";
  103.  
  104.                 if (distanceToDeploed < maxDistanceToStartDeploed)
  105.                 {
  106.                     if (inDeploedPosition == true)
  107.                     {
  108.                         message.text = "На позиции десантирования";
  109.  
  110.                         if (enableDeploed == true)
  111.                         {
  112.                             message.text = "Начинаю десантирование";
  113.                             // -= Создание ботов из очереди =- //
  114.                             StartCoroutine(DeploedBot(timeCheck));
  115.  
  116.                         }
  117.  
  118.                         inDeploedPosition = false;
  119.                     }
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 if (deploedEffect.activeSelf == true)
  125.                 {
  126.                     message.text = "Прекращаю десантирование";
  127.  
  128.                     deploedEffect.SetActive(false);
  129.                     inDeploedPosition = true;
  130.  
  131.                     if (transform.position.y != flyHeight)
  132.                     {
  133.                         message.text = "Набираю высоту";
  134.                         targetPosition = transform.TransformPoint(Vector3.forward * flyZoneRadius);
  135.                         targetPosition = new Vector3(targetPosition.x, flyHeight, targetPosition.z);
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.  
  141.     }
  142.  
  143.     void Fly()
  144.     {
  145.         if (targetPosition - transform.position != Vector3.zero)
  146.         {
  147.             direction = Quaternion.LookRotation(targetPosition - transform.position);
  148.             transform.rotation = Quaternion.RotateTowards(transform.rotation, direction, speedRotate * Time.deltaTime);
  149.            
  150.         }
  151.  
  152.         transform.position += transform.forward * speedMove * Time.deltaTime;
  153.  
  154.         if ( deploedEffect.activeSelf == true)
  155.         {
  156.             deploedSprite.transform.position = new Vector3(transform.position.x, Terrain.activeTerrain.SampleHeight(transform.position) + 0.1f, transform.position.z);
  157.             deploedSprite.transform.eulerAngles = new Vector3(-90, 0,0);
  158.             //deploedSprite.transform.LookAt(Vector3.up);
  159.         }
  160.  
  161.         message.transform.LookAt(player.transform.position);
  162.         message.characterSize = 1 + Vector3.Distance(player.transform.position, transform.position)/20;
  163.     }
  164.  
  165.     private IEnumerator DeploedBot(float time)
  166.     {
  167.         // -=Авто заполнение очереди =- //
  168.         if (autoCreateBot == true)
  169.         {
  170.             if (currentBot < maxBot)
  171.             {
  172.                 if (queueBot.Length > 0 && queueBot[0] == 0)
  173.                 {
  174.                     nextBotType = Random.Range(1, spawnObjPrefab.Length + 1);
  175.                     queueBot[0] = nextBotType;
  176.                 }
  177.             }
  178.         }
  179.  
  180.         // -= Создание ботов из очереди =- //
  181.         if (queueBot.Length > 0 && queueBot[0] > 0)
  182.         {
  183.             startPosition = transform.position;
  184.             endPisition = new Vector3(startPosition.x, Terrain.activeTerrain.SampleHeight(startPosition) + 0.1f, startPosition.z);
  185.             //distanceToGround = Vector3.Distance(startPosition, endPisition);
  186.  
  187.  
  188.             if (CheckOverlapSphere(endPisition) == false)
  189.             {
  190.                 Debug.DrawLine(startPosition, endPisition, Color.yellow, 1);
  191.                 GameObject deploedBotObj = Instantiate(spawnObjPrefab[queueBot[0] - 1], transform.position, Quaternion.identity) as GameObject;
  192.  
  193.                 deploedBotObj.GetComponent<BotControllerScr>().team = team;
  194.                 deploedBotObj.GetComponent<BotControllerScr>().SM_botState = SM_BotState.Deploed;
  195.                 currentBot++;
  196.                 ClearQueue();
  197.             }
  198.             else
  199.             {
  200.                 Debug.DrawLine(startPosition, endPisition, Color.red, 1);
  201.             }
  202.         }
  203.  
  204.         yield return new WaitForSeconds(time);
  205.  
  206.         if (GlobalGameController.playerInGame == true)
  207.         {
  208.             if (distanceToDeploed < maxDistanceToStartDeploed)
  209.             {
  210.                 StartCoroutine(DeploedBot(time));
  211.             }
  212.         }
  213.     }
  214.  
  215.     bool checkRayCollision(Vector3 myPosition, Vector3 targetPosition, float distanceToGround)
  216.     {
  217.         bool collision = false;
  218.         Ray rayCollision = new Ray(targetPosition, myPosition - targetPosition);
  219.         RaycastHit hitCollision;
  220.  
  221.         if (Physics.Raycast(rayCollision, out hitCollision, distanceToGround, layerMask))
  222.         {
  223.             collision = true;
  224.         }
  225.  
  226.         return collision;
  227.     }
  228.  
  229.     bool CheckOverlapSphere(Vector3 targetPosition)
  230.     {
  231.         bool collision = false;
  232.  
  233.         Vector3 center = targetPosition + Vector3.up * 2f;
  234.         float radius = 1.5f;
  235.         Collider[] hitColliders = Physics.OverlapSphere(center, radius, layerMask);
  236.  
  237.         if (hitColliders.Length > 0)
  238.         {
  239.             collision = true;
  240.         }
  241.  
  242.         return collision;
  243.     }
  244.  
  245.     void ClearQueue()
  246.     {
  247.         for (int i = 0; i < queueBot.Length; i++)
  248.         {
  249.             if (i + 1 < queueBot.Length)
  250.             {
  251.                 queueBot[i] = queueBot[i + 1];
  252.             }
  253.             else
  254.             {
  255.  
  256.                 queueBot[i] = 0;
  257.             }
  258.         }
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement