예제 #1
0
 // Search a list for a specific object
 private bool SearchListFor(List <world.Tile> list, world.Tile obj)
 {
     foreach (var item in list)
     {
         if (item.id == obj.id)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #2
0
    // returns the target from a mouse raycast
    public static world.Tile MouseRaycast()
    {
        world.Tile target   = null;
        Vector3    mousePos = Input.mousePosition;
        Vector3    worldPos = Camera.main.ScreenToWorldPoint(mousePos);
        RaycastHit hit;

        Debug.DrawRay(worldPos, Vector3.forward, Color.red, 100);
        if (Physics.Raycast(worldPos, Vector3.forward, out hit, Mathf.Infinity))
        {
            if (hit.transform.GetComponent <TileReference>())
            {
                return(target = hit.transform.GetComponent <TileReference>().reference);
            }
        }
        Debug.LogError("Raycast did not find any tile");
        return(target);
    }
예제 #3
0
 private void Update()
 {
     // if mouse press -> send co-ordinates to A-star pathfinding agent
     if (Input.GetMouseButtonDown(0))
     {
         world.Tile target = MouseRead.MouseRaycast();
         if (target != null)
         {
             if (!target.isWall)
             {
                 Vector2Int        destination = target.index;
                 List <Vector2Int> path        = astar.FindPath(destination, new Vector2Int((int)character.itemRep.transform.position.x, (int)character.itemRep.transform.position.y), map, 0.2f);
                 collorPath(path);
                 character.traversePath(path, map);
             }
         }
     }
 }
예제 #4
0
    // input a destination, start position and, the map in which to navigate
    // Returns a path of tiles
    public List <Vector2Int> FindPath(Vector2Int destination, Vector2Int startPosition, world.Tile[,] map, float _alphaGradient)
    {
        float alphaGradient = _alphaGradient;

        List <Tile>       OpenList = new List <Tile>();
        List <Vector2Int> path     = new List <Vector2Int>();

        Tile startTile   = map[startPosition.x, startPosition.y];
        Tile endTile     = map[destination.x, destination.y];
        Tile currentTile = startTile;

        OpenList.Add(currentTile);
        currentTile.Open = true;
        do
        {
            // FIND TILE WITH LOWEST FITNESS

            currentTile = FindLowestF(OpenList);

            // SEARCH ADJACENT TILES

            Vector2Int currentTileIndex = currentTile.index;
            currentTile.ItemRep.GetComponent <SpriteRenderer>().color = Color.green;

            for (int i = currentTileIndex.x - 1; i <= currentTileIndex.x + 1; i++)
            {
                for (int j = currentTileIndex.y - 1; j <= currentTileIndex.y + 1; j++)
                {
                    world.Tile successor = map[i, j];
                    successor.j = currentTile.j + 1 * alphaGradient;
                    successor.h = Vector2Int.Distance(destination, map[i, j].index);
                    successor.f = successor.h + successor.j;

                    if (!successor.Open && !successor.Closed && !successor.isWall) // should we mark it as open?
                    {
                        map[i, j].ItemRep.GetComponent <SpriteRenderer>().color = Color.red;
                        successor.Open   = true;
                        successor.parent = currentTile;
                        OpenList.Add(successor);
                    }

                    if (map[i, j].index == destination) // If we found the destination
                    {
                        map[i, j].ItemRep.GetComponent <SpriteRenderer>().color = Color.cyan;
                        world.Tile temp = currentTile;

                        do
                        {
                            path.Add(temp.parent.index);
                            temp = temp.parent;
                        } while (temp.parent != null);

                        ClearTiles(map);
                        return(path);
                    }
                }
            }
            currentTile.Closed = true;
            currentTile.Open   = false;
            OpenList.Remove(currentTile);
        } while (OpenList.Count > 0);

        Debug.LogWarning("Function Astar.FindPath() could not find a path to target");
        ClearTiles(map);
        return(path);
    }