robustness of tileset related functionality

This commit is contained in:
veclav talica
2023-12-25 17:29:53 +05:00
parent ec1c99ebb4
commit 85be2f7063
6 changed files with 61 additions and 23 deletions

View File

@ -1,6 +1,6 @@
tool
extends TK_Context
class_name TK_GridContext
class_name n_parent
## todo: Cell visualization.
## todo: Integration of TileMap.
@ -30,7 +30,8 @@ func try_moving(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
func register_collider(p_collider: Node) -> void:
_colliders[p_collider] = null
if p_collider.connect("child_exiting_tree", self, "_on_collider_exiting_tree") != OK:
if not p_collider.is_connected("child_exiting_tree", self, "_on_collider_exiting_tree") and \
p_collider.connect("child_exiting_tree", self, "_on_collider_exiting_tree") != OK:
assert(false)
func _on_collider_exiting_tree(p_collider) -> void:

View File

@ -2,10 +2,13 @@ tool
extends TileMap
# todo: Give care about descriptor swaps being loseless / non introducing artifacts.
# todo: Prevent user from deleting or editing tile_set property.
# todo: Update it when changed.
## Dictates whether tileset collision information should be exposed to game context.
export var expose_collisions: bool = true
# todo: Use Node identity instead of name.
var _descriptor_name_to_id: Dictionary # String to int
var _id_to_descriptor_name: Dictionary # int to String
@ -29,16 +32,26 @@ func is_path_traversible(p_game_object: Node2D, p_from: Vector2, p_to: Vector2)
func _enter_tree() -> void:
tile_set = TileSet.new()
get_parent().register_collider(self)
var n_parent := get_parent()
if not n_parent is TK_Context:
return
n_parent.register_collider(self)
# 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)
if connect("child_entered_tree", self, "_on_child_entered_tree") != OK:
if not n_parent.is_connected("cell_size_changed", self, "_update_cell_size") and \
n_parent.connect("cell_size_changed", self, "_update_cell_size") != OK:
assert(false)
if connect("child_exiting_tree", self, "_on_child_exiting_tree") != OK:
_update_cell_size(n_parent.cell_size, n_parent.cell_size)
if not is_connected("child_entered_tree", self, "_on_child_entered_tree") and \
connect("child_entered_tree", self, "_on_child_entered_tree") != OK:
assert(false)
if not is_connected("child_exiting_tree", self, "_on_child_exiting_tree") and \
connect("child_exiting_tree", self, "_on_child_exiting_tree") != OK:
assert(false)
func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
@ -56,11 +69,13 @@ func _on_child_entered_tree(p_node: Node) -> void:
_id_to_descriptor_name[id] = p_node.name
tile_set.create_tile(id)
tile_set.tile_set_texture(id, p_node.texture)
if not p_node.id.empty():
tile_set.tile_set_name(id, p_node.id)
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)
set_cellv(cell, -1)
tile_set.remove_tile(id)
if not _descriptor_name_to_id.erase(p_node.name):
assert(false)

View File

@ -7,6 +7,10 @@ export var texture: Texture setget _set_texture
# todo: More layer customizeability.
export var is_solid: bool
# todo: Update TileMapObject in editor when it's changed.
export var id: String = ""
# todo: Should work for non-grid objects.
func is_traversible_for(_p_game_object: Node2D) -> bool:
return not is_solid