Files
mortal-maze-the-game/ui/mobile_controls/mobile_controls.gd
T

104 lines
3.6 KiB
GDScript

extends CanvasLayer
## Controles táctiles provisionales para jugar en móvil.
##
## Los botones no llaman directamente al Player. En su lugar pulsan y liberan
## las mismas acciones del InputMap que ya usa el teclado. De esta forma la
## lógica de movimiento/combate sigue teniendo una única fuente de entrada:
##
## teclado -> InputMap <- controles táctiles
## |
## Player
##
## Cuando llegue el arte definitivo, esta escena puede cambiar por completo sin
## tocar player.gd mientras conserve estas seis acciones.
@onready var move_left_button: Button = $Controls/MoveLeft
@onready var move_right_button: Button = $Controls/MoveRight
@onready var jump_button: Button = $Controls/Jump
@onready var attack_button: Button = $Controls/Attack
@onready var ground_pound_button: Button = $Controls/GroundPound
@onready var special_attack_button: Button = $Controls/SpecialAttack
var direction_touch_index: int = -1
## Asocia botones con las mismas acciones utilizadas por el teclado.
func _ready() -> void:
_bind_button(jump_button, &"jump")
_bind_button(attack_button, &"attack")
_bind_button(ground_pound_button, &"ground_pound")
_bind_button(special_attack_button, &"special_attack")
## Mantiene un unico dedo para direccion y permite deslizarlo entre ambos lados.
func _input(event: InputEvent) -> void:
if event is InputEventScreenTouch:
var touch := event as InputEventScreenTouch
if touch.pressed and direction_touch_index == -1:
var action := _direction_at(touch.position)
if action != &"":
direction_touch_index = touch.index
_press_direction(action)
elif not touch.pressed and touch.index == direction_touch_index:
_release_direction()
elif event is InputEventScreenDrag:
var drag := event as InputEventScreenDrag
if drag.index == direction_touch_index:
var action := _direction_at(drag.position)
if action != &"":
_press_direction(action)
## Libera acciones al cambiar de escena para no dejar botones virtuales pulsados.
func _exit_tree() -> void:
# Si la escena cambia mientras un dedo sigue apoyado, Godot puede no llegar
# a emitir button_up. Liberamos las acciones para no dejar una tecla virtual
# atascada al cargar la siguiente escena.
Input.action_release(&"move_left")
Input.action_release(&"move_right")
Input.action_release(&"jump")
Input.action_release(&"attack")
Input.action_release(&"ground_pound")
Input.action_release(&"special_attack")
## Determina que control horizontal contiene la posicion tactil.
func _direction_at(position: Vector2) -> StringName:
if move_left_button.get_global_rect().has_point(position):
return &"move_left"
if move_right_button.get_global_rect().has_point(position):
return &"move_right"
return &""
## Activa una direccion y desactiva la opuesta.
func _press_direction(action: StringName) -> void:
var other_action: StringName = &"move_right" if action == &"move_left" else &"move_left"
Input.action_release(other_action)
Input.action_press(action)
## Suelta ambas direcciones y libera el dedo reservado.
func _release_direction() -> void:
Input.action_release(&"move_left")
Input.action_release(&"move_right")
direction_touch_index = -1
## Conecta pulsacion y liberacion con la accion indicada.
func _bind_button(button: Button, action: StringName) -> void:
button.button_down.connect(_press_action.bind(action))
button.button_up.connect(_release_action.bind(action))
## Envia una pulsacion al InputMap compartido.
func _press_action(action: StringName) -> void:
Input.action_press(action)
## Libera la accion tactil en el InputMap compartido.
func _release_action(action: StringName) -> void:
Input.action_release(action)