170 lines
6.1 KiB
GDScript
170 lines
6.1 KiB
GDScript
@tool
|
|
extends Area2D
|
|
class_name Pickup
|
|
|
|
signal picked_up(pickup: Pickup, data: PickupData)
|
|
|
|
# Los drops permanecen visibles brevemente antes de poder recogerse.
|
|
# Se recuerda al jugador que los toque durante esa espera, aunque continue corriendo.
|
|
const DROP_COLLECTION_DELAY_SECONDS: float = 0.75
|
|
|
|
## Recurso compartido de efecto; para cambiar solo esta instancia, hazlo unico.
|
|
@export var pickup_data: PickupData:
|
|
set(value):
|
|
if pickup_data != null and pickup_data.changed.is_connected(_update_preview):
|
|
pickup_data.changed.disconnect(_update_preview)
|
|
pickup_data = value
|
|
if pickup_data != null and not pickup_data.changed.is_connected(_update_preview):
|
|
pickup_data.changed.connect(_update_preview)
|
|
if is_inside_tree():
|
|
call_deferred("_update_preview")
|
|
## Espera antes de permitir la recogida; los objetos colocados usan cero.
|
|
@export var collection_delay_seconds: float = 0.0
|
|
## Aceleracion de caida del objeto; conserva el comportamiento previo.
|
|
@export var fall_speed: float = 150.0
|
|
## Amplitud vertical de la oscilacion al apoyarse en el suelo.
|
|
@export var bob_amplitude: float = 4.0
|
|
## Velocidad angular de esa oscilacion.
|
|
@export var bob_speed: float = 3.0
|
|
## Solo presentacion: los nombres aparecen al acercarse al objeto.
|
|
@export_range(32.0, 320.0, 2.0) var label_visibility_radius: float = 110.0
|
|
|
|
var velocity: Vector2 = Vector2.ZERO
|
|
var time: float = 0.0
|
|
var start_y: float = 0.0
|
|
var grounded: bool = false
|
|
var _can_collect: bool = false
|
|
var _pending_collector: Node2D # Jugador que toco el objeto durante la espera.
|
|
var _label_viewer: Node2D
|
|
|
|
@onready var label: Label = $Label
|
|
@onready var sprite: Sprite2D = $Sprite2D
|
|
@onready var placeholder: ColorRect = $Sprite2D/ColorRect
|
|
|
|
|
|
## Prepara la vista previa; solo en partida activa la recogida y sus senales.
|
|
func _ready() -> void:
|
|
_update_preview()
|
|
if Engine.is_editor_hint():
|
|
return
|
|
label.visible = false
|
|
add_to_group("pickup")
|
|
body_entered.connect(_on_body_entered)
|
|
|
|
if pickup_data == null:
|
|
push_error("Pickup without PickupData: %s" % get_path())
|
|
label.text = "SIN DATOS"
|
|
placeholder.color = Color.MAGENTA
|
|
return
|
|
|
|
label.text = pickup_data.display_name
|
|
sprite.texture = pickup_data.icon
|
|
placeholder.visible = pickup_data.icon == null
|
|
placeholder.color = pickup_data.color
|
|
start_y = position.y
|
|
|
|
if collection_delay_seconds > 0.0:
|
|
_enable_collection_after_delay()
|
|
else:
|
|
_can_collect = true
|
|
|
|
|
|
## Habilita el drop y resuelve un contacto que hubiera ocurrido durante la espera.
|
|
func _enable_collection_after_delay() -> void:
|
|
await get_tree().create_timer(collection_delay_seconds, false).timeout
|
|
if not is_inside_tree():
|
|
return
|
|
_can_collect = true
|
|
|
|
# No se pierde un contacto recibido durante la espera: atravesar el objeto cuenta como haberlo tocado.
|
|
if is_instance_valid(_pending_collector):
|
|
_on_body_entered(_pending_collector)
|
|
_pending_collector = null
|
|
|
|
|
|
## Hace caer/flotar el objeto y reintenta aplicar efectos mientras el jugador lo toca.
|
|
func _physics_process(delta: float) -> void:
|
|
if Engine.is_editor_hint():
|
|
return
|
|
_update_label_proximity()
|
|
time += delta
|
|
if not grounded:
|
|
velocity.y += fall_speed * delta
|
|
position += velocity * delta
|
|
if is_on_floor():
|
|
grounded = true
|
|
velocity = Vector2.ZERO
|
|
start_y = position.y
|
|
else:
|
|
position.y = start_y + sin(time * bob_speed) * bob_amplitude
|
|
|
|
# body_entered solo avisa al comenzar el contacto. Revisar solapamientos
|
|
# permite recoger un objeto cuando pasa a ser util sin exigir salir y volver a entrar.
|
|
if pickup_data != null and not is_queued_for_deletion():
|
|
for body in get_overlapping_bodies():
|
|
_on_body_entered(body)
|
|
if is_queued_for_deletion():
|
|
break
|
|
|
|
|
|
## Sonda de suelo sobre la capa World; los objetos son areas, no cuerpos fisicos.
|
|
func is_on_floor() -> bool:
|
|
var space = get_world_2d().direct_space_state
|
|
var params = PhysicsRayQueryParameters2D.create(global_position, global_position + Vector2(0, 12))
|
|
params.collision_mask = 1
|
|
var result = space.intersect_ray(params)
|
|
return result.size() > 0
|
|
|
|
|
|
## Consume el objeto solo cuando Player confirma que se ha aplicado su efecto.
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if pickup_data == null or is_queued_for_deletion():
|
|
return
|
|
if not body.is_in_group("player") or not body.has_method("collect_pickup"):
|
|
return
|
|
|
|
if not _can_collect:
|
|
_pending_collector = body
|
|
return
|
|
|
|
if body.collect_pickup(pickup_data):
|
|
picked_up.emit(self, pickup_data)
|
|
queue_free()
|
|
elif pickup_data.effect_type in ["damage_boost", "air_damage_boost"]:
|
|
label.text = "%s\n(NO MEJORA TU DANO)" % pickup_data.display_name
|
|
elif pickup_data.effect_type == "heal":
|
|
# No es un fallo de colision: el jugador ya tiene la vida completa.
|
|
label.text = "%s\n(HP LLENO)" % pickup_data.display_name
|
|
elif pickup_data.effect_type == "special_charge":
|
|
label.text = "%s\n(ESPECIAL LLENO)" % pickup_data.display_name
|
|
|
|
|
|
## Actualiza exclusivamente la presentacion; nunca consume objetos en el editor.
|
|
func _update_preview() -> void:
|
|
if not is_inside_tree() or not has_node("Label"):
|
|
return
|
|
var preview_label: Label = $Label
|
|
var preview_sprite: Sprite2D = $Sprite2D
|
|
var preview_placeholder: ColorRect = $Sprite2D/ColorRect
|
|
preview_label.text = pickup_data.display_name if pickup_data != null else "SIN DATOS"
|
|
preview_sprite.texture = pickup_data.icon if pickup_data != null else null
|
|
preview_placeholder.visible = preview_sprite.texture == null
|
|
preview_placeholder.color = pickup_data.color if pickup_data != null else Color.MAGENTA
|
|
update_configuration_warnings()
|
|
|
|
|
|
## Avisa en el arbol del editor cuando falta el recurso de datos.
|
|
func _get_configuration_warnings() -> PackedStringArray:
|
|
if pickup_data == null:
|
|
return PackedStringArray(["Asigna Pickup Data o arrastra una escena de pickups/variants."])
|
|
return PackedStringArray()
|
|
|
|
|
|
## Nombres y avisos de recogida permanecen legibles cerca del personaje sin
|
|
## llenar el horizonte de etiquetas. El editor conserva todas las etiquetas.
|
|
func _update_label_proximity() -> void:
|
|
if not is_instance_valid(_label_viewer):
|
|
_label_viewer = get_tree().get_first_node_in_group(&"player") as Node2D
|
|
label.visible = is_instance_valid(_label_viewer) and \
|
|
global_position.distance_squared_to(_label_viewer.global_position) <= label_visibility_radius * label_visibility_radius
|