Exemplo n.º 1
0
        public override void OnUpdate()
        {
            goo = InputPath.Value as FsmPath;
            if (goo.Value == null)
            {
                Finish();
                return;
            }

            if (FsmConverter.GetPath(InputPath).vectorPath.Count == 0)
            {
                return;
            }                       // wait until path's ready

            var go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;

            goo = InputPath.Value as FsmPath;

            var moo = (SimpleSmoothModifier)go.AddComponent(typeof(SimpleSmoothModifier));

            goo.Value.vectorPath = moo.SmoothSimple(goo.Value.vectorPath);

            GameObject.Destroy(moo);
            Finish();
        }
Exemplo n.º 2
0
        public void DoStuff()
        {
            var doo = FsmConverter.GetPath(InputPath);

            doo.duration         = duration.Value;
            doo.heuristicScale   = heuristicScale.Value;
            doo.enabledTags      = enabledTags.Value;
            doo.radius           = radius.Value;
            doo.pathID           = (ushort)pathID.Value;
            doo.searchedNodes    = searchedNodes.Value;
            doo.searchIterations = searchIterations.Value;
            doo.speed            = speed.Value;
            doo.turnRadius       = turnRadius.Value;
            doo.recycled         = recycled.Value;
            nnConstraint.Value   = FsmConverter.SetNNConstraint(doo.nnConstraint);
            nodes.Value          = FsmConverter.SetNodes(doo.path);
            runData.Value        = FsmConverter.SetNodeRunData(doo.runData);
        }
Exemplo n.º 3
0
        public override void OnUpdate()
        {
            if (updatePath && moveMode == MoveMode.followPath)
            {
                path = FsmConverter.GetPath(inputPath);
            }

            if (path == null || path.vectorPath.Count == 0)              // only continue if path is valid
            {
                return;
            }

            if (!pathLoading && ((moveMode == MoveMode.follow || moveMode == MoveMode.followTo || moveMode == MoveMode.fleeContinuously) && frame >= Math.Max(1, updateInterval.Value)))
            {
                CalculatePath();
                pathLoading = true;
                frame       = 0;
            }
            else
            {
                frame += 1;
            }

            if (moveMode == MoveMode.shadow || moveMode == MoveMode.shadowTo)
            {
                ShadowExtendPath();
            }

            if (auto.Value)
            {
                Auto();
                if (endOfPathEvent != null)
                {
                    Fsm.Event(endOfPathEvent);
                }
                Finish();
            }

            if (currentWaypoint >= (path.vectorPath).Count)
            {
                currentWaypoint = path.vectorPath.Count - 1;
            }

            if ((finishDistanceMode == FinishDistance.absolute && (
                     (target.Value != null && (target.Value.transform.position - go.transform.position).sqrMagnitude <= finishDistance.Value * finishDistance.Value) ||
                     (ignoreY.Value && target.Value != null && (new Vector3(target.Value.transform.position.x, go.transform.position.y, target.Value.transform.position.z) - go.transform.position).sqrMagnitude <= finishDistance.Value * finishDistance.Value) ||
                     (target.Value == null && Vector3.Distance(go.transform.position, targetPosition.Value) <= finishDistance.Value))
                 ) ||
                (finishDistanceMode == FinishDistance.absoluteEndnode && (
                     (!ignoreY.Value && Vector3.Distance(go.transform.position, path.vectorPath[path.vectorPath.Count - 1]) <= finishDistance.Value) ||
                     (ignoreY.Value && Vector3.Distance(new Vector3(go.transform.position.x, path.vectorPath[path.vectorPath.Count - 1].y, go.transform.position.z), path.vectorPath[path.vectorPath.Count - 1]) <= finishDistance.Value)))
                )
            {
                Debug.Log("Finish");
                if (moveMode != MoveMode.follow && moveMode != MoveMode.shadow && moveMode != MoveMode.fleeContinuously)
                {
                    if (LogEvents.Value)
                    {
                        Debug.Log("End Of path reached.");
                    }

                    if (controller2 != null && controllerType == ControllerType.rvoController)                     //RVO controller needs to be set to 0/0/0 , else it continues running.
                    {
                        controller2.Move(new Vector3(0, 0, 0));
                    }

                    if (rigidbody != null && (controllerType == ControllerType.rigidbody || controllerType == ControllerType.rigidbodyVelocity))
                    {
                        rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
                    }

                    if (endOfPathEvent != null)
                    {
                        Fsm.Event(endOfPathEvent);
                    }

                    Finish();
                    return;
                }
                else
                {
                    return;
                }
            }
            else if (finishDistanceMode == FinishDistance.relative)
            {
                var i    = currentWaypoint;
                var leng = 0.0f;
                while (i < path.vectorPath.Count - 1)
                {
                    leng += Vector3.Distance(path.vectorPath[currentWaypoint], path.vectorPath[currentWaypoint + 1]);
                    if (leng > finishDistance.Value)                  // if the distance is still too far to finish, break to save performance
                    {
                        break;
                    }
                }

                if (leng <= finishDistance.Value)
                {
                    if (moveMode != MoveMode.follow && moveMode != MoveMode.shadow && moveMode != MoveMode.fleeContinuously)
                    {
                        if (LogEvents.Value)
                        {
                            Debug.Log("End Of path reached.");
                        }
                        if (controller2 != null && controllerType == ControllerType.rvoController)                         //RVO controller needs to be set to 0/0/0 , else it continues running.
                        {
                            controller2.Move(new Vector3(0, 0, 0));
                        }
                        if (rigidbody != null && (controllerType == ControllerType.rigidbody || controllerType == ControllerType.rigidbodyVelocity))
                        {
                            rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
                        }

                        Fsm.Event(endOfPathEvent);
                        Finish();
                        return;
                    }
                    else
                    {
                        return;
                    }
                }
            }


            // Check if we are close enough to the next waypoint.
            if (ignoreY.Value)
            {
                var distVec = nextPos - go.transform.position;
                distVec.y = 0;
                dist      = distVec.magnitude;
            }
            else
            {
                dist = Vector3.Distance(go.transform.position, nextPos);
            }
            if (dist < nextWaypointDistance.Value && !smoothTurns.Value)
            {
                if (finishDistanceMode == FinishDistance.last && currentWaypoint >= (path.vectorPath).Count - 1)
                {
                    if (moveMode != MoveMode.follow && moveMode != MoveMode.shadow && moveMode != MoveMode.fleeContinuously)
                    {
                        if (LogEvents.Value)
                        {
                            Debug.Log("End Of path reached.");
                        }

                        if (controller2 != null && controllerType == ControllerType.rvoController)                         //RVO controller needs to be set to 0/0/0 , else it continues running.
                        {
                            controller2.Move(new Vector3(0, 0, 0));
                        }

                        if (rigidbody != null && (controllerType == ControllerType.rigidbody || controllerType == ControllerType.rigidbodyVelocity))
                        {
                            rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
                        }

                        Fsm.Event(endOfPathEvent);
                        Finish();
                        return;
                    }
                }
                // Proceed to follow the next waypoint.
                currentWaypoint++;
                currentWaypoint = Math.Min(currentWaypoint, path.vectorPath.Count - 1);
            }

            nextPos = path.vectorPath[currentWaypoint];

            // Direction to the next waypoint.
            if (!smoothTurns.Value)
            {
                direction = (nextPos - go.transform.position).normalized;
            }
            //test
            else
            {
                var turnDistance = 0.0f;
                var targetPos    = prevTarget;
                if (frame == 1)
                {
                    if (firstFrame)
                    {
                        targetPos = go.transform.position;
                    }                                                                // keep targetPos on update

                    currentWaypoint    = 1;
                    path.vectorPath[0] = targetPos;
                }
                currentWaypoint = Math.Min(currentWaypoint, path.vectorPath.Count - 2);
                var deltaPos = 0.0f;

                if (frame == 1)
                {
                    deltaPos = turnRadius.Value - (targetPos - go.transform.position).magnitude;
                }
                else
                {
                    deltaPos = (go.transform.position - prevPosition).magnitude;
                }


                if (deltaPos * deltaPos > (path.vectorPath[currentWaypoint + 1] - targetPos).sqrMagnitude)
                {
                    while (deltaPos * deltaPos > (path.vectorPath[currentWaypoint + 1] - targetPos).sqrMagnitude)
                    {
                        currentWaypoint++;
                        currentWaypoint = Math.Min(currentWaypoint, path.vectorPath.Count - 2);
                        targetPos       = path.vectorPath[currentWaypoint];
                        deltaPos       -= (path.vectorPath[currentWaypoint] - targetPos).magnitude;
                    }
                }

                if ((targetPos - go.transform.position).sqrMagnitude < turnRadius.Value * turnRadius.Value)
                {
                    targetPos += ((path.vectorPath[currentWaypoint + 1] - targetPos).normalized) * deltaPos * 1.5f;
                }

                //	targetObjectHelper.transform.position = targetPos;
                prevPosition = go.transform.position;
                prevTarget   = targetPos;
                direction    = (targetPos - go.transform.position).normalized;
            }

            directionOut.Value = direction;

            if (ignoreY.Value)
            {
                direction.y        = 0;
                directionOut.Value = new Vector3(directionOut.Value.x, 0, directionOut.Value.z);
                directionOut.Value = directionOut.Value.normalized;
                direction          = direction.normalized;
            }

            outputSpeed.Value = (float)((1 / Math.Exp(costDependendSpeed.Value * path.path[Math.Min(currentWaypoint, path.path.Count - 1)].penalty)) * speed.Value);
            direction        *= outputSpeed.Value;      // 1/e^x for exponentially slower speed, but never 0 or negative or more than 1. Math classes were good for something afterall :D

            Move();

            firstFrame = false;

            if (drawGizmos && path != null && path.path != null && path.vectorPath != null)
            {
                for (var i = 0; i < path.vectorPath.Count - 1; i++)
                {
                    Debug.DrawLine(path.vectorPath[i], path.vectorPath[i + 1], gizmosColor.Value);
                }
            }

            return;
        }
Exemplo n.º 4
0
        public override void OnEnter()
        {
            if (moveMode == MoveMode.followPath)
            {
                path = FsmConverter.GetPath(inputPath);
                if (path == null)
                {
                    if (LogEvents.Value)
                    {
                        Debug.Log("Astar Follow Path failed. The path is null");
                    }

                    Fsm.Event(failedEvent);
                    Finish();
                    return;
                }
                else if (path.vectorPath.Count == 0)
                {
                    if (LogEvents.Value)
                    {
                        Debug.Log("Astar Follow Path failed. The path contains no nodes");
                    }

                    Fsm.Event(failedEvent);
                    Finish();
                    return;
                }
            }

            currentWaypoint = 0;
            go = actor.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : actor.GameObject.Value;
            if (go == null)
            {
                if (LogEvents.Value)
                {
                    Debug.Log("Astar Move To failed. The actor is null");
                }

                Fsm.Event(failedEvent);
                Finish();
                return;
            }            //finish the action if any of the requirements are not met

            controller  = go.GetComponent <CharacterController>();
            controller2 = go.GetComponent <RVOController>();
            rigidbody   = go.GetComponent <Rigidbody>();

            if (controllerType == ControllerType.characterController && controller == null)
            {
                controller = go.AddComponent <CharacterController>();
            }
            else if (controllerType == ControllerType.rvoController && controller2 == null)
            {
                if (AstarPath.HasPro)
                {
                    controller2 = go.AddComponent <RVOController>();
                    controller2.Move(new Vector3(0, 0, 0));
                }
                else
                {
                    controllerType = ControllerType.characterController;                    //free version can't use RVOControllers

                    if (controller == null)
                    {
                        controller = go.AddComponent <CharacterController>();
                    }
                }
            }
            else if (controllerType == ControllerType.rigidbody && rigidbody == null)
            {
                rigidbody                = go.AddComponent <Rigidbody>();
                rigidbody.drag           = 0.5f;
                rigidbody.freezeRotation = true;
            }
            else if (controllerType == ControllerType.rigidbodyVelocity && rigidbody == null)
            {
                rigidbody = go.AddComponent <Rigidbody>();
                rigidbody.freezeRotation = true;
            }

            if (moveMode != MoveMode.followPath)
            {
                CalculatePath();
            }

            if (moveMode == MoveMode.followPath && !startAtStart.Value)
            {
                currentWaypoint = getClosest();
            }


            if (moveMode == MoveMode.followPath && connectPath.Value)
            {
                done = false;
                ConnectPathX();
            }
        }