In the “Snow Game” project I’m working on, you can dig through snow tiles, each action removing “half” a tile of snow. I set up everything like this:
First, I made a 2d array to store the game’s tiles. Each element will be a tile object, which is just a table since we’re working with Lua. Since digging will sometimes leave half-tiles behind, and since we’ll want to place snow back too, we’ll need a variable to keep track of the current state of a tile, so I made some constants to represent the state of a snow tile. Finally, I wanted some variety on the sprites used for a snow tile, so I randomly pick a sprite ID out of the snow tiles I made in the PV8 sprite editor.
--Constants to represent the state of a snow tile
local SNOW_EMPTY = 0
local SNOW_FULL = 1
local SNOW_HALF = 2
local SNOW_HALF_LEFT = 3
local SNOW_HALF_RIGHT = 4
--Sprite IDs for a full snow tile
local snowFullTileTypes = {24, 25, 26}
...other code...
--An example snow tile
local snow = {tileType = SNOW_FULL, tileFullId = snowFullTileTypes[math.random(#snowFullTileTypes)]}
The snow’s tileType is its current state. The tileFullId will be the sprite ID to use when drawing the tile at full size. We also have SNOW_HALF_LEFT and SNOW_HALF_RIGHT for sloped snow tiles, just to make the snow look nicer overall when digging.
For the digging itself, I’ll just write the pseudocode for it since there are a few different checks to do.
if Z is pressed
if the tile below the player is a non-empty snow tile
if the tile is a full snow tile
change this bottom tile to a half-snow tile
else
change this bottom tile to an empty tile
if the left tile is a half-snow tile of any kind
change this left tile to an empty tile
if the right tile is a half-snow tile of any kind
change this right tile to an empty tile
if the bottom-left tile is a full or half-snow tile and the tile above it is empty
change this bottom-left tile to a SNOW_HALF_RIGHT tile
if the bottom-right tile is a full or half-snow tile and the tile above it is empty
change this bottom-right tile to a SNOW_HALF_LEFT tile
I might’ve missed some edge cases, but the idea is to check the tiles around the player, and if the right conditions are met, change the appropriate tiles to the correct new state. The player moves in a tile-based manner, so we also have to check for bounds, too.
For placing back snow, it’s easier because unlike digging snow, placing back snow will not be affecting tiles to the left and right. I made this decision since it meant that the player would start seeing more “artificial”-looking columns of snow, signs that someone has played around in the snow, so to speak.
if X is pressed
if the tile below the player is a half-snow tile of any kind
change this bottom tile to a full snow tile
else
change the tile above this bottom tile into a half-snow tile
Like before, there has to be bounds checks since we’re dealing with a 2d array. But also, if you noticed, this code can change the tile the player is occupying into a half-snow tile, which means the player has to move up a tile. I already have the player’s y position updating constantly in another part of the code, so it was unnecessary to do so here.
That’s the overall way I’ve implemented the snow digging + placing mechanic. One good thing to do is have several helper functions when making a mechanic with different kinds of checks. Some of these functions I have, like “isFullTile()”, “isHalfTile()”, and “isValidTile()”, have actually helped me in implementing future mechanics, so it was definitely helpful making these functions early in development.