// Devuelve lista de vecinos sobre los que se puede expandir List<Vector3Int> getNeighbourListByType(Vector3Int pos, SpriteRepository.TileType type, bool inCross = false) { // Cogemos los vecinos List<Vector3Int> neigbours = new List<Vector3Int>(); for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++) if (mapTileDictionary.ContainsKey(new Vector3Int(x, y, 0) + pos)) { Vector3Int newPos = new Vector3Int(x, y, 0) + pos; // Solo contamos como vecinos, las celdas sobre las que nos podemos expandir if (mapTileDictionary[newPos].tileType == expandRules[type]) { // Cogemos en vecinos de matriz 3x3 if (!(y == 0 && x == 0) && !inCross) neigbours.Add(new Vector3Int(x, y, 0) + pos); // Cogemos vecinos en cruz else if (inCross && Mathf.Abs(x) + Mathf.Abs(y) == 1) neigbours.Add(new Vector3Int(x, y, 0) + pos); } } return neigbours; }
// Actualizamos el tipo de una celda y su sprite void updateMapTileType(Vector3Int pos, MapTile mapTile, SpriteRepository.TileType type) { tilePosByType[mapTile.tileType].Remove(pos); tilePosByType[type].Add(pos); mapTile.setTileType(type); tileMap.SetTile(pos, mapTile.tile); }
// Corutina - Actualizamos el tipo de una celda y su sprite IEnumerator updateMapTileType(Vector3Int pos, MapTile mapTile, SpriteRepository.TileType type, int timeToWait) { tilePosByType[mapTile.tileType].Remove(pos); tilePosByType[type].Add(pos); Debug.Log("Waiting for " + 1 * timeToWait); yield return new WaitForSecondsRealtime(1 * timeToWait); mapTile.setTileType(type); tileMap.SetTile(pos, mapTile.tile); }
// Creamos una celda según el tipo indicado MapTile createMapTile(Vector3Int pos, SpriteRepository.TileType type) { Debug.Log("Creando celda tipo " + type); // Creamos Tile y le añadimos el sprite Tile tile = ScriptableObject.CreateInstance("Tile") as Tile; tile.sprite = SpriteRepository.spriteRepo[type]; // Creamos nuestro objeto Tile MapTile mapTile = new MapTile(type, tile, pos); tileMap.SetTile(pos, tile); return mapTile; }
// Coloca semillas de un tipo en posiciones válidas List<Vector3Int> placeSeedsByType(SpriteRepository.TileType type, int typeSeedCount) { List<Vector3Int> seedPos = new List<Vector3Int>(); List<Vector3Int> keys = tilePosByType[expandRules[type]]; for (int i = 0; i < typeSeedCount; i++) { int index = Random.Range(0, keys.Count); Vector3Int pos = keys[index]; if(!seedPos.Contains(pos)) { updateMapTileType(pos, mapTileDictionary[pos], type); //tilePosByType[type].Add(pos); seedPos.Add(pos); } } return seedPos; }
// Método general, expande por Tipo de casilla void expandTileOpt(ref List<Vector3Int> tilePositions, SpriteRepository.TileType type, float chanceFactor = 1f) { List<Vector3Int> newTilePositions = new List<Vector3Int>(); // Iteramos por cada una de las celdas foreach (Vector3Int landPosition in tilePositions) { // Cogemos los vecinos List<Vector3Int> neigbours = getNeighbourListByType(landPosition,type); // Para cada uno de los vecinos de la celda foreach (Vector3Int neiPos in neigbours) { float randomValue = Random.Range(0f, 1f); // Si supera el umbral, y no pertenece a las que tenemos en esta iteración, la cambiamos a tipo hierba if (randomValue < expandChances[type] * chanceFactor) { if (!tilePositions.Contains(neiPos)) { if (mapTileDictionary[neiPos].tileType == type) continue; updateMapTileType(neiPos, mapTileDictionary[neiPos], type); newTilePositions.Add(neiPos); } // En caso contrarioa aumentamos su contador de transformación else { mapTileDictionary[neiPos].upgradeCount++; } } } } // Devolvemos únicamente las posiciones nuevas tilePositions = newTilePositions; }
// Devuelve vecinos de la celda dada del tipo dado List<Vector3Int> getNeighbourListOfType(Vector3Int pos, SpriteRepository.TileType type, bool inCross = false, int distance = 1) { // Cogemos los vecinos List<Vector3Int> neigbours = new List<Vector3Int>(); for (int x = -distance; x < distance + 1; x++) for (int y = -distance; y < distance + 1; y++) if (!(y == 0 && x == 0) && mapTileDictionary.ContainsKey(new Vector3Int(x, y, 0) + pos)) if (mapTileDictionary[new Vector3Int(x, y, 0) + pos].tileType == type) { // Cogemos en cruz if (inCross) { if (Mathf.Abs(x) + Mathf.Abs(y) == distance) neigbours.Add(new Vector3Int(x, y, 0) + pos); } // Cogemos en vecinos de matriz 3x3 else neigbours.Add(new Vector3Int(x, y, 0) + pos); } return neigbours; }