/// <summary> /// Returns the associated prefab if the given color id is recognize /// Returns NULL when a match is not found /// </summary> /// <param name="colordId"></param> /// <returns></returns> PixleColorToGameObject GetObjectInfoByColorId(Color32 colordId) { PixleColorToGameObject info = new PixleColorToGameObject(); if (m_definitionTable.ContainsKey(colordId)) { info = m_definitionTable[colordId]; } else { Debug.Log("Color ID not found: " + colordId); } return(info); }
/// <summary> /// Spawns all the prefabs defined in the tile map texture /// </summary> void CreateMap() { Texture2D textureMap = GetCurentLevelTextureMap(); int mapWidth = textureMap.width; int mapHeight = textureMap.height; m_currentMapSize = new Vector2(mapWidth, mapHeight); for (int x = -1; x <= mapWidth; x++) { for (int y = -1; y <= mapHeight; y++) { Vector2 coords = new Vector2(x, y); // Edge if (x < 0 || x >= m_mapSize.x || y < 0 || y >= m_mapSize.y) { SpawnEdgeAt(coords, textureMap); continue; } // Get the prefab associated with the current pixle color Color32 colorId = textureMap.GetPixel(x, y); PixleColorToGameObject info = GetObjectInfoByColorId(colorId); // Always spawn a tile Tile tile = SpawnTileAt(coords); m_tilemap.Add(coords, tile); // Base on the tile color we may need to either turn the tile into void // or spawn an enemy or player switch (info.type) { case ObjectType.Enemy: GameObject enemy = Instantiate(m_enemyPrefab, transform); enemy.transform.position = new Vector3( coords.x, enemy.transform.position.y, coords.y ); break; case ObjectType.Player: GameObject player = Instantiate(m_playerPrefab, transform); player.transform.position = new Vector3( coords.x, player.transform.position.y, coords.y ); break; case ObjectType.Void: tile.State = TileState.Void; break; } } } }