123 lines
4.2 KiB
GDScript
123 lines
4.2 KiB
GDScript
extends Node2D
|
|
|
|
## Android test course: 17 stations / 17 different guaranteed items.
|
|
## Encounters are configured in the scene, not spawned by code, so designers can
|
|
## freely move a station or switch its drop in the Godot inspector.
|
|
|
|
signal level_completed
|
|
|
|
const TEST_ITEM_COUNT: int = 17
|
|
const LEVEL_WIDTH: int = 8500
|
|
const FINISH_RESTART_SECONDS: float = 8.0
|
|
|
|
@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 showing_victory: bool = false
|
|
var victory_timer: float = 0.0
|
|
|
|
|
|
func _ready() -> void:
|
|
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():
|
|
# Catch missing scene overrides as soon as the level starts rather than
|
|
# silently falling back to the normal random drop table.
|
|
if enemy.get("guaranteed_drop") == null:
|
|
push_error("%s has no guaranteed_drop assigned." % enemy.name)
|
|
enemy.died.connect(_on_station_enemy_died)
|
|
_update_status()
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
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
|
|
|
|
# Two independent counters reveal whether an object failed to spawn or
|
|
# simply got collected. Useful for diagnosing drops on an Android APK.
|
|
var loose_drops: int = get_tree().get_nodes_in_group("pickup").size()
|
|
status_label.text = "LAB: %02d/%02d objetos | %02d KOs\nDROPS EN SUELO: %02d\nSIGUIENTE: %s" % [
|
|
collected_items.size(), TEST_ITEM_COUNT, defeated_enemies,
|
|
loose_drops, next_item
|
|
]
|
|
|
|
|
|
func _on_station_enemy_died() -> void:
|
|
defeated_enemies += 1
|
|
_update_status()
|
|
|
|
|
|
func _on_pickup_collected(data: PickupData) -> void:
|
|
# 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)
|