Exemplo n.º 1
0
    private void findAdjacentSectors(PathFindingTile curTile)
    {
        closedSet.Add(curTile);
        adjacentList.Clear();

        int minX = (curTile.index.x - 1) < 0 ? 0 : (curTile.index.x - 1);
        int maxX = (curTile.index.x + 1) >= tile_count.x ? tile_count.x - 1 : (curTile.index.x + 1);
        int minY = (curTile.index.y - 1) < 0 ? 0 : (curTile.index.y - 1);
        int maxY = (curTile.index.y + 1) >= tile_count.y ? tile_count.y - 1 : (curTile.index.y + 1);

        for (int y = minY; y <= maxY; ++y)
        {
            for (int x = minX; x <= maxX; ++x)
            {
                if (true == tiles[y, x].has_obstacle) // 장애물이 존재해서 못가는 곳이면
                {
                    continue;
                }
                if (true == closedSet.Contains(tiles[y, x]))
                {
                    continue;
                }

                if (null == tiles[y, x].parent)
                {
                    tiles[y, x].parent = curTile;
                    tiles[y, x].CalculateCost(this.targetIndex);
                }
                else
                {
                    PathFindingTile tile = new PathFindingTile();
                    tile.index  = tiles[y, x].index;
                    tile.parent = curTile;
                    tile.CalculateCost(this.targetIndex);

                    if (tile.f < tiles[y, x].f)
                    {
                        tiles[y, x].f      = tile.f;
                        tiles[y, x].g      = tile.g;
                        tiles[y, x].h      = tile.h;
                        tiles[y, x].h      = tile.h;
                        tiles[y, x].parent = tile.parent;
                    }
                }

                adjacentList.Add(tiles[y, x]);
            }
        }
    }
Exemplo n.º 2
0
    private PathFindingTile GetMinFSectorFromOpenSet()
    {
        PathFindingTile curTile  = openSet.First <PathFindingTile>();
        int             minFCost = curTile.f;

        foreach (var tile in openSet)
        {
            if (tile.f < minFCost)
            {
                curTile  = tile;
                minFCost = tile.f;
            }
        }

        openSet.Remove(curTile);

        return(curTile);
    }
Exemplo n.º 3
0
    public bool DoPathFinding()
    {
        Vector2 pos = player.transform.position;

        startIndex = ChangeWorldPointToTileIndex(pos);
        if (startIndex == targetIndex) // 찾은 경우
        {
            return(true);
        }

        openSet.Add(tiles[startIndex.y, startIndex.x]);
        PathFindingTile curTile = null;

        for (; ;)
        {
            if (openSet.Count == 0) // 없는 경우.
            {
                return(false);
            }

            curTile = GetMinFSectorFromOpenSet();
            findAdjacentSectors(curTile);
            if (adjacentList.Count == 0)
            {
                continue;
            }

            foreach (var tile in adjacentList)
            {
                if (tile == tiles[targetIndex.y, targetIndex.x])
                {
                    curTile = tile;
                    return(true);
                }

                if (false == openSet.Contains(tile))
                {
                    openSet.Add(tile);
                }
            }
        }
    }
Exemplo n.º 4
0
    public void Awake()
    {
        base_position = transform.parent.position;
        player        = GameObject.Find("MainPlayer");
        collider2d    = GetComponent <BoxCollider2D>();
        openSet       = new HashSet <PathFindingTile>();
        closedSet     = new HashSet <PathFindingTile>();
        adjacentList  = new List <PathFindingTile>();

        Vector2 parent_position = transform.parent.position;

        collider2d.enabled = false;
        tiles = new PathFindingTile[tile_count.y, tile_count.x];

        for (int y = 0; y < tile_count.y; ++y)
        {
            for (int x = 0; x < tile_count.x; ++x)
            {
                tiles[y, x]            = new PathFindingTile();
                tiles[y, x].map        = this;
                tiles[y, x].index.y    = y;
                tiles[y, x].index.x    = x;
                tiles[y, x].position.x = parent_position.x + (x * tile_size.x) + (tile_size.x / 2);
                tiles[y, x].position.y = parent_position.y - ((y * tile_size.y) + (tile_size.y / 2));

                RaycastHit2D hit = Physics2D.Raycast(tiles[y, x].position, Vector2.zero, 0f);
                if (hit.collider != null)
                {
                    //Debug.Log($" {hit.collider.name}: parent : {transform.parent.name},  tiles[{y}, {x}] obstacle position y: { tiles[y, x].position.y}, x:{tiles[y, x].position.x }");
                    tiles[y, x].has_obstacle = true;
                }
                else
                {
                    tiles[y, x].has_obstacle = false;
                }
            }
        }
        collider2d.enabled = true;
    }
Exemplo n.º 5
0
    public void Update()
    {
        if (Input.GetMouseButtonDown(1) && Input.GetKey(KeyCode.F))
        {
            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            targetIndex = ChangeWorldPointToTileIndex(pos);

            //Debug.Log($" mouse screen  position :  { Input.mousePosition }");
            //Debug.Log($" mouse world position :  { pos } ");
            //Debug.Log($" tile idx y:  { targetIndex.y }  x:  { targetIndex.x } ");
            //Debug.Log($" player pos y:  { player.transform.position.y }  x:  {player.transform.position.x } ");

            DoPathFinding();

            PathFindingTile curTile = tiles[targetIndex.y, targetIndex.x];
            while (curTile.parent != null)
            {
                Debug.Log($"curTile ({ curTile.index.y }, { curTile.index.x }) : position : { curTile.position.y }, { curTile.position.x }     ");

                curTile = curTile.parent;
            }
        }
    }