Exemple #1
0
    private void EvaluateLane()
    {
        CurrentIndex++; // index can equal laneData.Count so it can finish npc move IE
        if (CurrentIndex < LaneData.Count)
        {
            CurrentTarget = LaneData[CurrentIndex];
        }

        if (CurrentIndex == LaneData.Count)
        {
            if (WaypointLoop)
            {
                rb.MovePosition(InitPos);
                rb.MoveRotation(InitRot);
                InitNPC();
            }
            else
            {
                WaypointState = WaypointDriveState.Despawn;
                FixedUpdateManager.StopAllCoroutines();
                TriggerCoroutine = null;
                IdleCoroutine    = null;
                MoveCoroutine    = null;
            }
        }
        else
        {
            WaypointState = WaypointDriveState.Drive;
        }
    }
Exemple #2
0
 private void InitNPC()
 {
     Debug.Assert(LaneData != null);
     rb.isKinematic = true;
     controller.MainCollider.isTrigger = true;
     controller.ResetLights();
     currentSpeed       = 0f;
     rb.angularVelocity = Vector3.zero;
     rb.velocity        = Vector3.zero;
     CurrentIndex       = 0;
     CurrentLoopIndex   = 0;
     CurrentDeactivate  = LaneDeactivate[CurrentIndex];
     if (IdleCoroutine != null)
     {
         FixedUpdateManager.StopCoroutine(IdleCoroutine);
         IdleCoroutine = null;
     }
     if (MoveCoroutine != null)
     {
         FixedUpdateManager.StopCoroutine(MoveCoroutine);
         MoveCoroutine = null;
     }
     if (TriggerCoroutine != null)
     {
         FixedUpdateManager.StopCoroutine(TriggerCoroutine);
         TriggerCoroutine = null;
     }
     WaypointState = WaypointDriveState.Drive;
 }
Exemple #3
0
    private void EvaluateLane()
    {
        CurrentIndex++; // index can equal laneData.Count so it can finish npc move IE
        if (CurrentIndex < LaneData.Count)
        {
            CurrentTarget            = LaneData[CurrentIndex];
            controller.MovementSpeed = LaneSpeed[CurrentIndex];
            controller.currentSpeed  = LaneSpeed[CurrentIndex];
            controller.steerVector   = (CurrentTarget - controller.frontCenter.position).normalized;
        }

        if (CurrentIndex == LaneData.Count)
        {
            var api = ApiManager.Instance;
            if (WaypointLoop)
            {
                if (CurrentLoopIndex == 0 && api != null)
                {
                    api.AgentTraversedWaypoints(gameObject);
                }
                CurrentLoopIndex++;
                rb.MovePosition(InitPos);
                rb.MoveRotation(InitRot);
                InitNPC();
            }
            else
            {
                if (api != null)
                {
                    api.AgentTraversedWaypoints(gameObject);
                }
                WaypointState = WaypointDriveState.Despawn;
                if (TriggerCoroutine != null)
                {
                    FixedUpdateManager.StopCoroutine(TriggerCoroutine);
                }
                TriggerCoroutine = null;
                if (IdleCoroutine != null)
                {
                    FixedUpdateManager.StopCoroutine(IdleCoroutine);
                }
                IdleCoroutine = null;
                if (MoveCoroutine != null)
                {
                    FixedUpdateManager.StopCoroutine(MoveCoroutine);
                }
                MoveCoroutine = null;
            }
        }
        else
        {
            WaypointState = WaypointDriveState.Drive;
        }
    }
Exemple #4
0
    private void EvaluateLane()
    {
        CurrentIndex++; // index can equal laneData.Count so it can finish npc move IE
        if (CurrentIndex < LaneData.Count)
        {
            CurrentTarget            = LaneData[CurrentIndex];
            controller.MovementSpeed = LaneSpeed[CurrentIndex];
        }

        if (CurrentIndex == LaneData.Count)
        {
            var api = ApiManager.Instance;
            if (WaypointLoop)
            {
                if (CurrentLoopIndex == 0 && api != null)
                {
                    api.AgentTraversedWaypoints(gameObject);
                }
                CurrentLoopIndex++;
                rb.MovePosition(InitPos);
                rb.MoveRotation(InitRot);
                InitNPC();
            }
            else
            {
                if (api != null)
                {
                    api.AgentTraversedWaypoints(gameObject);
                }
                WaypointState = WaypointDriveState.Despawn;
                FixedUpdateManager.StopAllCoroutines();
                TriggerCoroutine = null;
                IdleCoroutine    = null;
                MoveCoroutine    = null;
            }
        }
        else
        {
            WaypointState = WaypointDriveState.Drive;
        }
    }
Exemple #5
0
    private IEnumerator NPCMoveIE()
    {
        if (CurrentIndex == 0)
        {
            // increment index since spawn is index = 0 with no passed params
            EvaluateLane();
        }

        if (CurrentIndex != 0)
        {
            var duration    = LaneTime[CurrentIndex] - LaneTime[CurrentIndex - 1];
            var elapsedTime = 0f;
            while (elapsedTime < duration)
            {
                var factor = elapsedTime / duration;
                var pose   = Vector3.Lerp(LaneData[CurrentIndex - 1], LaneData[CurrentIndex], factor);
                var rot    = Quaternion.Slerp(Quaternion.Euler(LaneAngle[CurrentIndex - 1]), Quaternion.Euler(LaneAngle[CurrentIndex]), factor);
                if (!float.IsNaN(pose.x))
                {
                    rb.MovePosition(pose);
                    rb.MoveRotation(rot);
                }
                elapsedTime += Mathf.Min(Time.fixedDeltaTime, duration - elapsedTime);
                yield return(new WaitForFixedUpdate());
            }
            rb.MovePosition(LaneData[CurrentIndex]);
            rb.MoveRotation(Quaternion.Euler(LaneAngle[CurrentIndex]));
        }

        if (CurrentIndex <= LaneData.Count - 1)
        {
            ApiManager.Instance?.AddWaypointReached(gameObject, CurrentIndex);

            // trigger
            if (LaneTriggerDistance[CurrentIndex] > 0)
            {
                WaypointState = WaypointDriveState.Trigger;
                yield return(TriggerCoroutine = FixedUpdateManager.StartCoroutine(NPCTriggerIE()));
            }

            // deactivate
            CurrentDeactivate = LaneDeactivate[CurrentIndex];

            // idle
            if (LaneIdle[CurrentIndex] > 0)
            {
                WaypointState = WaypointDriveState.Idle;
                yield return(IdleCoroutine = FixedUpdateManager.StartCoroutine(NPCIdleIE(LaneIdle[CurrentIndex], CurrentDeactivate)));
            }
            else if (LaneIdle[CurrentIndex] == -1 && CurrentDeactivate)
            {
                WaypointState = WaypointDriveState.Despawn;
                gameObject.SetActive(false);
                yield break;
            }
            else
            {
                // lane
                EvaluateLane();
            }
        }
        MoveCoroutine = null;
    }
Exemple #6
0
    private IEnumerator NPCMoveIE()
    {
        if (CurrentIndex == 0)
        {
            // increment index since spawn is index = 0 with no passed params
            EvaluateLane();
        }

        if (CurrentIndex != 0)
        {
            var duration    = LaneTime[CurrentIndex] - LaneTime[CurrentIndex - 1];
            var elapsedTime = 0f;
            while (elapsedTime < duration)
            {
                var factor = elapsedTime / duration;
                var pose   = Vector3.Lerp(LaneData[CurrentIndex - 1], LaneData[CurrentIndex], factor);
                var rot    = Quaternion.Slerp(Quaternion.Euler(LaneAngle[CurrentIndex - 1]), Quaternion.Euler(LaneAngle[CurrentIndex]), factor);
                if (!float.IsNaN(pose.x))
                {
                    rb.MovePosition(pose);
                    rb.MoveRotation(rot);
                }
                elapsedTime += Mathf.Min(Time.fixedDeltaTime, duration - elapsedTime);
                yield return(new WaitForFixedUpdate());
            }
            rb.MovePosition(LaneData[CurrentIndex]);
            rb.MoveRotation(Quaternion.Euler(LaneAngle[CurrentIndex]));
        }

        if (CurrentIndex <= LaneData.Count - 1)
        {
            //LaneData includes npc position at 0 index, waypoints starts from index 1
            //Because of that index has to be lowered by 1 before passing to the API
            if (ApiManager.Instance != null)
            {
                ApiManager.Instance.AddWaypointReached(gameObject, CurrentIndex - 1);
            }

            // apply simple distance trigger
            if (LaneTriggerDistance[CurrentIndex] > 0)
            {
                WaypointState = WaypointDriveState.Trigger;
                yield return(TriggerCoroutine = FixedUpdateManager.StartCoroutine(NPCTriggerIE()));
            }

            // apply complex triggers
            if (CurrentIndex < LaneTriggers.Count && LaneTriggers[CurrentIndex] != null)
            {
                WaypointState = WaypointDriveState.Trigger;
                yield return(TriggerCoroutine = FixedUpdateManager.StartCoroutine(LaneTriggers[CurrentIndex].Apply(controller)));

                TriggerCoroutine = null;
            }

            // deactivate
            CurrentDeactivate = LaneDeactivate[CurrentIndex];

            // idle
            if (LaneIdle[CurrentIndex] > 0)
            {
                WaypointState = WaypointDriveState.Idle;
                yield return(IdleCoroutine = FixedUpdateManager.StartCoroutine(NPCIdleIE(LaneIdle[CurrentIndex], CurrentDeactivate)));
            }
            else if (LaneIdle[CurrentIndex] == -1 && CurrentDeactivate)
            {
                WaypointState = WaypointDriveState.Despawn;
                gameObject.SetActive(false);
                MoveCoroutine = null;
                yield break;
            }
            else
            {
                // lane
                EvaluateLane();
            }
        }
        MoveCoroutine = null;
    }