Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DeploedShipController : MonoBehaviour
- {
- public bool enableDeploed;
- public TeamInGame team;
- public int currentBot;
- public int maxBot;
- public bool autoCreateBot;
- public int[] queueBot;
- public int nextBotType;
- public GameObject[] spawnObjPrefab;
- public bool inDeploedPosition;
- public Vector3 deploedPosition;
- public float speedMove;
- public float speedRotate;
- public float flyZoneRadius;
- public float maxDistanceToDeploed;
- public float maxDistanceToStartDeploed;
- public float deploedRadius;
- public float deploedHeight;
- public float flyHeight;
- private GameObject deploedSprite;
- private GameObject deploedEffect;
- //private CaptureState captureState;
- private Animator animator;
- private Vector3 targetPosition;
- private Quaternion direction;
- private Vector2 shipPosition;
- private Vector2 endPosition;
- private Vector3 startPosition;
- private Vector3 endPisition;
- public float distanceToDeploed;
- //private float distanceToGround;
- public float timeCheck;
- public LayerMask layerMask;
- //Для дебага//
- public TextMesh message;
- public GameObject player;
- public void StartGame (Vector3 startPosition)
- {
- player = GameObject.Find("Player");
- deploedEffect = transform.GetChild(0).gameObject;
- deploedSprite = deploedEffect.transform.GetChild(0).gameObject;
- deploedEffect.SetActive(false);
- inDeploedPosition = true;
- queueBot = new int[maxBot];
- transform.parent = null;
- transform.position = startPosition + Vector3.left * (flyZoneRadius) + Vector3.up * flyHeight;
- deploedPosition = startPosition;
- message.gameObject.SetActive(false);
- enableDeploed = true;
- //if ()
- }
- void LateUpdate ()
- {
- if (team == GlobalPlayerConfig.playerFriendTeam)
- {
- //autoCreateBot ==
- }
- else
- {
- autoCreateBot = true;
- }
- DirectionFly();
- Fly();
- }
- void DirectionFly()
- {
- shipPosition = new Vector2(transform.position.x, transform.position.z);
- endPosition = new Vector2(deploedPosition.x, deploedPosition.z);
- distanceToDeploed = Vector2.Distance(shipPosition, endPosition);
- Debug.DrawLine(shipPosition, endPosition, Color.green);
- Debug.DrawLine(transform.position, targetPosition);
- Debug.DrawLine(deploedPosition, deploedPosition + Vector3.up * deploedHeight, Color.red);
- if (distanceToDeploed >= flyZoneRadius)
- {
- targetPosition = deploedPosition + Vector3.up * deploedHeight;
- message.text = "Цель десантирования";
- }
- else
- {
- if (distanceToDeploed < maxDistanceToDeploed)
- {
- deploedEffect.SetActive(true);
- message.text = "Снижаюсь";
- if (distanceToDeploed < maxDistanceToStartDeploed)
- {
- if (inDeploedPosition == true)
- {
- message.text = "На позиции десантирования";
- if (enableDeploed == true)
- {
- message.text = "Начинаю десантирование";
- // -= Создание ботов из очереди =- //
- StartCoroutine(DeploedBot(timeCheck));
- }
- inDeploedPosition = false;
- }
- }
- }
- else
- {
- if (deploedEffect.activeSelf == true)
- {
- message.text = "Прекращаю десантирование";
- deploedEffect.SetActive(false);
- inDeploedPosition = true;
- if (transform.position.y != flyHeight)
- {
- message.text = "Набираю высоту";
- targetPosition = transform.TransformPoint(Vector3.forward * flyZoneRadius);
- targetPosition = new Vector3(targetPosition.x, flyHeight, targetPosition.z);
- }
- }
- }
- }
- }
- void Fly()
- {
- if (targetPosition - transform.position != Vector3.zero)
- {
- direction = Quaternion.LookRotation(targetPosition - transform.position);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, direction, speedRotate * Time.deltaTime);
- }
- transform.position += transform.forward * speedMove * Time.deltaTime;
- if ( deploedEffect.activeSelf == true)
- {
- deploedSprite.transform.position = new Vector3(transform.position.x, Terrain.activeTerrain.SampleHeight(transform.position) + 0.1f, transform.position.z);
- deploedSprite.transform.eulerAngles = new Vector3(-90, 0,0);
- //deploedSprite.transform.LookAt(Vector3.up);
- }
- message.transform.LookAt(player.transform.position);
- message.characterSize = 1 + Vector3.Distance(player.transform.position, transform.position)/20;
- }
- private IEnumerator DeploedBot(float time)
- {
- // -=Авто заполнение очереди =- //
- if (autoCreateBot == true)
- {
- if (currentBot < maxBot)
- {
- if (queueBot.Length > 0 && queueBot[0] == 0)
- {
- nextBotType = Random.Range(1, spawnObjPrefab.Length + 1);
- queueBot[0] = nextBotType;
- }
- }
- }
- // -= Создание ботов из очереди =- //
- if (queueBot.Length > 0 && queueBot[0] > 0)
- {
- startPosition = transform.position;
- endPisition = new Vector3(startPosition.x, Terrain.activeTerrain.SampleHeight(startPosition) + 0.1f, startPosition.z);
- //distanceToGround = Vector3.Distance(startPosition, endPisition);
- if (CheckOverlapSphere(endPisition) == false)
- {
- Debug.DrawLine(startPosition, endPisition, Color.yellow, 1);
- GameObject deploedBotObj = Instantiate(spawnObjPrefab[queueBot[0] - 1], transform.position, Quaternion.identity) as GameObject;
- deploedBotObj.GetComponent<BotControllerScr>().team = team;
- deploedBotObj.GetComponent<BotControllerScr>().SM_botState = SM_BotState.Deploed;
- currentBot++;
- ClearQueue();
- }
- else
- {
- Debug.DrawLine(startPosition, endPisition, Color.red, 1);
- }
- }
- yield return new WaitForSeconds(time);
- if (GlobalGameController.playerInGame == true)
- {
- if (distanceToDeploed < maxDistanceToStartDeploed)
- {
- StartCoroutine(DeploedBot(time));
- }
- }
- }
- bool checkRayCollision(Vector3 myPosition, Vector3 targetPosition, float distanceToGround)
- {
- bool collision = false;
- Ray rayCollision = new Ray(targetPosition, myPosition - targetPosition);
- RaycastHit hitCollision;
- if (Physics.Raycast(rayCollision, out hitCollision, distanceToGround, layerMask))
- {
- collision = true;
- }
- return collision;
- }
- bool CheckOverlapSphere(Vector3 targetPosition)
- {
- bool collision = false;
- Vector3 center = targetPosition + Vector3.up * 2f;
- float radius = 1.5f;
- Collider[] hitColliders = Physics.OverlapSphere(center, radius, layerMask);
- if (hitColliders.Length > 0)
- {
- collision = true;
- }
- return collision;
- }
- void ClearQueue()
- {
- for (int i = 0; i < queueBot.Length; i++)
- {
- if (i + 1 < queueBot.Length)
- {
- queueBot[i] = queueBot[i + 1];
- }
- else
- {
- queueBot[i] = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement