2023-11-23 23:02:18 +00:00
|
|
|
extends Node
|
|
|
|
class_name TK_GridController
|
|
|
|
|
|
|
|
## Composable 4-way grid controller.
|
|
|
|
## Depends on placement below some Node2D below some GridContext.
|
|
|
|
|
2023-11-24 10:11:00 +00:00
|
|
|
# todo: Turn-based logic context.
|
|
|
|
|
2023-11-23 23:02:18 +00:00
|
|
|
signal moved(p_new_cell_position)
|
|
|
|
|
2023-11-24 10:11:00 +00:00
|
|
|
var _grid_context_cache: Node2D
|
|
|
|
|
|
|
|
func _enter_tree() -> void:
|
|
|
|
_grid_context_cache = _get_grid_context()
|
|
|
|
|
2023-11-23 23:02:18 +00:00
|
|
|
func _input(p_event: InputEvent):
|
|
|
|
var direction := InputUtils.input_event_to_4way_direction(p_event)
|
|
|
|
if direction == Vector2.ZERO:
|
|
|
|
return
|
|
|
|
|
2023-11-24 10:11:00 +00:00
|
|
|
var cell = _grid_context_cache.position_to_cell_position(get_parent().position)
|
2023-11-23 23:02:18 +00:00
|
|
|
var new_cell = cell + direction
|
2023-11-24 10:11:00 +00:00
|
|
|
if _grid_context_cache.try_moving(get_parent(), new_cell):
|
2023-11-23 23:02:18 +00:00
|
|
|
emit_signal("moved", new_cell)
|
|
|
|
|
|
|
|
func _get_grid_context() -> Node:
|
|
|
|
var current := self.get_parent().get_parent()
|
|
|
|
while current.name != "GridContext":
|
|
|
|
current = current.get_parent()
|
|
|
|
return current
|