Using autotile to draw a wall: how to replace collided wall tiles with broken wall tiles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Inferiplex

I have a top-down game where I need to draw walls.

I used autotile & a bitmask and the wall looks great. I added collisions to each tile and the player’s bullets now collide with them and explode.

My next step is to damage the appropriate tile.
I need each tile in the tileset to have HP and get destroyed when this hits zero. No idea how to go about this.

For starters, I need to be able to identify which tile was struck by the bullet - found some information but this has not worked for me yet. The collision is triggered by an event from _on_Area2D_body_entered().

I also need to replace the normal wall tile with, perhaps, a tile from a “broken” wall tileset - I guess this would be a separate autotiled tileset. Eventually I’d need to queue_free() or otherwise destroy wall segments entirely.

Not sure whether autotiles are the best fit for this but I’d love to get feedback, thanks!

:bust_in_silhouette: Reply From: zhyrin

When the Area2D body_entered(body: Node) signal is emitted, you cast the parameter into a TileSet.
I assume the area 2d is your bullet.
You want to get the tile at the bullet’s location:

# notice I use types here instead of instances of your bullet/tilemap
var bullet_in_global_space = Area2D.global_position
var bullet_in_tilemap_space = TileMap.to_local(bullet_in_global_space)
var bullet_in_tile_coordinate_space = TileMap.world_to_map(bullet_in_tilemap_space)
var cell_index = TileMap.get_cellv(bullet_in_tile_coordinate_space)
var tile_name = TileMap.tile_set.tile_get_name(cell_index)

From this you can calculate which is the tile you want to replace it with.

var new_tile_name = ...
var new_cell_index = TileMap.tile_set.find_tile_by_name(new_tile_name)
TileMap.set_cellv(bullet_in_tile_coordinate_space, new_cell_index)

This doesn’t work quite right, as it gets the tile at the center of the area2D and you have the collision at the edge of it, but I’ll leave that for you to figure out :slight_smile: