private void GetEnvTileType(LevelManager.LevelData levelData, int x, int y, Tileset tileset, out Tileset.TileCollection tileCollection, out float rotation) { tileCollection = tileset.fallback; rotation = 0; // True = same :: False = not same bool up = false; bool down = false; bool left = false; bool right = false; //Vector2Int size = levelData.GetSize(); int value = levelData.GetTile(x, y); if (y > 0 && levelData.GetTile(x, y - 1) == value) { up = true; } if (y < mapSize.y - 1 && levelData.GetTile(x, y + 1) == value) { down = true; } if (x > 0 && levelData.GetTile(x - 1, y) == value) { left = true; } if (x < mapSize.x - 1 && levelData.GetTile(x + 1, y) == value) { right = true; } bool upleft = false; bool upright = false; bool downleft = false; bool downright = false; if (up && left && levelData.GetTile(x - 1, y - 1) == value) { upleft = true; } if (up && right && levelData.GetTile(x + 1, y - 1) == value) { upright = true; } if (down && left && levelData.GetTile(x - 1, y + 1) == value) { downleft = true; } if (down && right && levelData.GetTile(x + 1, y + 1) == value) { downright = true; } if (up && down && left && right) { tileCollection = tileset.surrounded; rotation = 0; if (!upleft) { tileCollection = tileset.surrounded1; rotation = 180; } else if (!upright) { tileCollection = tileset.surrounded1; rotation = 90; } else if (!downright) { tileCollection = tileset.surrounded1; rotation = 0; } else if (!downleft) { tileCollection = tileset.surrounded1; rotation = 270; } if (tileCollection.objects.Length == 0) { tileCollection = tileset.surrounded; rotation = 0; } } else if (left && up && right) { if (upleft && upright) { tileCollection = tileset.tFill; } else if (upleft) { tileCollection = tileset.tFillRight; } else if (upright) { tileCollection = tileset.tFillLeft; } else { tileCollection = tileset.t; } rotation = 0; } else if (up && right && down) { if (upright && downright) { tileCollection = tileset.tFill; } else if (upright) { tileCollection = tileset.tFillRight; } else if (downright) { tileCollection = tileset.tFillLeft; } else { tileCollection = tileset.t; } rotation = 270; } else if (right && down && left) { if (downright && downleft) { tileCollection = tileset.tFill; } else if (downright) { tileCollection = tileset.tFillRight; } else if (downleft) { tileCollection = tileset.tFillLeft; } else { tileCollection = tileset.t; } rotation = 180; } else if (down && left && up) { if (downleft && upleft) { tileCollection = tileset.tFill; } else if (downleft) { tileCollection = tileset.tFillRight; } else if (upleft) { tileCollection = tileset.tFillLeft; } else { tileCollection = tileset.t; } rotation = 90; } else if (up && down) { tileCollection = tileset.straight; rotation = 90; } else if (left && right) { tileCollection = tileset.straight; rotation = 0; } else if (up && right) { if (upright) { tileCollection = tileset.cornerFill; } else { tileCollection = tileset.corner; } rotation = 0; } else if (right && down) { if (downright) { tileCollection = tileset.cornerFill; } else { tileCollection = tileset.corner; } rotation = 270; } else if (down && left) { if (downleft) { tileCollection = tileset.cornerFill; } else { tileCollection = tileset.corner; } rotation = 180; } else if (left && up) { if (upleft) { tileCollection = tileset.cornerFill; } else { tileCollection = tileset.corner; } rotation = 90; } else if (up) { tileCollection = tileset.peninsula; rotation = 90; } else if (right) { tileCollection = tileset.peninsula; rotation = 180; } else if (down) { tileCollection = tileset.peninsula; rotation = 270; } else if (left) { tileCollection = tileset.peninsula; rotation = 0; } // Fallback if (tileCollection == null) { tileCollection = tileset.fallback; rotation = 0; } }
/// <summary> /// generated grid map /// map_size : the number of cube in row and column /// node_radius: the cube radius /// parameter: None /// </summary> public void GenerateMap(LevelManager.LevelData levelData) { if (!gridRoot) { gridRoot = transform.Find("GridRoot"); } if (!gridRoot) { gridRoot = new GameObject("GridRoot").transform; gridRoot.parent = transform; } // Setup tilesets Dictionary <int, TilesetElement> tElements = new Dictionary <int, TilesetElement>(); foreach (TilesetElement te in tilesetElements) { tElements.Add(te.id, te); } // Extract data from levelData if (levelData != null) { mapSize = new Vector2Int(levelData.width, Mathf.CeilToInt(levelData.tiles.Length / levelData.width)); } // new grid with size [mapSize.x,mapSize.y] grid = new Tile[mapSize.x, mapSize.y]; int numExistedTiles = gridRoot.childCount; for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { int i = x + y * levelData.tiles.Length; int tileType = levelData.GetTile(x, y); TilesetElement te; tElements.TryGetValue(tileType, out te); // parse position for tile Vector3 tilePosition = GetWorldPosition(x, y); Transform tileTransform = i < numExistedTiles?gridRoot.GetChild(i) : Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90), gridRoot); // initiate outline tileTransform.localScale = Vector3.one * (1 - outlinePercent); // set tile value Tile tile = tileTransform.GetComponent <Tile>(); tile.walkable = (te != null ? te.walkable : true); tile.gridPosition = new Vector2Int(x, y); // insertion grid[x, y] = tile; // Tile display if (te != null) { Tileset.TileCollection tileCollection = null; float envTileRotation; GetEnvTileType(levelData, x, y, te.tileset, out tileCollection, out envTileRotation); if (tileCollection.objects.Length == 0) { tileCollection = te.tileset.fallback; } GameObject envTilePrefab = tileCollection.GetRandom(); if (envTilePrefab == null) { envTilePrefab = te.tileset.fallback.GetRandom(); } Vector3 envTilePosition = tilePosition; envTilePosition.y = -0.55f; Spawn(envTilePrefab, envTilePosition, Quaternion.Euler(0, envTileRotation, 0)); } } } for (int i = 0; i < levelData.endingPoints.Length; i += 2) { Spawn(ResourceUtility.GetPrefab("EndingPoint"), GetWorldPosition(levelData.endingPoints[i], levelData.endingPoints[i + 1]) + new Vector3(0, TileSize * 0.9f, 0), Quaternion.identity); } //light map initialize Instantiate(ResourceUtility.GetPrefab <GameObject>("LightMap"), Vector3.zero, Quaternion.identity, EnvironmentRoot); //change when light config implemented. }