/// <summary> /// Create a chunk of game map. /// The actul game map is combined by two chunk of map, each map chunck contents fix amount of tiles. /// When player move near to the eage of one of map chunk, other chunk will reset its position so it can be /// connect with current chunk of map. /// Repeat this process everytime when the player get near the eage. /// </summary> private void CreateMapChunk() { // Caluate the color of the wall. Color32 wallColor = MapThemeColor.ChangeColorBrightness(-0.3f); for (int chunkIndex = 0; chunkIndex < NumberOfMapChunk; chunkIndex++) { // Create a new gameObject as a root transform of the tile group. GameObject chunkRoot = new GameObject(); #if UNITY_EDITOR // Change its name for easies bugging in editor. chunkRoot.name = "MapChunkRoot " + chunkIndex; #endif // Create a new chunk for store all the map tile data. MapChunk chunk = new MapChunk(); // Notice that each loop creates two rows of tiles. for (int rowIndex = 0; rowIndex < LengthOfMapChunk; rowIndex++) { // Add tiles on the odd row. chunk.AddRowOfTiles(rowIndex, CreateTilesOnOddRow(chunkRoot.transform, MapThemeColor, wallColor, rowIndex, MaxNumberOfTilesOnRow)); // Add tiles on the even row. chunk.AddRowOfTiles(rowIndex, CreateTilesOnEvenRow(chunkRoot.transform, MapThemeColor, rowIndex, MaxNumberOfTilesOnRow - 1)); } // Reset its parent transform to the map manager for better management. chunkRoot.transform.SetParent(this.transform); // Set its offset position based on its index in the list. chunkRoot.transform.position = new Vector3(0, 0, chunkIndex * m_MapChunkOffset); // Apply the new create transform to this map chunk. chunk.Root = chunkRoot.transform; // Add all the datachunkRoot into the chunk. m_MapChunkList.Add(chunk); } }
/// <summary> /// Create tiles on the odd rows. /// </summary> /// <param name="numberOfTilesOnColumn">The number of tiles on column.</param> private List <MapTile> CreateTilesOnEvenRow(Transform root, Color32 tileColor, int rowIndex, int numberOfTilesOnColumn) { List <MapTile> column = new List <MapTile>(); // Get a lighter color for the tiles. Color32 secondlyColor = tileColor.ChangeColorBrightness(-.2f); for (int columnIndex = 0; columnIndex < numberOfTilesOnColumn; columnIndex++) { // Instantiate a new tile with position and rotatoin settings. GameObject tile = Instantiate( // Since the even rows wont have a wall, just pass in the normal map tile. m_MapTilePrefab, new Vector3( columnIndex * GameManager.TileOffset + (GameManager.TileOffset / 2), 0, rowIndex * GameManager.TileOffset + (GameManager.TileOffset / 2)), Quaternion.Euler(GameManager.TileRotation)) as GameObject; MapTile mapTile = tile.GetComponent <MapTile>(); // Note: if using MeshRenderer but not Renderer, the color setting will affect // on all the GameObjects which shares this same material. foreach (var render in tile.GetComponentsInChildren <Renderer>()) { // Like befer, Since the even rows wont have a wall, set the normal color. render.material.color = secondlyColor; mapTile.OriginalUpperMeshColor = render.material.color; } tile.transform.SetParent(root); // Remenber the local position, use it when reseting the tiles. mapTile.OriginalLocalPosition = tile.transform.localPosition; #if UNITY_EDITOR tile.name = "Even [ " + (rowIndex * 2 + 1) + ", " + columnIndex + " ]"; #endif column.Add(mapTile); } return(column); }
private void ChangeTileUpperMeshColor(int playerAtRow, int playerAtColumn, Color32 color) { GetTile(playerAtRow, playerAtColumn).UpperMesh.material.color = playerAtRow % 2 != 0 ? color.ChangeColorBrightness(-0.3f) : color; }