26 Oct 2019 - vidya
A major aspect of farming simulators like Stardew Valley and Harvest Moon is the farming!
Piggybacking on my targeting implementation from my last post, I realized I was going to need a means of converting from world space (in which the target point is found) to grid space. I was utilizing Unity’s Tilemap system for the grid placement, so I created a GridManager object that reads through the entire bounds of the Tilemap and holds onto a list of each Tile, its contents, etc. I could then input any point in world space and have the corresponding Tile (or its child objects) be returned.
Then I needed to utilize Unity’s 2D Extras repository, specifically the RuleTile script. This script allows for a Tile that adjusts its appearance based on adjacent Tiles. Using this, it became easy to create versatile Tilesets for a variety of layers and situations.
Once this was done, it was as easy as changing the effect of the Hoe item to not target any collisions, but instead to target the Tile inhabiting the target point. Then, if that Tile is of the correct identity (in this case, “Untilled Dirt”), the Hoe simply changes that “legalBeforeTile” to a corresponding “afterTile” (in this case, “Tilled Dirt”), all connected using the Unity Editor. Now, it’s easy to till and untill the soil!
And as you can see, it is unable to till the grass/dirt border tiles!
I’m still considering other uses of this implementation for gameplay, but for now it’s exciting to have one of the most fundamental aspects of farming in the game! Up next: grid placement of objects like trees, rocks, etc. and how to interact with them!