Exemple #1
0
        void OnDrawGizmosSelected()
        {
            if (transform.childCount < 2)
            {
                return;
            }
            Vector3 current = transform.GetComponentInChildren <AIDestination>().transform.position;

            Gizmos.color = new Color(0.8f, 0.4f, 0);
            foreach (Transform item in transform)
            {
                if (!item.gameObject.activeSelf)
                {
                    continue;
                }
                AIDestination dest = item.GetComponent <AIDestination>();
                if (!dest)
                {
                    continue;
                }
                Gizmos.DrawLine(current, item.position);
                current = item.position;
            }
            if (closedCircut)
            {
                Gizmos.DrawLine(current, transform.GetComponentInChildren <AIDestination>().transform.position);
            }
        }
Exemple #2
0
 public void AddWaypoint(AIDestination waypoint, Vector3 localPosition, int index = -1)
 {
     waypoint.transform.parent = transform;
     if (index > -1)
     {
         waypoint.transform.SetSiblingIndex(index);
     }
     if (!waypoints.Contains(waypoint))
     {
         waypoints.Add(waypoint);
     }
     waypoint.transform.localPosition = localPosition;
 }
        void DestinationMoved(Event e)
        {
            AIDestination destination = (AIDestination)e.data;

            if (!destination)
            {
                return;
            }
            if (current != destination)
            {
                return;
            }
            brain.SetDestination(current.position);
        }
Exemple #4
0
        public AIDestination GetPreviousWaypoint(AIDestination current)
        {
            int idx = waypoints.FindIndex(item => item == current);

            if (idx < 0)
            {
                return(GetLastWaypoint());
            }
            if (idx - 1 < 0 && closedCircut)
            {
                return(GetLastWaypoint());
            }
            else if (idx - 1 < 0 && !closedCircut)
            {
                return(GetFirstWaypoint());
            }
            return(waypoints[idx - 1]);
        }
Exemple #5
0
        public AIDestination GetNextWaypoint(AIDestination current)
        {
            int idx = waypoints.FindIndex(item => item == current);

            if (idx < 0)
            {
                return(GetFirstWaypoint());
            }
            if (idx + 1 >= waypoints.Count && closedCircut)
            {
                return(GetFirstWaypoint());
            }
            else if (idx + 1 >= waypoints.Count && !closedCircut)
            {
                return(GetLastWaypoint());
            }
            return(waypoints[idx + 1]);
        }
        void NextWaypoint(Event e)
        {
            // If the new destination is the same as the current don't continue
            // If there is no waypoint group set don't continue
            if ((AIDestination)e.data != current || !waypointGroup)
            {
                return;
            }
            // Set an empty destination
            AIDestination destination = null;

            // If patrolling is enabled toggle directions if needed
            if (patrol)
            {
                if (direction == TraverseDirection.Forward && current == waypointGroup.GetLastWaypoint())
                {
                    direction = TraverseDirection.Backward;
                }
                else if (direction == TraverseDirection.Backward && current == waypointGroup.GetFirstWaypoint())
                {
                    direction = TraverseDirection.Forward;
                }
            }

            // If the direction is forward get the next waypoint
            if (direction == TraverseDirection.Forward)
            {
                destination = waypointGroup.GetNextWaypoint(current);
            }
            // If the direction is backward get the previous waypoint
            else if (direction == TraverseDirection.Backward)
            {
                destination = waypointGroup.GetPreviousWaypoint(current);
            }

            // If we have a destination set the new destination
            if (destination != null)
            {
                current = destination;
                brain.SetDestination(current.position);
            }
        }
Exemple #7
0
 public void DestroyWaypoint(AIDestination waypoint)
 {
     waypoints.Remove(waypoint);
     Destroy(waypoint.gameObject);
 }
Exemple #8
0
 public void RemoveWaypoint(AIDestination waypoint)
 {
     waypoints.Remove(waypoint);
     waypoint.transform.parent = transform.root;
 }
        void Start()
        {
            if (!waypointGroup)
            {
                return;
            }
            waypointGroup.events.AddEventListener("AIDestinationMoved", DestinationMoved);
            switch (startingWaypoint)
            {
            case StartType.First:
                current = waypointGroup.GetFirstWaypoint();
                break;

            case StartType.Last:
                current = waypointGroup.GetLastWaypoint();
                break;

            case StartType.Random:
                current = waypointGroup.GetRandomWaypoint();
                break;

            case StartType.RoundRobin:
                current = waypointGroup.GetNextWaypoint(roundRobinLastLocation);
                roundRobinLastLocation = current;
                break;

            case StartType.ClosestByAir:
                current = waypointGroup.GetClosestByAir(brain);
                break;

            case StartType.FurthestByAir:
                current = waypointGroup.GetFurthestByAir(brain);
                break;

            case StartType.ClosestByGround:
                current = waypointGroup.GetClosestByGround(brain);
                break;

            case StartType.FurthestByGround:
                current = waypointGroup.GetFurthestByGround(brain);
                break;

            case StartType.Weighted:
                current = waypointGroup.GetWeightedWaypoint();
                break;

            case StartType.LeastTraffic:
                current = waypointGroup.GetLeastTrafficWaypoint();
                break;

            case StartType.MostTraffic:
                current = waypointGroup.GetMostTrafficWaypoint();
                break;

            case StartType.Specific:
                if (specificStartLocation)
                {
                    current = specificStartLocation;
                }
                else
                {
                    current = waypointGroup.GetFirstWaypoint();
                }
                break;

            default:
                current = waypointGroup.GetFirstWaypoint();
                break;
            }
            brain.SetDestination(current.position);
        }