Exemple #1
0
    /**
     * initiate navigation map
     */
    public void initNavigationMap()
    {
        calculatedPaths = new Dictionary <((int, int), (int, int)), List <NodeItem> >();
        for (int i = 0; i < WallMarks.transform.childCount; i++)
        {
            Destroy(WallMarks.transform.GetChild(i).gameObject);
        }
        for (int i = 0; i < PathMarks.transform.childCount; i++)
        {
            PathMarks.transform.GetChild(i).gameObject.SetActive(false);
        }
        w   = Mathf.RoundToInt(tilemapEnd.x - tilemapStart.x + 1);
        h   = Mathf.RoundToInt(tilemapEnd.y - tilemapStart.y + 1);
        map = new NodeItem[w, h];

        CompositeCollider2D wallCollider  = wall.GetComponent <CompositeCollider2D>();
        CompositeCollider2D floorCollider = GetComponent <CompositeCollider2D>();
        CompositeCollider2D pitCollider   = pit.GetComponent <CompositeCollider2D>();

        // write unwalkable node
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                Vector2 pos = new Vector2(tilemapStart.x + x, tilemapStart.y + y);
                // check walkable or not
                bool isWall   = wallCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f));
                bool isPit    = pitCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f));
                int  nearWall = 0;
                bool nearPit  = false;
                // print("isWall: " + isWall.ToString());
                for (int i = 0; i < 3; ++i)
                {
                    for (int j = 0; j < 3; ++j)
                    {
                        if (!(i == 1 && j == 1))
                        {
                            if (wallCollider.OverlapPoint(pos + new Vector2(0.5f + i - 1, 0.5f + j - 1)))
                            {
                                ++nearWall;
                            }
                        }
                        nearPit = nearPit || pitCollider.OverlapPoint(pos + new Vector2(0.5f + i - 1, 0.5f + j - 1));
                    }
                }
                // print("nearWall: " + nearWall.ToString());
                // new a node
                map [x, y] = NodeItem.init(isWall || isPit, pos, x, y, 100000);
                if (floorCollider.OverlapPoint(pos + new Vector2(0.5f, 0.5f)))
                {
                    // print("Setting floor cost...");
                    if (nearWall > 0 || nearPit)
                    {
                        if (nearWall > 1)
                        {
                            map[x, y].cost = GetComponent <NodeCost>().cost *nearWallCost;
                        }
                        else
                        {
                            map[x, y].cost = GetComponent <NodeCost>().cost;
                        }
                        if (nearPit)
                        {
                            map[x, y].cost = GetComponent <NodeCost>().cost *nearWallCost;
                        }
                    }
                    else if (!(isWall || isPit))
                    {
                        map[x, y].cost = GetComponent <NodeCost>().cost;
                    }
                }
                // mark unwalkable node
                if ((nearWall > 1 || nearPit) && !(isWall || isPit) && showWallMark && WallMark)
                {
                    GameObject obj = Instantiate(WallMark, new Vector3(pos.x + 0.5f, pos.y + 0.5f, 0), Quaternion.identity);
                    obj.transform.SetParent(WallMarks.transform);
                }
            }
        }
    }