Files
mortal-maze-the-game/tests/test_mobile_controls_runtime.gd

159 lines
8.0 KiB
GDScript

extends SceneTree
## godot --headless --path . --script res://tests/test_mobile_controls_runtime.gd
const MOBILE_SCENE: PackedScene = preload("res://ui/mobile_controls/mobile_controls.tscn")
const MobileControls = preload("res://ui/mobile_controls/mobile_controls.gd")
const TouchSurface = preload("res://ui/mobile_controls/touch_surface.gd")
const ACTIONS: Array[StringName] = [&"jump", &"attack", &"ground_pound", &"special_attack"]
var _failures: int = 0
var _mobile: MobileControls
var _surface: TouchSurface
func _initialize() -> void:
call_deferred("_run")
func _run() -> void:
preload("res://tests/runtime_test_profile.gd").prepare(self)
_mobile = MOBILE_SCENE.instantiate() as MobileControls
_mobile.show_on_desktop = true
root.add_child(_mobile)
await process_frame
_mobile.notification(Node.NOTIFICATION_APPLICATION_FOCUS_IN)
_surface = _mobile.get_node("Controls") as TouchSurface
for area: Rect2 in [Rect2(0, 0, 1280, 720), Rect2(0, 0, 1600, 720), Rect2(48, 0, 1504, 690)]:
_check_layout(area)
# Volvemos a las coordenadas reales del viewport para probar _input.
_mobile.call("_refresh_layout")
var move_area: Rect2 = _surface.movement_area()
var first: Vector2 = move_area.position + move_area.size * Vector2(0.40, 0.62)
var second: Vector2 = move_area.position + move_area.size * Vector2(0.67, 0.42)
var travel: float = _surface.joystick_travel()
var jump: Vector2 = _surface.button_center(&"jump")
var attack: Vector2 = _surface.button_center(&"attack")
var pound: Vector2 = _surface.button_center(&"ground_pound")
var solo: Vector2 = _surface.button_center(&"special_attack")
# Primer toque donde cae el dedo, no sobre el joystick dibujado en reposo.
_touch(2, first, true)
_check(_mobile.direction_touch_index == 2, "El dedo izquierdo reserva el joystick")
_check(_surface._joystick_center.is_equal_approx(first), "El joystick nace en el toque")
_check(is_zero_approx(Input.get_axis(&"move_left", &"move_right")), "Tocar sin arrastrar no mueve")
_drag(2, first + Vector2(travel * 0.10, 0))
_check(is_zero_approx(Input.get_axis(&"move_left", &"move_right")), "La zona muerta evita deriva")
_drag(2, first + Vector2(travel * 0.50, 0))
var partial: float = Input.get_axis(&"move_left", &"move_right")
_check(partial > 0.0 and partial < 1.0, "El control es analogico")
_drag(2, first + Vector2(travel * 2.0, 0))
_check(is_equal_approx(Input.get_axis(&"move_left", &"move_right"), 1.0), "El arrastre fuera del circulo sigue funcionando")
_touch(5, jump, true)
_touch(8, attack, true)
_check(Input.is_action_pressed(&"jump") and Input.is_action_pressed(&"attack"), "Salto y ataque tienen dedos propios")
_check(Input.is_action_pressed(&"move_right"), "Los botones no interrumpen el movimiento")
_touch(9, jump, true)
_touch(9, jump, false)
_check(Input.is_action_pressed(&"jump"), "Un segundo dedo no roba el salto")
_drag(5, attack)
_check(Input.is_action_pressed(&"jump"), "Deslizar fuera mantiene el salto")
_touch(5, attack, false)
_check(not Input.is_action_pressed(&"jump") and Input.is_action_pressed(&"attack"), "Soltar salto no suelta ataque")
_touch(8, attack, false, true)
_check(not Input.is_action_pressed(&"attack"), "Un evento cancelado suelta su boton")
# Abajo es una direccion propia y no se activa con una diagonal ligera.
_drag(2, first + Vector2(travel * 0.8, travel * 0.3))
_check(Input.is_action_pressed(&"move_right") and not Input.is_action_pressed(&"move_down"), "Diagonal horizontal no agacha")
_drag(2, first + Vector2(0, travel * 0.50))
_check(not Input.is_action_pressed(&"move_down"), "Umbral de agachado")
_drag(2, first + Vector2(0, travel * 0.75))
_check(Input.is_action_pressed(&"move_down") and is_zero_approx(Input.get_axis(&"move_left", &"move_right")), "Abajo frena y agacha")
_drag(2, first + Vector2(0, travel * 0.45))
_check(Input.is_action_pressed(&"move_down"), "Histeresis mantiene agachado")
_touch(5, jump, true)
_check(Input.is_action_pressed(&"jump") and Input.is_action_pressed(&"move_down"), "Abajo y salto tienen dedos independientes")
_touch(5, jump, false)
_drag(2, first + Vector2(0, travel * 0.20))
_check(not Input.is_action_pressed(&"move_down"), "Volver al centro libera abajo")
_drag(2, first + Vector2(0, travel))
_drag(2, first - Vector2(travel, 0))
_check(not Input.is_action_pressed(&"move_down"), "Cambiar a izquierda libera abajo")
_check(Input.is_action_pressed(&"move_left") and not Input.is_action_pressed(&"move_right"), "Giro sin dos direcciones activas")
_drag(2, first)
_check(is_zero_approx(Input.get_axis(&"move_left", &"move_right")), "Volver al centro frena")
_touch(2, first, false)
_touch(3, second, true)
_check(_surface._joystick_center.is_equal_approx(second), "El siguiente toque recoloca el joystick")
_drag(3, second + Vector2(0, travel))
_check(Input.is_action_pressed(&"move_down"), "El segundo origen permite agacharse")
_touch(10, pound, true)
_touch(11, solo, true)
_check(Input.is_action_pressed(&"ground_pound") and Input.is_action_pressed(&"special_attack"), "Caer y solo conservan sus acciones")
paused = true
await process_frame
_mobile.call("_sync_visibility")
_check(not _surface.visible and _mobile.direction_touch_index == -1, "La pausa oculta y libera el joystick")
_check(not Input.is_action_pressed(&"move_left") and not Input.is_action_pressed(&"move_down") and not Input.is_action_pressed(&"ground_pound") and not Input.is_action_pressed(&"special_attack"), "Pausa no deja teclas tactiles atascadas")
paused = false
await process_frame
_mobile.call("_sync_visibility")
_drag(3, second + Vector2(travel, 0))
_check(is_zero_approx(Input.get_axis(&"move_left", &"move_right")), "Reanudar requiere un toque nuevo")
_touch(4, first, true)
_drag(4, first + Vector2(0, travel))
_touch(6, jump, true)
_mobile.notification(Node.NOTIFICATION_APPLICATION_FOCUS_OUT)
_check(not Input.is_action_pressed(&"jump") and not Input.is_action_pressed(&"move_down"), "Perder foco libera acciones y abajo")
_mobile.notification(Node.NOTIFICATION_APPLICATION_FOCUS_IN)
_touch(7, attack, true)
_check(Input.is_action_pressed(&"attack"), "Tras recuperar foco funcionan nuevos toques")
_mobile.queue_free()
await process_frame
await process_frame
_check(not Input.is_action_pressed(&"attack"), "Salir de la escena libera el boton")
if _failures == 0:
print("OK MOBILE: distribucion, joystick flotante analogico, multitouch, cancelacion, pausa y salida")
quit(0 if _failures == 0 else 1)
func _check_layout(area: Rect2) -> void:
_surface.set_safe_rect(area)
for action: StringName in ACTIONS:
var center: Vector2 = _surface.button_center(action)
var radius: float = _surface.button_radius(action)
var padding: float = TouchSurface.HIT_PADDING * _surface.layout_scale()
var extent := Vector2.ONE * (radius + padding)
_check(area.encloses(Rect2(center - extent, extent * 2.0)), "Boton dentro de zona segura: " + str(action))
_check(center.x > area.get_center().x, "Acciones en la mitad derecha")
_check(_surface.action_at(center) == action, "Hit test en el centro")
_check(_surface.action_at(center + Vector2(radius * 0.88, radius * 0.88)) != action, "El hitbox es circular, no cuadrado")
for other: StringName in ACTIONS:
if other == action:
continue
_check(center.distance_to(_surface.button_center(other)) > radius + _surface.button_radius(other) + padding * 2.0, "No se superponen las areas tactiles")
_check(not _surface.movement_area().has_point(area.position + Vector2(20, 20)), "El HUD superior queda libre")
func _touch(index: int, local: Vector2, pressed: bool, canceled: bool = false) -> void:
var event := InputEventScreenTouch.new()
event.index = index
event.position = _surface.get_global_transform_with_canvas() * local
event.pressed = pressed
event.canceled = canceled
root.push_input(event, true)
func _drag(index: int, local: Vector2) -> void:
var event := InputEventScreenDrag.new()
event.index = index
event.position = _surface.get_global_transform_with_canvas() * local
root.push_input(event, true)
func _check(condition: bool, message: String) -> void:
if not condition:
_failures += 1
push_error(message)