private void GetWorldTiles() { tiles = new Dictionary <Vector3Int, WorldTiles>(); foreach (Tilemap map in grid.GetComponentsInChildren <Tilemap>()) { foreach (Vector3Int pos in map.cellBounds.allPositionsWithin) { Vector3Int localplace = new Vector3Int(pos.x, pos.y, pos.z); if (!map.HasTile(localplace)) { continue; } if (tiles.ContainsKey(localplace)) { tiles.Remove(localplace); } WorldTiles tile = new WorldTiles() { LocalPlace = localplace, WorldLocation = map.CellToWorld(localplace), TileBase = map.GetTile(localplace), TilemapMember = map, //nambah nanti Name = localplace.x + "," + localplace.y, Cost = 1 }; tiles.Add(tile.LocalPlace, tile); } } }
// Add a new land tile at x, y public void AddLandTile(Vector3 position, ref EnvironmentTile tile, Vector2Int mapSize, int x, int y) { // Calculate the random chance of choosing x tile group float randomChoice = Random.Range(0.0f, 100.0f); float runningTotal = 0.0f; // Provide a default tile group incase it falls through WorldTiles worldTilesChoice = mTiles[EnvironmentTile.TileType.Accessible]; foreach (WorldTiles worldTiles in mTiles.Values) { // If the current tile group falls within the bounds of the random number, select the tile group if (runningTotal + worldTiles.spawnChance > randomChoice) { worldTilesChoice = worldTiles; break; } runningTotal += worldTiles.spawnChance; } EnvironmentTile prefab = null; // Calculate the random chance of choosing x tile randomChoice = Random.Range(0.0f, 100.0f); runningTotal = 0; foreach (TileInstance tileInstance in worldTilesChoice.tiles) { // If the current tile group falls within the bounds of the random number, select the tile group if (runningTotal + tileInstance.spawnChance > randomChoice) { prefab = tileInstance.tile; break; } runningTotal += tileInstance.spawnChance; } tile = Instantiate(prefab, position, Quaternion.identity, transform); tile.Type = worldTilesChoice.type; FinalizeTile(ref tile, x, y, position); // Rotate the tile a random amount int rotationRand = Random.Range(0, 4); SetTileRotation(ref tile, rotationRand); }
public void Awake() { knight = knight_inst; worldTiles = worldTiles_inst; detonationHolder = detonationHolder_inst; }