27 lines
874 B
GDScript
27 lines
874 B
GDScript
extends Node
|
|
class_name TK_GridController
|
|
|
|
## Composable 4-way grid controller.
|
|
## Depends on placement below some Node2D below some GridContext.
|
|
|
|
signal moved(p_new_cell_position)
|
|
|
|
func _input(p_event: InputEvent):
|
|
var direction := InputUtils.input_event_to_4way_direction(p_event)
|
|
if direction == Vector2.ZERO:
|
|
return
|
|
|
|
var context := _get_grid_context()
|
|
var cell = context.position_to_cell_position(get_parent().global_position)
|
|
var new_cell = cell + direction
|
|
if context.is_cell_traversible(new_cell):
|
|
# todo: Should we move the outmost node2d?
|
|
get_parent().position += context.cell_size * direction
|
|
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
|