tochie-kit/nodes/GridContext.gd

34 lines
977 B
GDScript3
Raw Normal View History

tool
extends TK_Context
class_name TK_GridContext
## todo: Cell visualization.
## todo: Integration of TileMap.
export var cell_size: Vector2 = Vector2(64, 64) setget _set_cell_size
signal cell_size_changed(p_old_cell_size, p_new_cell_size)
func local_position_to_context_position(p_position: Vector2) -> Vector2:
return p_position / cell_size
func is_path_traversible(p_from: Vector2, p_to: Vector2) -> bool:
# todo:
return true
func try_moving(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
if not is_path_traversible(p_from, p_to):
return false
p_game_object.position = cell_size * p_to
return true
func _set_cell_size(p_new_cell_size: Vector2) -> void:
if p_new_cell_size.x == 0 or p_new_cell_size.y == 0:
push_error("Cell size cannot be zero")
return
var cell_size_cache = cell_size
cell_size = p_new_cell_size
emit_signal("cell_size_changed", cell_size_cache, p_new_cell_size)