Exemple #1
0
    private static void UpdateTileCollision(NavTilemap navMap, Tilemap[] tilemaps)
    {
        foreach (Tilemap tilemap in tilemaps)
        {
            var hasCollision = HasTileCollision(tilemap);

            for (int x = tilemap.cellBounds.xMin; x < tilemap.cellBounds.xMax; x++)
            {
                for (int y = tilemap.cellBounds.yMin; y < tilemap.cellBounds.yMax; y++)
                {
                    var tile = tilemap.GetTile(new Vector3Int(x, y, 0));
                    if (tile != null)
                    {
                        var navTile = navMap.GetTile(x, y);

                        navTile.exists = true;
                        if (hasCollision)
                        {
                            navTile.solid = true;
                        }
                    }
                }
            }
        }
    }
Exemple #2
0
    private static void ConnectNeighboringWaypoints(NavTilemap navMap, int x, int y)
    {
        var currTile = navMap.GetTile(x, y);

        if (currTile.waypoint == null)
        {
            return;
        }

        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                if (dx == dy && dx == 0)
                {
                    continue;
                }

                var shouldConnect = false;
                var tile          = navMap.GetTile(x + dx, y + dy);
                if (tile != null && tile.waypoint != null)
                {
                    var isDiagonal = dx != 0 && dy != 0;
                    if (isDiagonal)
                    {
                        var leftTile  = navMap.GetTile(x + dx, y);
                        var rightTile = navMap.GetTile(x, y + dy);

                        shouldConnect = (leftTile == null || !leftTile.solid) && (rightTile == null || !rightTile.solid);
                    }
                    else
                    {
                        shouldConnect = true;
                    }
                }

                if (shouldConnect)
                {
                    currTile.waypoint.neighbors.Add(tile.waypoint.gameObject);
                }
            }
        }
    }
Exemple #3
0
    //TODO move this to NavTilemap?
    private static void BuildWaypoints(Grid grid, NavTilemap navMap)
    {
        var        waypointPrefab = Resources.Load <GameObject>("Prefabs/ScenePrefabs/Waypoint");
        GameObject waypoints      = new GameObject("Waypoints");

        waypoints.AddComponent <WaypointGroup>();

        for (int x = navMap.cellBounds.xMin; x < navMap.cellBounds.xMax; x++)
        {
            for (int y = navMap.cellBounds.yMin; y < navMap.cellBounds.yMax; y++)
            {
                var tile = navMap.GetTile(x, y);
                if (!tile.solid && tile.exists)
                {
                    var waypointGO = GameObject.Instantiate(waypointPrefab);
                    waypointGO.name = String.Format("({0}, {1})", x, y);
                    waypointGO.transform.position = grid.GetCellCenterWorld(new Vector3Int(x, y, 0));
                    waypointGO.transform.SetParent(waypoints.transform);

                    tile.waypoint = waypointGO.GetComponent <Waypoint>();
                }
            }
        }
    }