void TileDataSet() { PlantTiles = new Dictionary <PlantType, Dictionary <PlantTileData, Tile> >(); PlantTiles[PlantType.Leaf] = new Dictionary <PlantTileData, Tile>(); PlantTiles[PlantType.Tree] = new Dictionary <PlantTileData, Tile>(); PlantTiles[PlantType.Mushroom] = new Dictionary <PlantTileData, Tile>(); PlantTile p = Resources.Load <PlantTile>("Tile/Tree"); PlantTiles[PlantType.Tree][PlantTileData.None] = null; PlantTiles[PlantType.Tree][PlantTileData.Zero] = p.plant0; PlantTiles[PlantType.Tree][PlantTileData.Twenty] = p.plant20; PlantTiles[PlantType.Tree][PlantTileData.Fifty] = p.plant50; PlantTiles[PlantType.Tree][PlantTileData.Seventy] = p.plant70; PlantTiles[PlantType.Tree][PlantTileData.Hundred] = p.plant100; p = Resources.Load <PlantTile>("Tile/Leaf"); PlantTiles[PlantType.Leaf][PlantTileData.None] = null; PlantTiles[PlantType.Leaf][PlantTileData.Zero] = p.plant0; PlantTiles[PlantType.Leaf][PlantTileData.Twenty] = p.plant20; PlantTiles[PlantType.Leaf][PlantTileData.Fifty] = p.plant50; PlantTiles[PlantType.Leaf][PlantTileData.Seventy] = p.plant70; PlantTiles[PlantType.Leaf][PlantTileData.Hundred] = p.plant100; p = Resources.Load <PlantTile>("Tile/Mushroom"); PlantTiles[PlantType.Mushroom][PlantTileData.None] = null; PlantTiles[PlantType.Mushroom][PlantTileData.Zero] = p.plant0; PlantTiles[PlantType.Mushroom][PlantTileData.Twenty] = p.plant20; PlantTiles[PlantType.Mushroom][PlantTileData.Fifty] = p.plant50; PlantTiles[PlantType.Mushroom][PlantTileData.Seventy] = p.plant70; PlantTiles[PlantType.Mushroom][PlantTileData.Hundred] = p.plant100; }
public void Seed(PlantTile tile, float fertility) { var growValue = Random.Range(0.0f, 1.0f); if (growValue <= Mathf.Clamp01(growChance)) { var randomIndex = Random.Range(0, plantBlueprints.Count); var choosenPlantBlueprint = plantBlueprints[randomIndex]; tile.Fertilize(fertility); tile.Seed(choosenPlantBlueprint.CreatePlant()); } }
/* Merge: * - Given a tile position, attempt to use it to merge into a new plant nearby. */ public bool CheckForMerge(Vector3Int tilePosition) { // At this location, what's the plant tile? PlantTile plantTile = plantTileFromPosition[tilePosition]; // What type of plant am I? Plant plant = plantTile.Plant; // Create empty list of potentialMerges List <List <PlantTile> > potentialMerges = new List <List <PlantTile> >(); // Who are my neighboring triples? List <List <Vector3Int> > tileTriples = GetClusters(tilePosition); tileTriples.AddRange(GetTendrils(tilePosition)); // For every triple, check if it forms a valid recipe and add to completedRecipes list. foreach (List <Vector3Int> triple in tileTriples) { // ValidRecipe should return a Plant if it found a valid merge, else a null. List <PlantTile> potentialMerge = ValidRecipe(triple); // If ValidRecipe returned a name, ther was a valid recipe. If null, no recipe found for this triple. if (potentialMerge != null) { potentialMerges.Add(potentialMerge); } } // If there are no potentialMerges, we can terminate early. if (potentialMerges.Count <= 0 || potentialMerges == null) { return(false); } // Now that we have every valid merge, we just need to pick one from among them and complete the merge. // First, pick one of the completedRecipes by some method. // TODO: By some other method than just random? int randomInt = Random.Range(0, potentialMerges.Count); List <PlantTile> chosenMerge = potentialMerges[randomInt]; randomInt = Random.Range(0, 3); Vector3Int checkAfter = new Vector3Int(); for (int i = 0; i < 3; i++) { if (i == 1) { // Update the tilemap with the new plant. plantTilemap.SetTile(chosenMerge[i].GridVector, chosenMerge[i].ThisTile); // Now we need to set the old PlantTile at tilePosition to be the chosenMerge plantTileFromPosition[chosenMerge[i].GridVector] = chosenMerge[i]; // Adjust the score! A plant was cleared, so the score should go down by the associated value. Score.scoreCount += plantTileFromPosition[tilePosition].Plant.score; checkAfter = chosenMerge[i].GridVector; } else { // Clear the plant off this location. ClearPlants(chosenMerge[i].GridVector); } } // TODO: Implement a merge delay. // Since this new tile might now be used for a different recipe, we need to check for another merge here. // TODO: Add a merge delay here too. CheckForMerge(checkAfter); return(true); }
// Spawning: // Spawn New Plants: IEnumerator SpawnPeriodicPlants() { while (true) { // Spawn plants on some tile from the list of allTilePositions. foreach (Vector3Int tilePosition in allTilePositions) { PlantTile plantTile = plantTileFromPosition[tilePosition]; Tile groundTile = gridManager.GetTiles(tilePosition)[0]; // If there is a ground tile and the tile has no plants... if (plantTile is null || plantTile.ThisTile.name.Equals("Empty", System.StringComparison.Ordinal)) { switch (groundTile.name) { case "Water": // Check if this tile will spawn by rolling under its associated spawnChance if ((Random.Range(0f, 1f)) < terrainFromName["Water"].spawnRate) { // Which tile spawns? Get a random number from 1 to the totalSpawnChance int randomDrop = Random.Range(1, terrainFromName["Water"].totalSpawnChance + 1); // Loop over each plant in the tarrain dropsDict and stop when randomDrop is <= 0 foreach (KeyValuePair <string, int> kvp in terrainFromName["Water"].dropsDict) { randomDrop -= kvp.Value; if (randomDrop <= 0) { // Start a cooroutine to change the tile. StartCoroutine(DelayedPlantSpawn(tilePosition, plantTileFromName[kvp.Key])); CheckForMerge(tilePosition); break; } } } break; case "Marsh": // Check if this tile will spawn by rolling under its associated spawnChance if ((Random.Range(0f, 1f)) < terrainFromName["Marsh"].spawnRate) { // Which tile spawns? Get a random number from 1 to the totalSpawnChance int randomDrop = Random.Range(1, terrainFromName["Marsh"].totalSpawnChance + 1); // Loop over each plant in the tarrain dropsDict and stop when randomDrop is <= 0 foreach (KeyValuePair <string, int> kvp in terrainFromName["Marsh"].dropsDict) { randomDrop -= kvp.Value; if (randomDrop <= 0) { // Start a cooroutine to change the tile. StartCoroutine(DelayedPlantSpawn(tilePosition, plantTileFromName[kvp.Key])); break; } } } break; case "Soil": // Check if this tile will spawn by rolling under its associated spawnChance if ((Random.Range(0f, 1f)) < terrainFromName["Soil"].spawnRate) { // Which tile spawns? Get a random number from 1 to the totalSpawnChance int randomDrop = Random.Range(1, terrainFromName["Soil"].totalSpawnChance + 1); // Loop over each plant in the tarrain dropsDict and stop when randomDrop is <= 0 foreach (KeyValuePair <string, int> kvp in terrainFromName["Soil"].dropsDict) { randomDrop -= kvp.Value; if (randomDrop <= 0) { // Start a cooroutine to change the tile. StartCoroutine(DelayedPlantSpawn(tilePosition, plantTileFromName[kvp.Key])); break; } } } break; case "Barren": // Check if this tile will spawn by rolling under its associated spawnChance if ((Random.Range(0f, 1f)) < terrainFromName["Barren"].spawnRate) { // Which tile spawns? Get a random number from 1 to the totalSpawnChance int randomDrop = Random.Range(1, terrainFromName["Barren"].totalSpawnChance + 1); // Loop over each plant in the tarrain dropsDict and stop when randomDrop is <= 0 foreach (KeyValuePair <string, int> kvp in terrainFromName["Barren"].dropsDict) { randomDrop -= kvp.Value; if (randomDrop <= 0) { // Start a cooroutine to change the tile. StartCoroutine(DelayedPlantSpawn(tilePosition, plantTileFromName[kvp.Key])); break; } } } break; } } } // Fudge with the spawnTimer ? /* * int degrees = Mathf.RoundToInt(Time.time) % 360; * spawnTimerFudged = 11 * Mathf.Cos(7 * degrees) + spawnTimer; * yield return new WaitForSeconds(spawnTimerFudged); */ // Otherwise, just wait the same time every time. yield return(new WaitForSeconds(spawnTimer)); } }
private void TryGrowTree(PlantTile tile) { RaycastTerrain(tile.Coordinate).Filter(terrain => terrain.Fertility != 0) .MatchSome(terrain => Seed(tile, terrain.Fertility)); }