155 lines
5.8 KiB
GDScript
155 lines
5.8 KiB
GDScript
@tool
|
|
extends LevelRoot
|
|
|
|
## Laboratorio Android: 17 estaciones y 17 objetos garantizados distintos.
|
|
## Los encuentros se colocan en la escena. Este nivel observa is_dead
|
|
## y gestiona sus propias recompensas aunque no reciba alguna senal died.
|
|
|
|
signal level_completed
|
|
|
|
const TEST_ITEM_COUNT: int = 17
|
|
const FINISH_RESTART_SECONDS: float = 8.0
|
|
const LAB_BUILD: String = "LAB DROPS v4"
|
|
const CONTROL_ITEM_ID: StringName = &"lab_control"
|
|
const PICKUP_SCENE: PackedScene = preload("res://pickups/pickup.tscn")
|
|
|
|
@onready var player: Node2D = $Player
|
|
@onready var test_enemies: Node2D = $TestEnemies
|
|
@onready var status_label: Label = $TestHUD/Overlay/Status
|
|
@onready var finish_message: Label = $TestHUD/Overlay/FinishMessage
|
|
|
|
var collected_items: Dictionary = {} # PickupData.id -> true; cuenta cada objeto una sola vez.
|
|
var defeated_enemies: int = 0
|
|
var death_signals: int = 0
|
|
var spawned_drops: int = 0
|
|
var _processed_deaths: Dictionary = {} # Identificador de instancia enemiga -> muerte ya procesada.
|
|
var showing_victory: bool = false
|
|
var victory_timer: float = 0.0
|
|
|
|
|
|
## Configura las estaciones sin alterar las recompensas del resto de niveles.
|
|
func _ready() -> void:
|
|
super._ready()
|
|
if Engine.is_editor_hint():
|
|
return
|
|
# Estos marcadores distinguen la version cargada de una instalacion antigua del APK.
|
|
status_label.text = "%s: script iniciado" % LAB_BUILD
|
|
print("%s: Level01 script iniciado" % LAB_BUILD)
|
|
finish_message.visible = false
|
|
|
|
$FinishZone.body_entered.connect(_on_finish_entered)
|
|
$DeathZone.body_entered.connect(_on_death_entered)
|
|
player.connect(&"pickup_collected", _on_pickup_collected)
|
|
for enemy in test_enemies.get_children():
|
|
# Mantiene aisladas las estaciones aunque las variantes normales persigan a mayor distancia.
|
|
enemy.set("chase_radius", 135.0)
|
|
# Solo el laboratorio asume la recompensa fija; los enemigos normales conservan sus propios drops.
|
|
enemy.set("drops_handled_by_level", true)
|
|
if enemy.get("guaranteed_drop") == null:
|
|
push_error("%s has no guaranteed_drop assigned." % enemy.name)
|
|
if enemy.has_signal(&"died"):
|
|
enemy.connect(&"died", _on_station_enemy_died)
|
|
else:
|
|
push_error("%s no expone la senal died" % enemy.name)
|
|
_update_status()
|
|
|
|
|
|
## Observa muertes y actualiza el diagnostico; tras completar, cuenta hasta reiniciar.
|
|
func _process(delta: float) -> void:
|
|
super._process(delta)
|
|
if Engine.is_editor_hint():
|
|
return
|
|
# El recuento y las recompensas no dependen de died. Cada muerte se procesa una sola vez.
|
|
_process_station_deaths()
|
|
if showing_victory:
|
|
victory_timer += delta
|
|
if victory_timer >= FINISH_RESTART_SECONDS:
|
|
get_tree().reload_current_scene()
|
|
return
|
|
|
|
_update_status()
|
|
|
|
|
|
## Muestra progreso, estado de drops y siguiente recompensa.
|
|
func _update_status() -> void:
|
|
# Busca la primera estacion viva por delante para anticipar su recompensa en el HUD.
|
|
var next_item: String = "FIN DEL RECORRIDO"
|
|
for enemy in test_enemies.get_children():
|
|
if bool(enemy.get("is_dead")):
|
|
continue
|
|
var enemy_body: Node2D = enemy as Node2D
|
|
if enemy_body.position.x < player.position.x - 165.0:
|
|
continue
|
|
var data: PickupData = enemy.get("guaranteed_drop") as PickupData
|
|
if data != null:
|
|
next_item = data.display_name
|
|
break
|
|
|
|
# Compara senales con muertes reales: una diferencia permite diagnosticar senales perdidas.
|
|
var loose_drops: int = get_tree().get_nodes_in_group("pickup").size()
|
|
status_label.text = "%s | %02d/%02d objetos\nKOs: %02d | Senales: %02d | Creados: %02d | Suelo: %02d\nSIGUIENTE: %s" % [
|
|
LAB_BUILD, collected_items.size(), TEST_ITEM_COUNT,
|
|
defeated_enemies, death_signals, spawned_drops, loose_drops, next_item
|
|
]
|
|
|
|
|
|
## Convierte cada nueva muerte en un unico objeto garantizado.
|
|
func _process_station_deaths() -> void:
|
|
for enemy in test_enemies.get_children():
|
|
if not bool(enemy.get("is_dead")):
|
|
continue
|
|
var instance_id: int = enemy.get_instance_id()
|
|
if _processed_deaths.has(instance_id):
|
|
continue
|
|
_processed_deaths[instance_id] = true
|
|
defeated_enemies += 1
|
|
|
|
var data: PickupData = enemy.get("guaranteed_drop") as PickupData
|
|
if data == null:
|
|
push_error("%s: %s ha muerto sin objeto asignado" % [LAB_BUILD, enemy.name])
|
|
continue
|
|
|
|
# Prepara posicion y datos antes de diferir la entrada al arbol.
|
|
# No se debe anadir un Area2D durante la resolucion de contactos fisicos.
|
|
var pickup: Pickup = PICKUP_SCENE.instantiate() as Pickup
|
|
pickup.pickup_data = data
|
|
pickup.collection_delay_seconds = Pickup.DROP_COLLECTION_DELAY_SECONDS
|
|
pickup.position = to_local(enemy.global_position + Vector2(34.0, -12.0))
|
|
call_deferred("add_child", pickup)
|
|
spawned_drops += 1
|
|
print("%s: KO %s -> %s" % [LAB_BUILD, enemy.name, data.display_name])
|
|
|
|
|
|
## Cuenta senales para compararlas con las muertes observadas.
|
|
func _on_station_enemy_died() -> void:
|
|
# Contador diagnostico; la recompensa se procesa por separado al observar is_dead.
|
|
death_signals += 1
|
|
|
|
|
|
## Cuenta identificadores distintos de recompensas realmente aplicadas.
|
|
func _on_pickup_collected(data: PickupData) -> void:
|
|
# El objeto inicial verifica imagen y recogida sin matar enemigos; no cuenta entre las 17 recompensas.
|
|
if data.id == CONTROL_ITEM_ID:
|
|
return
|
|
# Una curacion con vida llena o carga ya completa no consume el objeto ni aumenta el recuento.
|
|
collected_items[data.id] = true
|
|
_update_status()
|
|
|
|
|
|
## Completa el laboratorio al entrar el jugador en la meta.
|
|
func _on_finish_entered(body: Node2D) -> void:
|
|
if body != player or showing_victory:
|
|
return
|
|
showing_victory = true
|
|
level_completed.emit()
|
|
finish_message.text = "FIN DEL LAB!\nObjetos recogidos: %d / %d\nReinicio en 8 s" % [
|
|
collected_items.size(), TEST_ITEM_COUNT
|
|
]
|
|
finish_message.visible = true
|
|
|
|
|
|
## Aplica el comportamiento original del area de caida del laboratorio.
|
|
func _on_death_entered(body: Node2D) -> void:
|
|
if body == player and body.has_method("take_damage"):
|
|
body.take_damage(9999, body.global_position)
|