2023-11-25 20:44:42 +00:00
|
|
|
tool
|
2023-11-25 13:27:41 +00:00
|
|
|
extends TK_Context
|
2023-11-23 23:02:18 +00:00
|
|
|
class_name TK_GridContext
|
|
|
|
|
|
|
|
## todo: Cell visualization.
|
|
|
|
## todo: Integration of TileMap.
|
|
|
|
|
2023-11-25 20:44:42 +00:00
|
|
|
export var cell_size: Vector2 = Vector2(64, 64) setget _set_cell_size
|
|
|
|
|
|
|
|
signal cell_size_changed(p_old_cell_size, p_new_cell_size)
|
2023-11-23 23:02:18 +00:00
|
|
|
|
2023-11-25 13:27:41 +00:00
|
|
|
func local_position_to_context_position(p_position: Vector2) -> Vector2:
|
2023-11-24 10:11:00 +00:00
|
|
|
return p_position / cell_size
|
2023-11-23 23:02:18 +00:00
|
|
|
|
2023-11-25 13:27:41 +00:00
|
|
|
func is_path_traversible(p_from: Vector2, p_to: Vector2) -> bool:
|
|
|
|
# todo:
|
2023-11-23 23:02:18 +00:00
|
|
|
return true
|
2023-11-24 10:11:00 +00:00
|
|
|
|
2023-11-25 13:27:41 +00:00
|
|
|
func try_moving(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
|
|
|
|
if not is_path_traversible(p_from, p_to):
|
2023-11-24 10:11:00 +00:00
|
|
|
return false
|
|
|
|
|
2023-11-25 13:27:41 +00:00
|
|
|
p_game_object.position = cell_size * p_to
|
2023-11-24 10:11:00 +00:00
|
|
|
return true
|
2023-11-25 20:44:42 +00:00
|
|
|
|
|
|
|
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)
|