Dynamically generating tiles in specific shapes/regions

Godot Version

4.3

Question

Hi there!

Working on a game with 16x16 tiles. I’ve successfully created a way for the player to change 1 tile to another tile (e.g., till the land), using the set_cells_terrain_connect method. It works great, changing 1 tileset to another tileset, and connecting the tiles nicely.

However, I am wanting now to, from the player’s position, create a shape (3x3 tiles) in the direction the player is facing. I can’t seem to figure it out. I did pull surrounding tiles, but then the player is caught underneath, which I don’t want.

Attached is an image showing what I mean. I’m looking to do the left part of the image. Right now I can only do the right (green is player, yellow is changed terrain).

godot-help

Additionally, if creating this tile square of 3x3, I’d also like to prevent the tile from being placed if a cliff (or collision) will interact with any part of the 3x3 square. Hope that makes sense.

Can anyone point me in the right direction? Many thanks.

If I understand correctly, you can just loop through all the tiles that should be changed…

for x in range(3): for y in range(3):
	pass

…and check if that tile can be changed (for example is not on a cliff). You’d just have to find the “origin” of the square you’re looping through:


(The red pixels are the origins, grey is the squares, black the player)
The position of the origins relative to the player is 2 * dir + Vector2i(-1, -1) where dir is the direction the square should be placed in.

So putting that all together it should be more or less

var tile_positions: Array[Vector2i] = []
var square_orgin: Vector2i = player_position + 2 * dir + Vector2i(-1, -1)
for x in range(3): for y in range(3):
	# check if is valid tile
	tile_positions.push_back(square_orgin + Vector2i(x, y)

Now tile_positions has all the tiles that need to be changed