31 lines
886 B
GDScript
31 lines
886 B
GDScript
extends Node
|
|
class_name TK_GridController
|
|
|
|
## Composable 4-way grid controller.
|
|
## Depends on placement below some Node2D below some GridContext.
|
|
|
|
# todo: Turn-based logic context.
|
|
|
|
signal moved(p_new_cell_position)
|
|
|
|
var _grid_context_cache: Node2D
|
|
|
|
func _enter_tree() -> void:
|
|
_grid_context_cache = _get_grid_context()
|
|
|
|
func _input(p_event: InputEvent):
|
|
var direction := InputUtils.input_event_to_4way_direction(p_event)
|
|
if direction == Vector2.ZERO:
|
|
return
|
|
|
|
var cell = _grid_context_cache.position_to_cell_position(get_parent().position)
|
|
var new_cell = cell + direction
|
|
if _grid_context_cache.try_moving(get_parent(), new_cell):
|
|
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
|