// This method will run the AStar algorithm to find a shortest path between the given start and finish grid positions.
    // It will then draw tiles on a temporary tileMap corresponding to the NPCMovementSteps populated by AStar in the
    // NPCMovementStepsStack so we can see the corresponding path.
    private void Update()
    {
        // Only run if we've supplied a start/finish position, tilemap to display, and tiles to make path. Else, do nothing!
        if (startPosition != null && finishPosition != null & tileMapToDisplayPathOn != null && tileToUseToDisplayPath != null)
        {
            // Display the start and finish tiles if we set the flag to do so
            if (displayStartAndFinish)
            {
                // Display the start tile on the temporary tileMap
                tileMapToDisplayPathOn.SetTile(new Vector3Int(startPosition.x, startPosition.y, 0), tileToUseToDisplayPath);

                // Display the finish tile on the temporary tileMap
                tileMapToDisplayPathOn.SetTile(new Vector3Int(finishPosition.x, finishPosition.y, 0), tileToUseToDisplayPath);
            }
            // If we didn't set the flag, clear out any tiles that might be displayed from when we did have the flag set
            else
            {
                // Clear the start tile on the temporary tileMap if we didn't set the flag
                tileMapToDisplayPathOn.SetTile(new Vector3Int(startPosition.x, startPosition.y, 0), null);

                // Clear the finish tile on the temporary tileMap if we didn't set the flag
                tileMapToDisplayPathOn.SetTile(new Vector3Int(finishPosition.x, finishPosition.y, 0), null);
            }

            // Display the path found if we set the flag to do so
            if (displayPath)
            {
                // Get the current active scene name
                Enum.TryParse <SceneName>(SceneManager.GetActiveScene().name, out SceneName sceneName);

                // Build the path with the AStar algorithm
                aStar.BuildPath(sceneName, startPosition, finishPosition, npcMovementSteps);

                // Display the path on the tilemap. Loop through all of the movementSteps stored in the npcMovementStapsStack, as determined with AStar, and enable a tile for each of them
                foreach (NPCMovementStep npcMovementStep in npcMovementSteps)
                {
                    tileMapToDisplayPathOn.SetTile(new Vector3Int(npcMovementStep.gridCoordinate.x, npcMovementStep.gridCoordinate.y, 0), tileToUseToDisplayPath);
                }
            }
            // If we didn't set the flag, clear out any tiles that might be displayed, and npcMovementSteps in the stack from when we did have the flag set
            else
            {
                // Clear the path on the temporary tileMap if we didn't set the flag
                if (npcMovementSteps.Count > 0)
                {
                    // Clear the path on the tilemap
                    foreach (NPCMovementStep npcMovementStep in npcMovementSteps)
                    {
                        tileMapToDisplayPathOn.SetTile(new Vector3Int(npcMovementStep.gridCoordinate.x, npcMovementStep.gridCoordinate.y, 0), null);
                    }

                    // Clear out the NPC movement steps
                    npcMovementSteps.Clear();
                }
            }
        }
    }
Esempio n. 2
0
 public bool MoveToLocation(Vector2 location)
 {
     if (!AStar.BuildPath(World.Get().Tiles, transform.position, location, ref CurrentPath))
     {
         Debug.LogWarning("No path could be built for agent: " + gameObject.name + " from location " + transform.position.ToString() + " to " + location.ToString());
         return(false);
     }
     return(true);
 }
Esempio n. 3
0
 public bool BuildPath(SceneName sceneName, Vector2Int startGridPosition, Vector2Int endGridPosition, Stack <NPCMovementStep> npcMovementStepStack)
 {
     if (aStar.BuildPath(sceneName, startGridPosition, endGridPosition, npcMovementStepStack))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 4
0
    public GameObject GetClosestIdleWorker(Vector2 location)
    {
        float      shortestPathLength = 999999f;
        Path       currentPath        = new Path();
        GameObject nearestWorker      = null;

        foreach (GameObject worker in Workers)
        {
            if (worker.GetComponent <AgentJobHandler>().IsIdle)
            {
                if (AStar.BuildPath(Tiles, new Vector2(worker.transform.position.x, worker.transform.position.y), location, ref currentPath))
                {
                    if (currentPath.PathLength() < shortestPathLength)
                    {
                        nearestWorker = worker;
                    }
                }
                Debug.LogWarning("Cannot build path!");
            }
        }
        return(nearestWorker);
    }