mejoras en interfaz y movimento de personaje
This commit is contained in:
1 parent
7a8b03e760
commit
f0fa01607c
29 files changed
+504320
-280
No files matched your search
@@ -1,110 +1,197 @@
|
||||
extends CanvasLayer
|
||||
|
||||
## Adaptador tactil multitouch al InputMap existente. El dibujo no captura eventos.
|
||||
const TouchSurface = preload("res://ui/mobile_controls/touch_surface.gd")
|
||||
const MOUSE_FINGER: int = -2
|
||||
|
||||
## La misma entrada del teclado, presentada como placas de metal tactiles.
|
||||
## Se muestran en pantallas tactiles; el Inspector permite probarlas en escritorio.
|
||||
## Permite probar con raton en escritorio sin modificar la configuracion del proyecto.
|
||||
@export var show_on_desktop: bool = false
|
||||
## Fraccion del recorrido horizontal ignorada alrededor del centro.
|
||||
@export_range(0.0, 0.45, 0.01) var joystick_deadzone: float = 0.18
|
||||
## Fraccion del recorrido a partir de la cual se alcanza la velocidad maxima.
|
||||
@export_range(0.5, 1.0, 0.01) var joystick_full_speed_at: float = 0.90
|
||||
## Umbrales separados: la histeresis evita alternar agachado/de pie por un temblor.
|
||||
@export_range(0.3, 0.9, 0.01) var joystick_crouch_at: float = 0.55
|
||||
@export_range(0.1, 0.8, 0.01) var joystick_crouch_release_at: float = 0.35
|
||||
|
||||
@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
|
||||
@onready var controls: TouchSurface = $Controls
|
||||
|
||||
var direction_touch_index: int = -1
|
||||
var _direction_action: StringName = &""
|
||||
var _origin: Vector2
|
||||
var _finger_actions: Dictionary = {}
|
||||
var _player: Node
|
||||
var _touch_detected: bool = false
|
||||
var _foreground: bool = true
|
||||
|
||||
|
||||
## Asocia botones con las mismas acciones utilizadas por el teclado.
|
||||
func _ready() -> void:
|
||||
$Controls.visible = show_on_desktop or DisplayServer.is_touchscreen_available() or OS.has_feature("mobile")
|
||||
_bind_button(jump_button, &"jump")
|
||||
_bind_button(attack_button, &"attack")
|
||||
_bind_button(ground_pound_button, &"ground_pound")
|
||||
_bind_button(special_attack_button, &"special_attack")
|
||||
# Debe seguir procesando al pausar: libera los dedos antes de mostrar el menu.
|
||||
process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
controls.resized.connect(_refresh_layout)
|
||||
_refresh_layout()
|
||||
_sync_visibility()
|
||||
|
||||
|
||||
## Indica el enfriamiento del golpe aereo sin alterar la entrada del resto de botones.
|
||||
func _process(_delta: float) -> void:
|
||||
_sync_visibility()
|
||||
if not controls.visible:
|
||||
return
|
||||
if not is_instance_valid(_player):
|
||||
_player = get_tree().get_first_node_in_group(&"player")
|
||||
if not is_instance_valid(_player):
|
||||
ground_pound_button.text = "CAER"
|
||||
ground_pound_button.modulate = Color.WHITE
|
||||
return
|
||||
var remaining: float = float(_player.get("ground_pound_cooldown_timer"))
|
||||
ground_pound_button.text = "CAER\n%ds" % ceili(remaining) if remaining > 0.0 else "CAER"
|
||||
ground_pound_button.modulate = Color(0.55, 0.55, 0.55, 0.85) if remaining > 0.0 else Color.WHITE
|
||||
var remaining: float = float(_player.get("ground_pound_cooldown_timer")) if is_instance_valid(_player) else 0.0
|
||||
controls.set_pound_seconds(ceili(maxf(remaining, 0.0)))
|
||||
|
||||
|
||||
func _sync_visibility() -> void:
|
||||
var enabled: bool = visible and _foreground and not get_tree().paused and (
|
||||
show_on_desktop or _touch_detected or DisplayServer.is_touchscreen_available() or OS.has_feature("mobile")
|
||||
)
|
||||
if not enabled:
|
||||
_release_all()
|
||||
controls.visible = enabled
|
||||
|
||||
|
||||
func _refresh_layout() -> void:
|
||||
_release_all()
|
||||
var area := Rect2(Vector2.ZERO, controls.size)
|
||||
if OS.has_feature("mobile"):
|
||||
var safe_screen := Rect2(DisplayServer.get_display_safe_area())
|
||||
if safe_screen.has_area():
|
||||
# La zona segura del SO esta en pixeles de pantalla; convertirla al mismo
|
||||
# espacio logico que el dibujo y los eventos (incluye stretch/expand).
|
||||
var to_screen: Transform2D = get_viewport().get_screen_transform() * controls.get_global_transform_with_canvas()
|
||||
var safe_local: Rect2 = to_screen.affine_inverse() * safe_screen
|
||||
var clipped: Rect2 = area.intersection(safe_local)
|
||||
if clipped.has_area():
|
||||
area = clipped
|
||||
controls.set_safe_rect(area)
|
||||
|
||||
|
||||
## Mantiene un unico dedo para direccion y permite deslizarlo entre ambos lados.
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventScreenTouch or event is InputEventScreenDrag:
|
||||
_touch_detected = true
|
||||
_sync_visibility()
|
||||
if not controls.visible:
|
||||
return
|
||||
var handled: bool = false
|
||||
if event is InputEventScreenTouch:
|
||||
$Controls.show()
|
||||
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()
|
||||
if touch.canceled or not touch.pressed:
|
||||
handled = _release_touch(touch.index)
|
||||
else:
|
||||
handled = _begin_touch(touch.index, _local_point(touch.position))
|
||||
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)
|
||||
handled = _drag_touch(drag.index, _local_point(drag.position))
|
||||
# El raton es solo una ayuda de escritorio. No duplicar el mouse sintetizado
|
||||
# por Android, que se mantiene habilitado para los menus normales del juego.
|
||||
elif show_on_desktop and event.device != InputEvent.DEVICE_ID_EMULATION:
|
||||
if event is InputEventMouseButton:
|
||||
var mouse_button := event as InputEventMouseButton
|
||||
if mouse_button.button_index == MOUSE_BUTTON_LEFT:
|
||||
if mouse_button.pressed:
|
||||
handled = _begin_touch(MOUSE_FINGER, _local_point(mouse_button.position))
|
||||
else:
|
||||
handled = _release_touch(MOUSE_FINGER)
|
||||
elif event is InputEventMouseMotion:
|
||||
var motion := event as InputEventMouseMotion
|
||||
handled = _drag_touch(MOUSE_FINGER, _local_point(motion.position))
|
||||
if handled:
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
|
||||
func _local_point(point: Vector2) -> Vector2:
|
||||
return controls.get_global_transform_with_canvas().affine_inverse() * point
|
||||
|
||||
|
||||
func _begin_touch(index: int, point: Vector2) -> bool:
|
||||
if index == direction_touch_index or _finger_actions.has(index):
|
||||
return true
|
||||
var action: StringName = controls.action_at(point)
|
||||
if action != &"":
|
||||
# No se roba un boton ya pulsado por otro dedo ni se libera al soltar ese segundo dedo.
|
||||
if not _finger_actions.values().has(action):
|
||||
_finger_actions[index] = action
|
||||
controls.set_action_pressed(action, true)
|
||||
Input.action_press(action)
|
||||
return true
|
||||
if controls.movement_area().has_point(point):
|
||||
if direction_touch_index == -1:
|
||||
direction_touch_index = index
|
||||
_origin = point
|
||||
# El origen es EXACTAMENTE el primer toque, incluso lejos de la pista visual.
|
||||
controls.set_joystick(_origin, Vector2.ZERO, true)
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func _drag_touch(index: int, point: Vector2) -> bool:
|
||||
if index == direction_touch_index:
|
||||
var offset: Vector2 = (point - _origin).limit_length(controls.joystick_travel())
|
||||
controls.set_joystick(_origin, offset, true)
|
||||
var axes: Vector2 = offset / maxf(controls.joystick_travel(), 1.0)
|
||||
# Abajo debe predominar sobre el eje X; las diagonales poco profundas
|
||||
# siguen sirviendo para correr. Solo se emite una direccion a la vez.
|
||||
var threshold: float = joystick_crouch_at
|
||||
if _direction_action == &"move_down":
|
||||
threshold = minf(joystick_crouch_at, joystick_crouch_release_at)
|
||||
if axes.y >= threshold and axes.y >= absf(axes.x):
|
||||
_set_direction(&"move_down", 1.0)
|
||||
return true
|
||||
var horizontal: float = clampf(axes.x, -1.0, 1.0)
|
||||
var strength: float = clampf(inverse_lerp(joystick_deadzone, joystick_full_speed_at, absf(horizontal)), 0.0, 1.0)
|
||||
var action: StringName = &""
|
||||
if strength > 0.0:
|
||||
action = &"move_left" if horizontal < 0.0 else &"move_right"
|
||||
_set_direction(action, strength)
|
||||
return true
|
||||
# El boton inicial conserva el dedo aunque se deslice fuera; no se pierde un
|
||||
# salto mantenido ni se ejecuta accidentalmente otra accion al cruzarla.
|
||||
return _finger_actions.has(index)
|
||||
|
||||
|
||||
func _set_direction(action: StringName, strength: float = 0.0) -> void:
|
||||
if _direction_action != action and _direction_action != &"":
|
||||
Input.action_release(_direction_action)
|
||||
_direction_action = action
|
||||
if action != &"":
|
||||
Input.action_press(action, strength)
|
||||
|
||||
|
||||
func _release_touch(index: int) -> bool:
|
||||
if index == direction_touch_index:
|
||||
_set_direction(&"")
|
||||
direction_touch_index = -1
|
||||
controls.set_joystick(Vector2.ZERO, Vector2.ZERO, false)
|
||||
return true
|
||||
if _finger_actions.has(index):
|
||||
var action: StringName = _finger_actions[index]
|
||||
_finger_actions.erase(index)
|
||||
Input.action_release(action)
|
||||
controls.set_action_pressed(action, false)
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func _release_all() -> void:
|
||||
# Liberar SOLO las acciones activadas por esta instancia, sin un reset global.
|
||||
if direction_touch_index != -1:
|
||||
_release_touch(direction_touch_index)
|
||||
for index: int in _finger_actions.keys():
|
||||
_release_touch(index)
|
||||
|
||||
|
||||
func _notification(what: int) -> void:
|
||||
if not is_node_ready():
|
||||
return
|
||||
if what == NOTIFICATION_APPLICATION_FOCUS_OUT or what == NOTIFICATION_APPLICATION_PAUSED or what == NOTIFICATION_WM_WINDOW_FOCUS_OUT:
|
||||
_foreground = false
|
||||
_release_all()
|
||||
elif what == NOTIFICATION_APPLICATION_FOCUS_IN or what == NOTIFICATION_APPLICATION_RESUMED or what == NOTIFICATION_WM_WINDOW_FOCUS_IN:
|
||||
_foreground = true
|
||||
_refresh_layout()
|
||||
|
||||
|
||||
## 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)
|
||||
_release_all()
|
||||
@@ -1,35 +1,11 @@
|
||||
[gd_scene load_steps=7 format=3 uid="uid://g7h185yageqr"]
|
||||
[gd_scene load_steps=4 format=3 uid="uid://g7h185yageqr"]
|
||||
|
||||
[ext_resource type="Script" path="res://ui/editor_ui_visibility.gd" id="editor_ui_visibility"]
|
||||
[ext_resource type="Script" path="res://ui/mobile_controls/mobile_controls.gd" id="1_controls"]
|
||||
[ext_resource type="Theme" path="res://ui/menu/menu_theme.tres" id="theme"]
|
||||
[ext_resource type="Texture2D" path="res://assets/ashen/ui/button_plate.png" id="plate"]
|
||||
|
||||
[sub_resource type="StyleBoxTexture" id="ButtonNormal"]
|
||||
texture = ExtResource("plate")
|
||||
texture_margin_left = 24.0
|
||||
texture_margin_top = 18.0
|
||||
texture_margin_right = 28.0
|
||||
texture_margin_bottom = 16.0
|
||||
content_margin_left = 12.0
|
||||
content_margin_top = 12.0
|
||||
content_margin_right = 12.0
|
||||
content_margin_bottom = 12.0
|
||||
modulate_color = Color(0.68, 0.61, 0.48, 0.78)
|
||||
|
||||
[sub_resource type="StyleBoxTexture" id="ButtonPressed"]
|
||||
texture = ExtResource("plate")
|
||||
texture_margin_left = 24.0
|
||||
texture_margin_top = 18.0
|
||||
texture_margin_right = 28.0
|
||||
texture_margin_bottom = 16.0
|
||||
content_margin_left = 12.0
|
||||
content_margin_top = 14.0
|
||||
content_margin_right = 12.0
|
||||
content_margin_bottom = 10.0
|
||||
modulate_color = Color(1, 0.78, 0.51, 1)
|
||||
[ext_resource type="Script" path="res://ui/mobile_controls/touch_surface.gd" id="2_surface"]
|
||||
|
||||
[node name="MobileControls" type="CanvasLayer"]
|
||||
process_mode = 3
|
||||
layer = 100
|
||||
script = ExtResource("1_controls")
|
||||
|
||||
@@ -44,136 +20,4 @@ anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
mouse_filter = 2
|
||||
theme = ExtResource("theme")
|
||||
|
||||
[node name="MoveLeft" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 0.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 0.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = 24.0
|
||||
offset_top = -140.0
|
||||
offset_right = 136.0
|
||||
offset_bottom = -28.0
|
||||
grow_horizontal = 1
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 32
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = "<<"
|
||||
|
||||
[node name="MoveRight" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 0.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 0.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = 148.0
|
||||
offset_top = -140.0
|
||||
offset_right = 260.0
|
||||
offset_bottom = -28.0
|
||||
grow_horizontal = 1
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 32
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = ">>"
|
||||
|
||||
[node name="Jump" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -140.0
|
||||
offset_top = -264.0
|
||||
offset_right = -28.0
|
||||
offset_bottom = -152.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 16
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = "SALTO"
|
||||
|
||||
[node name="Attack" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -264.0
|
||||
offset_top = -140.0
|
||||
offset_right = -152.0
|
||||
offset_bottom = -28.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 16
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = "ATAQUE"
|
||||
|
||||
[node name="GroundPound" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -140.0
|
||||
offset_top = -140.0
|
||||
offset_right = -28.0
|
||||
offset_bottom = -28.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 16
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = "CAER"
|
||||
|
||||
[node name="SpecialAttack" type="Button" parent="Controls"]
|
||||
layout_mode = 1
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -264.0
|
||||
offset_top = -264.0
|
||||
offset_right = -152.0
|
||||
offset_bottom = -152.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
focus_mode = 0
|
||||
theme_override_colors/font_color = Color(0.88, 0.82, 0.68, 0.92)
|
||||
theme_override_colors/font_pressed_color = Color(1, 0.94, 0.77, 1)
|
||||
theme_override_font_sizes/font_size = 16
|
||||
theme_override_styles/normal = SubResource("ButtonNormal")
|
||||
theme_override_styles/pressed = SubResource("ButtonPressed")
|
||||
theme_override_styles/hover = SubResource("ButtonNormal")
|
||||
keep_pressed_outside = true
|
||||
text = "SOLO"
|
||||
script = ExtResource("2_surface")
|
||||
@@ -0,0 +1,187 @@
|
||||
@tool
|
||||
extends Control
|
||||
|
||||
## Presentacion y geometria compartida por el dibujo y el hit test.
|
||||
## No lee Input ni ejecuta logica del jugador; tambien se puede ver en el editor.
|
||||
const BUTTONS := {
|
||||
&"jump": {"offset": Vector2(105, 110), "radius": 78.0, "label": "SALTO"},
|
||||
&"attack": {"offset": Vector2(285, 135), "radius": 76.0, "label": "GOLPE"},
|
||||
&"ground_pound": {"offset": Vector2(205, 280), "radius": 62.0, "label": "CAER"},
|
||||
&"special_attack": {"offset": Vector2(90, 360), "radius": 58.0, "label": "SOLO"},
|
||||
}
|
||||
const BASE_SIZE := Vector2(1280, 720)
|
||||
const GOLD := Color(0.91, 0.75, 0.48, 0.90)
|
||||
const IVORY := Color(1.0, 0.92, 0.76, 0.96)
|
||||
const BASE_JOYSTICK_RADIUS: float = 92.0
|
||||
const BASE_JOYSTICK_TRAVEL: float = 64.0
|
||||
const HIT_PADDING: float = 8.0
|
||||
|
||||
var _safe_rect: Rect2
|
||||
var _joystick_active: bool = false
|
||||
var _joystick_center: Vector2
|
||||
var _joystick_offset: Vector2
|
||||
var _pressed: Dictionary = {}
|
||||
var _pound_seconds: int = 0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
resized.connect(_on_resized)
|
||||
_on_resized()
|
||||
|
||||
|
||||
func _on_resized() -> void:
|
||||
set_safe_rect(Rect2(Vector2.ZERO, size))
|
||||
|
||||
|
||||
func set_safe_rect(rect: Rect2) -> void:
|
||||
_safe_rect = rect if rect.has_area() else Rect2(Vector2.ZERO, size)
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func layout_scale() -> float:
|
||||
# El viewport usa canvas_items/expand: las medidas se mantienen en 16:9 y 20:9.
|
||||
# Si la zona segura es mas pequena, todo se reduce de forma proporcionada.
|
||||
return maxf(0.1, minf(1.0, minf(_safe_rect.size.x / BASE_SIZE.x, _safe_rect.size.y / BASE_SIZE.y)))
|
||||
|
||||
|
||||
func joystick_travel() -> float:
|
||||
return BASE_JOYSTICK_TRAVEL * layout_scale()
|
||||
|
||||
|
||||
func movement_area() -> Rect2:
|
||||
# El tercio superior queda libre para el HUD. Toda la mitad izquierda inferior
|
||||
# permite iniciar el joystick, no solo el circulo que sirve de pista visual.
|
||||
return Rect2(
|
||||
_safe_rect.position + Vector2(0.0, _safe_rect.size.y * 0.34),
|
||||
Vector2(_safe_rect.size.x * 0.5, _safe_rect.size.y * 0.66)
|
||||
)
|
||||
|
||||
|
||||
func button_center(action: StringName) -> Vector2:
|
||||
var data: Dictionary = BUTTONS[action]
|
||||
var offset: Vector2 = data["offset"]
|
||||
return _safe_rect.end - offset * layout_scale()
|
||||
|
||||
|
||||
func button_radius(action: StringName) -> float:
|
||||
return float(BUTTONS[action]["radius"]) * layout_scale()
|
||||
|
||||
|
||||
func action_at(point: Vector2) -> StringName:
|
||||
if not _safe_rect.has_point(point):
|
||||
return &""
|
||||
var closest: StringName = &""
|
||||
var best: float = INF
|
||||
for action: StringName in BUTTONS:
|
||||
var distance: float = point.distance_to(button_center(action))
|
||||
if distance <= button_radius(action) + HIT_PADDING * layout_scale() and distance < best:
|
||||
closest = action
|
||||
best = distance
|
||||
return closest
|
||||
|
||||
|
||||
func set_joystick(center: Vector2, offset: Vector2, active: bool) -> void:
|
||||
_joystick_center = center
|
||||
_joystick_offset = offset.limit_length(joystick_travel())
|
||||
_joystick_active = active
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func set_action_pressed(action: StringName, pressed: bool) -> void:
|
||||
if pressed:
|
||||
_pressed[action] = true
|
||||
else:
|
||||
_pressed.erase(action)
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func set_pound_seconds(seconds: int) -> void:
|
||||
if _pound_seconds != seconds:
|
||||
_pound_seconds = seconds
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
if not _safe_rect.has_area():
|
||||
return
|
||||
_draw_joystick()
|
||||
for action: StringName in BUTTONS:
|
||||
_draw_action(action)
|
||||
# No dejamos transformaciones pendientes en el CanvasItem.
|
||||
draw_set_transform(Vector2.ZERO)
|
||||
|
||||
|
||||
func _draw_joystick() -> void:
|
||||
var factor: float = layout_scale()
|
||||
var center: Vector2 = _joystick_center if _joystick_active else (
|
||||
_safe_rect.position + Vector2(144.0 * factor, _safe_rect.size.y - 138.0 * factor)
|
||||
)
|
||||
var opacity: float = 1.0 if _joystick_active else 0.48
|
||||
draw_set_transform(center, 0.0, Vector2.ONE * factor)
|
||||
draw_circle(Vector2.ZERO, BASE_JOYSTICK_RADIUS, Color(0.045, 0.034, 0.055, 0.54 * opacity), true, -1.0, true)
|
||||
draw_arc(Vector2.ZERO, BASE_JOYSTICK_RADIUS - 2.0, 0.0, TAU, 96, Color(GOLD, GOLD.a * opacity), 3.0, true)
|
||||
draw_arc(Vector2.ZERO, BASE_JOYSTICK_RADIUS - 11.0, 0.0, TAU, 96, Color(GOLD, 0.22 * opacity), 1.5, true)
|
||||
# Izquierda/derecha para mover; abajo para agacharse y bajar plataformas.
|
||||
for side: float in [-1.0, 1.0]:
|
||||
var tip := Vector2(72.0 * side, 0.0)
|
||||
draw_polyline(PackedVector2Array([tip + Vector2(-10.0 * side, -9.0), tip, tip + Vector2(-10.0 * side, 9.0)]), Color(IVORY, 0.65 * opacity), 3.0, true)
|
||||
var down_tip := Vector2(0.0, 72.0)
|
||||
draw_polyline(PackedVector2Array([down_tip + Vector2(-9.0, -10.0), down_tip, down_tip + Vector2(9.0, -10.0)]), Color(IVORY, 0.65 * opacity), 3.0, true)
|
||||
var knob: Vector2 = _joystick_offset / factor if _joystick_active else Vector2.ZERO
|
||||
draw_circle(knob + Vector2(0, 3), 34.0, Color(0.0, 0.0, 0.0, 0.30 * opacity), true, -1.0, true)
|
||||
draw_circle(knob, 32.0, Color(0.37, 0.27, 0.20, 0.86 * opacity), true, -1.0, true)
|
||||
draw_arc(knob, 30.0, 0.0, TAU, 64, Color(IVORY, 0.84 * opacity), 3.0, true)
|
||||
draw_circle(knob, 6.0, Color(IVORY, 0.75 * opacity), true, -1.0, true)
|
||||
|
||||
|
||||
func _draw_action(action: StringName) -> void:
|
||||
var data: Dictionary = BUTTONS[action]
|
||||
var radius: float = float(data["radius"])
|
||||
var pressed: bool = _pressed.has(action)
|
||||
var cooling: bool = action == &"ground_pound" and _pound_seconds > 0
|
||||
var opacity: float = 0.52 if cooling else 1.0
|
||||
var rim: Color = IVORY if pressed else GOLD
|
||||
var fill := Color(0.38, 0.24, 0.14, 0.86) if pressed else Color(0.055, 0.041, 0.060, 0.64)
|
||||
draw_set_transform(button_center(action), 0.0, Vector2.ONE * layout_scale())
|
||||
draw_circle(Vector2(0, 3), radius, Color(0.0, 0.0, 0.0, 0.28 * opacity), true, -1.0, true)
|
||||
draw_circle(Vector2.ZERO, radius - 1.0, Color(fill, fill.a * opacity), true, -1.0, true)
|
||||
draw_arc(Vector2.ZERO, radius - 2.0, 0.0, TAU, 96, Color(rim, rim.a * opacity), 4.0 if pressed else 2.5, true)
|
||||
draw_arc(Vector2.ZERO, radius - 10.0, 0.0, TAU, 96, Color(rim, 0.24 * opacity), 1.5, true)
|
||||
# Iconos vectoriales: distinguibles sin tener que leer el texto durante el combate.
|
||||
var ink := Color(IVORY, IVORY.a * opacity)
|
||||
_draw_icon(action, Vector2(0.0, -12.0), radius / 78.0, ink)
|
||||
var caption: String = "%ds" % _pound_seconds if cooling else str(data["label"])
|
||||
var font: Font = ThemeDB.fallback_font
|
||||
var font_size: int = 18 if radius >= 70.0 else 16
|
||||
var text_width: float = font.get_string_size(caption, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x
|
||||
draw_string(font, Vector2(-text_width * 0.5, radius * 0.58), caption, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, ink)
|
||||
|
||||
|
||||
func _draw_icon(action: StringName, origin: Vector2, factor: float, ink: Color) -> void:
|
||||
# Los vertices estan en coordenadas de icono, no en las del viewport.
|
||||
match action:
|
||||
&"jump":
|
||||
_stroke(origin, factor, [Vector2(0, 25), Vector2(0, -24)], ink)
|
||||
_stroke(origin, factor, [Vector2(-19, -5), Vector2(0, -24), Vector2(19, -5)], ink)
|
||||
&"attack":
|
||||
# Puno cerrado, no una espada: corresponde al ataque real del personaje.
|
||||
_stroke(origin, factor, [Vector2(-22, 7), Vector2(-22, -14), Vector2(-15, -24), Vector2(16, -24), Vector2(24, -14), Vector2(24, 6), Vector2(13, 23), Vector2(-12, 23), Vector2(-22, 7)], ink)
|
||||
for x: float in [-10.0, 1.0, 12.0]:
|
||||
_stroke(origin, factor, [Vector2(x, -23), Vector2(x, -5)], ink)
|
||||
_stroke(origin, factor, [Vector2(-21, 4), Vector2(-8, -4), Vector2(10, -4), Vector2(13, 8), Vector2(-3, 12)], ink)
|
||||
&"ground_pound":
|
||||
_stroke(origin, factor, [Vector2(0, -26), Vector2(0, 12)], ink)
|
||||
_stroke(origin, factor, [Vector2(-17, -4), Vector2(0, 13), Vector2(17, -4)], ink)
|
||||
_stroke(origin, factor, [Vector2(-25, 25), Vector2(25, 25)], ink)
|
||||
&"special_attack":
|
||||
var points := PackedVector2Array()
|
||||
for vertex: Vector2 in [Vector2(5, -30), Vector2(-22, 5), Vector2(-2, 5), Vector2(-9, 30), Vector2(24, -10), Vector2(3, -10)]:
|
||||
points.append(origin + vertex * factor)
|
||||
draw_colored_polygon(points, ink)
|
||||
|
||||
|
||||
func _stroke(origin: Vector2, factor: float, vertices: Array, ink: Color) -> void:
|
||||
var points := PackedVector2Array()
|
||||
for vertex: Vector2 in vertices:
|
||||
points.append(origin + vertex * factor)
|
||||
draw_polyline(points, ink, 4.5 * factor, true)
|
||||
Reference in new issue
Block a user