Esempio n. 1
0
 public void StopMoving()
 {
     nextWaypoint = null;
     movementWaypoints.Clear();
     stopMovingObservers?.Invoke();
     navMeshAgent.ResetPath();
 }
Esempio n. 2
0
        public void ProcessMovement()
        {
            if (nextWaypoint != null)
            {
                // Get the real distance between the character and its current node, and between the character and its next node
                float currentNodeDistance  = Vector3.Distance(node.transform.position, transform.position);
                float nextWaypointDistance = Vector3.Distance(nextWaypoint.position, transform.position);

                // If the character is nearer from its next node than from its current one, process the position change
                if (nextWaypoint.node != node && nextWaypointDistance <= currentNodeDistance)
                {
                    // Free the previous node
                    node.LeaveNode(this);

                    // Update the character's position
                    node = nextWaypoint.node;

                    // Reduce movement left
                    movementLeft = Mathf.Clamp(movementLeft - Mathf.RoundToInt(nextWaypoint.node.CostToEnter()), 0, 100);

                    enterNewNodeObservers?.Invoke(nextWaypoint.node.CostToEnter());

                    // Trigger the fact that the character entered a Node
                    nextWaypoint.node.EnterNode(this);

                    // Occupy the node
                    node.OccupyNode(gameObject);
                }

                // If the character is near enough from the waypoint to "reconsider" it (see if it should go to the next one), do it
                if (nextWaypointDistance <= 0.1f)
                {
                    // Check to see if the character should move to another waypoint
                    if (nextWaypoint.nextWaypoint != null)
                    {
                        nextWaypoint = nextWaypoint.nextWaypoint;
                        MoveToNextWaypoint();
                    }
                    // This is the final waypoint
                    else
                    {
                        StopMoving();
                    }
                }
                else
                {
                    MoveToNextWaypoint();
                }
            }
        }
Esempio n. 3
0
        // Make the character move towards a specific destination. If the character has not enough movement to reach it, it will get as near to the destination as possible
        public void MoveTo(Node destination, List <Node> path)
        {
            // Create the variables
            int movementCostSpent = 0;
            MovementWaypoint previouslyCreatedWaypoint = null;

            // Create the Movement Waypoints
            foreach (Node pathNode in path)
            {
                // Make sure to not create a waypoint on the current node, and to not create more waypoints than required
                if (node != pathNode && (movementCostSpent + Mathf.RoundToInt(pathNode.CostToEnter())) <= movementLeft)
                {
                    // Create the Waypoint
                    MovementWaypoint createdWaypoint = new MovementWaypoint
                    {
                        node     = pathNode,
                        position = new Vector3(pathNode.transform.position.x, 0, pathNode.transform.position.z)
                    };

                    // Set this newly created Waypoint as the last Waypoint's next Waypoint
                    if (previouslyCreatedWaypoint != null)
                    {
                        previouslyCreatedWaypoint.nextWaypoint = createdWaypoint;
                    }

                    // Store this Waypoint as the new "previous" one
                    previouslyCreatedWaypoint = createdWaypoint;

                    // Add this Waypoint to the list
                    movementWaypoints.Add(createdWaypoint);

                    // Update the path cost
                    movementCostSpent += Mathf.RoundToInt(pathNode.CostToEnter());
                }
            }

            // If at least a waypoint has been created, set the character destination, and reduce its movement left
            if (movementWaypoints.Count > 0)
            {
                nextWaypoint = movementWaypoints[0];

                // Start moving
                MoveToNextWaypoint();
            }
        }