2023-11-25 20:44:42 +00:00
|
|
|
tool
|
|
|
|
extends TileMap
|
|
|
|
|
2023-11-25 23:16:46 +00:00
|
|
|
# todo: Give care about descriptor swaps being loseless / non introducing artifacts.
|
|
|
|
|
2023-11-25 20:44:42 +00:00
|
|
|
## Dictates whether tileset collision information should be exposed to game context.
|
|
|
|
export var expose_collisions: bool = true
|
|
|
|
|
2023-11-25 23:16:46 +00:00
|
|
|
var _descriptor_name_to_id: Dictionary # String to int
|
|
|
|
|
|
|
|
func change_tile_texture(p_tile: String, p_texture: Texture) -> void:
|
|
|
|
if not p_tile in _descriptor_name_to_id:
|
|
|
|
push_error("Invalid tile name")
|
|
|
|
return
|
|
|
|
|
|
|
|
var id := _descriptor_name_to_id[p_tile] as int
|
|
|
|
tile_set.tile_set_texture(id, p_texture)
|
|
|
|
|
2023-11-25 20:44:42 +00:00
|
|
|
func _enter_tree() -> void:
|
2023-11-25 23:16:46 +00:00
|
|
|
tile_set = TileSet.new()
|
|
|
|
|
2023-11-25 20:44:42 +00:00
|
|
|
# It's okay to fail here for non-grid contexts.
|
|
|
|
if get_parent().connect("cell_size_changed", self, "_update_cell_size"):
|
|
|
|
_update_cell_size(get_parent().cell_size, get_parent().cell_size)
|
|
|
|
|
2023-11-25 23:16:46 +00:00
|
|
|
if connect("child_entered_tree", self, "_on_child_entered_tree") != OK:
|
|
|
|
assert(false)
|
|
|
|
|
|
|
|
if connect("child_exiting_tree", self, "_on_child_exiting_tree") != OK:
|
|
|
|
assert(false)
|
|
|
|
|
2023-11-25 20:44:42 +00:00
|
|
|
func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
|
|
|
|
# We're assuming that all tile textures are of same dimensions.
|
|
|
|
# todo: Diagnose mismatch by walking over all textures?
|
2023-11-25 23:21:49 +00:00
|
|
|
if tile_set.get_tiles_ids().size() == 0:
|
|
|
|
return
|
2023-11-25 20:44:42 +00:00
|
|
|
var tileset_texture_size := tile_set.tile_get_texture(0).get_size()
|
|
|
|
scale = p_new_cell_size / tileset_texture_size
|
|
|
|
cell_size = p_new_cell_size / scale
|
2023-11-25 23:16:46 +00:00
|
|
|
|
|
|
|
func _on_child_entered_tree(p_node: Node) -> void:
|
|
|
|
var id := tile_set.get_last_unused_tile_id()
|
|
|
|
_descriptor_name_to_id[p_node.name] = id
|
|
|
|
tile_set.create_tile(id)
|
|
|
|
tile_set.tile_set_texture(id, p_node.texture)
|
|
|
|
|
|
|
|
func _on_child_exiting_tree(p_node: Node) -> void:
|
|
|
|
var id := _descriptor_name_to_id[p_node.name] as int
|
|
|
|
for cell in get_used_cells_by_id(id):
|
|
|
|
set_cell(cell.x, cell.y, -1)
|
|
|
|
tile_set.remove_tile(id)
|
|
|
|
if not _descriptor_name_to_id.erase(p_node.name):
|
|
|
|
assert(false)
|