245 lines
5.5 KiB
C#
245 lines
5.5 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class FlyCabway : MonoBehaviour
|
|
{
|
|
public float speedCruise;
|
|
|
|
public float speedTakeoffLanding;
|
|
|
|
public int WaitingTime;
|
|
|
|
public Transform[] Waypoint;
|
|
|
|
private float distanceLanding;
|
|
|
|
private float speed;
|
|
|
|
private Transform Goal;
|
|
|
|
private Vector3 velocity;
|
|
|
|
private float distanceNextWaypoint;
|
|
|
|
private int indexWaypoint;
|
|
|
|
private Transform[] WaypointInternal;
|
|
|
|
private int secArray;
|
|
|
|
private Quaternion rot;
|
|
|
|
private bool execMoveTo;
|
|
|
|
private string phase;
|
|
|
|
private int stopTime;
|
|
|
|
private float distanceFromWaypoint;
|
|
|
|
private AudioSource audioSource;
|
|
|
|
private Animator anim;
|
|
|
|
private Transform colliderWall1;
|
|
|
|
private Transform colliderWall2;
|
|
|
|
private Transform colliderWall3;
|
|
|
|
private Transform colliderWall4;
|
|
|
|
private Transform colliderWall5;
|
|
|
|
private int round;
|
|
|
|
private bool canCallFunction = true;
|
|
|
|
private void Start()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
anim = base.transform.GetComponentInChildren<Animator>();
|
|
phase = "DEFAULT";
|
|
execMoveTo = true;
|
|
distanceFromWaypoint = 1f;
|
|
distanceLanding = 0.1f;
|
|
int num = Waypoint.Length * 2;
|
|
int num2 = Waypoint.Length - 1;
|
|
int num3 = num + num2;
|
|
WaypointInternal = new Transform[num3];
|
|
Goal = Waypoint[0];
|
|
for (int i = 0; i <= Waypoint.Length; i++)
|
|
{
|
|
if (i % 2 == 0)
|
|
{
|
|
if (i > 0)
|
|
{
|
|
if (secArray == WaypointInternal.Length)
|
|
{
|
|
break;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i].transform.Find("TakeOff");
|
|
secArray++;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i];
|
|
secArray++;
|
|
if (secArray == WaypointInternal.Length)
|
|
{
|
|
break;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i].transform.Find("TakeOff");
|
|
}
|
|
if (i % 2 == 1)
|
|
{
|
|
secArray++;
|
|
if (secArray == WaypointInternal.Length)
|
|
{
|
|
break;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i].transform.Find("TakeOff");
|
|
secArray++;
|
|
if (secArray == WaypointInternal.Length)
|
|
{
|
|
break;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i];
|
|
secArray++;
|
|
if (secArray == WaypointInternal.Length)
|
|
{
|
|
break;
|
|
}
|
|
WaypointInternal[secArray] = Waypoint[i].transform.Find("TakeOff");
|
|
secArray++;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Vector3 position = base.transform.position;
|
|
distanceNextWaypoint = Vector3.Distance(Goal.position, base.transform.position);
|
|
if (phase != null && phase.StartsWith("LANDING") && distanceNextWaypoint <= distanceLanding)
|
|
{
|
|
stopTime = WaitingTime;
|
|
}
|
|
switch (phase)
|
|
{
|
|
case "LANDING":
|
|
audioSource.pitch -= 0.001f;
|
|
anim.speed -= 0.001f;
|
|
break;
|
|
case "CRUISE":
|
|
audioSource.pitch = 1f;
|
|
anim.speed = 1f;
|
|
break;
|
|
case "TAKEOFF":
|
|
audioSource.pitch += 0.004f;
|
|
anim.speed += 0.001f;
|
|
break;
|
|
}
|
|
if (distanceNextWaypoint <= distanceFromWaypoint)
|
|
{
|
|
indexWaypoint++;
|
|
if (WaypointInternal.Length < indexWaypoint)
|
|
{
|
|
round++;
|
|
indexWaypoint = 1;
|
|
}
|
|
phase = GetPhase(indexWaypoint);
|
|
switch (phase)
|
|
{
|
|
case "LANDING":
|
|
speed = speedTakeoffLanding;
|
|
distanceFromWaypoint = 0f;
|
|
break;
|
|
case "CRUISE":
|
|
speed = speedCruise;
|
|
distanceFromWaypoint = 0f;
|
|
stopTime = 0;
|
|
break;
|
|
case "TAKEOFF":
|
|
speed = speedTakeoffLanding;
|
|
distanceFromWaypoint = 1f;
|
|
stopTime = 0;
|
|
colliderWall1 = base.transform.Find("colliderWall1");
|
|
colliderWall2 = base.transform.Find("colliderWall2");
|
|
colliderWall3 = base.transform.Find("colliderWall3");
|
|
colliderWall4 = base.transform.Find("colliderWall4");
|
|
colliderWall5 = base.transform.Find("colliderWall5");
|
|
colliderWall1.gameObject.SetActive(true);
|
|
colliderWall2.gameObject.SetActive(true);
|
|
colliderWall3.gameObject.SetActive(true);
|
|
colliderWall4.gameObject.SetActive(true);
|
|
colliderWall5.gameObject.SetActive(true);
|
|
break;
|
|
}
|
|
if (indexWaypoint < WaypointInternal.Length && indexWaypoint >= 0)
|
|
{
|
|
Goal = WaypointInternal[indexWaypoint];
|
|
}
|
|
}
|
|
velocity = Goal.position - position;
|
|
velocity *= speed;
|
|
if (canCallFunction)
|
|
{
|
|
StartCoroutine(MoveTo(execMoveTo));
|
|
}
|
|
}
|
|
|
|
private string GetPhase(int actualIndex)
|
|
{
|
|
if (round == 1)
|
|
{
|
|
round = 0;
|
|
return "CRUISE";
|
|
}
|
|
int num = actualIndex - 1;
|
|
if (actualIndex == WaypointInternal.Length)
|
|
{
|
|
return "DEFAULT";
|
|
}
|
|
if (num < 0)
|
|
{
|
|
return "DEFAULT";
|
|
}
|
|
string text = WaypointInternal[actualIndex].name;
|
|
string text2 = WaypointInternal[num].name;
|
|
if (text.StartsWith("Sta") && text2.StartsWith("TakeOff"))
|
|
{
|
|
return "LANDING";
|
|
}
|
|
if (text.StartsWith("Take") && text2.StartsWith("TakeOff"))
|
|
{
|
|
return "CRUISE";
|
|
}
|
|
if (text.StartsWith("Take") && text2.StartsWith("Sta"))
|
|
{
|
|
return "TAKEOFF";
|
|
}
|
|
return "ERROR";
|
|
}
|
|
|
|
private IEnumerator MoveTo(bool exec)
|
|
{
|
|
canCallFunction = false;
|
|
if (stopTime > 0)
|
|
{
|
|
colliderWall1 = base.transform.Find("colliderWall1");
|
|
colliderWall2 = base.transform.Find("colliderWall2");
|
|
colliderWall3 = base.transform.Find("colliderWall3");
|
|
colliderWall4 = base.transform.Find("colliderWall4");
|
|
colliderWall5 = base.transform.Find("colliderWall5");
|
|
colliderWall1.gameObject.SetActive(false);
|
|
colliderWall2.gameObject.SetActive(false);
|
|
colliderWall3.gameObject.SetActive(false);
|
|
colliderWall4.gameObject.SetActive(false);
|
|
colliderWall5.gameObject.SetActive(false);
|
|
}
|
|
yield return new WaitForSeconds(stopTime / 3);
|
|
canCallFunction = true;
|
|
float step = speed * Time.deltaTime;
|
|
base.transform.position = Vector3.MoveTowards(base.transform.position, Goal.position, step);
|
|
stopTime = 0;
|
|
}
|
|
}
|