public override void OnTick(BaseTilemap <CropTileData> tilemap, int x, int y) { // Increment the age of the crop. CropTileData crop = tilemap[x, y]; crop.Age++; tilemap[x, y] = crop; // If the age is greater than or equal to the death age, unset the tile. if (crop.Age >= deathAge) { tilemap.SetTile(x, y, tilemap.Tileset.EmptyTileName); } // Otherwise; handle ageing the crop and spawning creatures. else { // Scale the plant. scalePlant(tilemap, x, y, crop); // If the age is greater than or equal to the mature age, spawn creatures. if (crop.Age >= matureAge) { CropTilemap cropTilemap = tilemap as CropTilemap; // Spawn the amount of creatures for this plant. for (int i = 0; i < creaturesPerTick; i++) { cropTilemap.CreatureManager.SpawnCreature(cropTilemap.GetCropSeed(x, y), creaturePrefab, x, y); } } } }
/// <summary> Calculates if the tile at the given <paramref name="x"/> and <paramref name="y"/> positions can be replaced with this <see cref="Tile{T}"/>. </summary> /// <param name="tilemap"> The tilemap. </param> /// <param name="x"> The x axis of the position. </param> /// <param name="y"> The y axis of the position. </param> /// <returns> True if this tile can be placed, otherwise; false. </returns> public override bool CanPlace(BaseTilemap <FloorTileData> tilemap, int x, int y) { // Get the object tilemap. BaseTilemap <ObjectTileData> objectMap = tilemap.WorldMap.GetTilemap <ObjectTileData>(); // If there is an object at this position and it cannot support this type of flooring, return false. if (objectMap.GetTile(x, y) is ObjectTile objectTile && !objectTile.FloorIsValid(this)) { return(false); } // If the previous checks have passed, return true as the placement is valid. return(true); }
/// <summary> Tries to spread the given <paramref name="tile"/> to the given <paramref name="x"/> and <paramref name="y"/> positions. </summary> /// <param name="tilemap"> The tilemap. </param> /// <param name="tile"> The tile that is being spread. </param> /// <param name="x"> The x position that is being spread to. </param> /// <param name="y"> The y position that is being spread to. </param> /// <returns> True if the tile spread, otherwise; false. </returns> private bool trySpreadToTile(BaseTilemap <FloorTileData> tilemap, Tile <FloorTileData> tile, int x, int y) { // If the tile is the spreadable material, can be placed, and the random roll was successful, spread the grass and return true. if (tilemap.IsTile(x, y, spreadableTile) && tile.CanPlace(tilemap, x, y) && Random.value <= spreadChance) { tilemap.SetTile(x, y, tile); return(true); } // Otherwise; return false as no grass was spread. else { return(false); } }
/// <summary> Attempts to spread to the surrounding 4 adjacent tiles, or all 8 if <see cref="spreadDiagonally"/> is true. </summary> /// <param name="tilemap"> The tilemap. </param> /// <param name="tile"> The tile that is being spread. </param> /// <param name="x"> The central x position. </param> /// <param name="y"> The central y position. </param> private void trySpread(BaseTilemap <FloorTileData> tilemap, Tile <FloorTileData> tile, int x, int y) { // If the given tile is null, do nothing. if (tile == null) { return; } // Try to spread to the directly adjacent tiles, if any of the attempts succeed, don't do the others. if (trySpreadToTile(tilemap, tile, x, y + 1)) { return; } if (trySpreadToTile(tilemap, tile, x + 1, y)) { return; } if (trySpreadToTile(tilemap, tile, x, y - 1)) { return; } if (trySpreadToTile(tilemap, tile, x - 1, y)) { return; } // If this tile cannot spread diagonally, return. if (!spreadDiagonally) { return; } // Try to spread to the diagonally adjacent tiles. if (trySpreadToTile(tilemap, tile, x + 1, y + 1)) { return; } if (trySpreadToTile(tilemap, tile, x + 1, y - 1)) { return; } if (trySpreadToTile(tilemap, tile, x - 1, y - 1)) { return; } if (trySpreadToTile(tilemap, tile, x - 1, y + 1)) { return; } }
/// <summary> Calculates if the given specific tile at the <paramref name="x"/> and <paramref name="y"/> position is valid for this object's placement. Note that this does not necessarily mean that the entire object can be placed. </summary> /// <param name="objectMap"> The tilemap. </param> /// <param name="x"> The x axis of the position. </param> /// <param name="y"> The y axis of the position. </param> /// <returns> True if the tile is valid, otherwise; false. </returns> public bool TileIsValid(BaseTilemap <ObjectTileData> objectMap, int x, int y) { // If this tile has an object already, return false. if (!objectMap.IsTileEmpty(x, y)) { return(false); } // If the floor is not valid, return false. if (!FloorIsValid(objectMap.WorldMap.GetTilemap <FloorTileData>(), x, y)) { return(false); } // If the other checks have passed, return true. return(true); }
/// <summary> Calculates if the given tile position has no other objects and a valid floor. </summary> /// <param name="tilemap"> The tilemap. </param> /// <param name="x"> The x axis of the position. </param> /// <param name="y"> The y axis of the position. </param> /// <returns> True if this tile can be placed, otherwise; false. </returns> public override bool CanPlace(BaseTilemap <ObjectTileData> tilemap, int x, int y) { // Check each tile in the area of the object. for (int checkX = x; checkX < x + Width; checkX++) { for (int checkY = y; checkY < y + Height; checkY++) { // If the area is not empty on the object map, or the floor is not valid on the floor map, return false. if (!TileIsValid(tilemap, checkX, checkY)) { return(false); } } } // If the other checks have passed, return true. return(true); }
/// <summary> Calculates if the given tile position has no objects and a valid floor. </summary> /// <param name="tilemap"> The tilemap. </param> /// <param name="x"> The x axis of the position. </param> /// <param name="y"> The y axis of the position. </param> /// <returns> True if this tile can be placed, otherwise; false. </returns> public override bool CanPlace(BaseTilemap <CropTileData> tilemap, int x, int y) { // If this tile has a crop on it already, return false. if (!tilemap.IsTileEmpty(x, y)) { return(false); } // If the floor is not valid, return false. if (!string.IsNullOrWhiteSpace(requiredFloor) && !tilemap.WorldMap.GetTilemap <FloorTileData>().IsTile(x, y, requiredFloor)) { return(false); } // If this tile has an object, return false. if (!tilemap.WorldMap.GetTilemap <ObjectTileData>().IsTileEmpty(x, y)) { return(false); } // If the other checks have passed, return true. return(true); }
/// <summary> Calculates if the <see cref="Tile{T}"/> at the given <paramref name="x"/> and <paramref name="y"/> positions is a valid floor compared to this <see cref="Tile{T}"/>'s <see cref="RequiredFloor"/>. </summary> /// <param name="floorMap"> The tilemap of the floor. </param> /// <param name="x"> The x axis of the position. </param> /// <param name="y"> The y axis of the position. </param> /// <returns> True if the tile is a valid floor, otherwise; false. </returns> public bool FloorIsValid(BaseTilemap <FloorTileData> floorMap, int x, int y) => FloorIsValid(floorMap.GetTile(x, y));
public override void OnTilePlaced(BaseTilemap <CropTileData> tilemap, int x, int y) => scalePlant(tilemap, x, y, tilemap[x, y]);
private void scalePlant(BaseTilemap <CropTileData> tilemap, int x, int y, CropTileData crop) { // Set the y scale of the plant so that it starts at 0 and is at maximum 1 when the plant is fully mature. // TODO: Multiple plant models for growth stages. tilemap.GetTileObject(x, y).transform.localScale = new Vector3(1, Mathf.Min((float)crop.Age / matureAge, 1), 1); }
public override void OnTick(BaseTilemap <FloorTileData> tilemap, int x, int y) { // Try to spread to the adjacent tiles. trySpread(tilemap, tilemap.GetTile(x, y), x, y); }