Files
mortal-maze-the-game/levels/level_01.gd
T

170 lines
6.3 KiB
GDScript

extends Node2D
## Android test course: 17 stations / 17 different guaranteed items.
## Encounters are configured in the scene, not spawned by code.
## This laboratory intentionally owns fixed drops: each death is observed from
## the actual is_dead state, even when a died signal is missed.
signal level_completed
const TEST_ITEM_COUNT: int = 17
const LEVEL_WIDTH: int = 8500
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; count each only once
var defeated_enemies: int = 0
var death_signals: int = 0
var spawned_drops: int = 0
var _processed_deaths: Dictionary = {} # enemy instance id -> true
var showing_victory: bool = false
var victory_timer: float = 0.0
func _ready() -> void:
# These two markers distinguish an actually loaded script from an old APK.
status_label.text = "%s: script iniciado" % LAB_BUILD
print("%s: Level01 script iniciado" % LAB_BUILD)
finish_message.visible = false
# Player Camera2D sets its own provisional limit (4000) in _ready.
# Override it here, after child _ready, for the long Android route.
var camera: Camera2D = player.get_node("Camera2D") as Camera2D
camera.limit_right = LEVEL_WIDTH
$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():
# The laboratory, not Walker, owns the fixed drop. Only this level sets
# the flag: normal enemies still generate their usual random 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()
func _process(delta: float) -> void:
# Do not depend on the died signal to count KOs or create the object.
# A transition to is_dead can only be processed once per enemy.
_process_station_deaths()
if showing_victory:
victory_timer += delta
if victory_timer >= FINISH_RESTART_SECONDS:
get_tree().reload_current_scene()
return
_update_status()
func _update_status() -> void:
# Each station has one enemy and an assigned pickup. Look ahead to the first
# living enemy so the player knows what to expect without opening a menu.
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
# S is the number of died signals received. KOs come from is_dead,
# not signals. A difference between them pinpoints a signal problem.
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
]
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
# Adding an Area2D during a physics step is unsafe. Set position and
# data before adding it, then defer its entry into the scene tree.
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])
func _on_station_enemy_died() -> void:
# Diagnostic counter only; the actual reward is managed above.
death_signals += 1
func _on_pickup_collected(data: PickupData) -> void:
# The extra pickup at the start checks rendering and collection independently
# of enemy deaths. It is not one of the 17 rewards.
if data.id == CONTROL_ITEM_ID:
return
# A heal at full HP or a charge item with a full meter isn't consumed and
# doesn't count. The object remains on the floor for later collection.
collected_items[data.id] = true
_update_status()
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
func _on_death_entered(body: Node2D) -> void:
if body == player and body.has_method("take_damage"):
body.take_damage(9999, body.global_position)
func _draw() -> void:
# Temporary coloured geometry; collision shapes are the single source of
# truth for position and dimensions, so the visuals cannot desynchronise.
for child in $World.get_children():
if not child is CollisionShape2D:
continue
var collision: CollisionShape2D = child as CollisionShape2D
var rectangle: RectangleShape2D = collision.shape as RectangleShape2D
if rectangle == null:
continue
var color: Color = Color(0.32, 0.57, 0.79)
if collision.name == "Floor":
color = Color(0.20, 0.40, 0.25)
elif collision.name.begins_with("High"):
color = Color(0.68, 0.39, 0.76)
var rect: Rect2 = Rect2(collision.position - rectangle.size / 2.0, rectangle.size)
draw_rect(rect, color, true)
draw_rect(rect, Color(0.93, 0.94, 1.0, 0.6), false, 2.0)