Update spawner to be scene-based.

This commit is contained in:
Henry Faber 2026-06-30 20:16:26 +01:00
parent 8fe05e0c70
commit da9f947788
9 changed files with 59 additions and 46 deletions

View file

@ -3,6 +3,9 @@ class_name PowerUpSpawner extends Node2D
@export var spawn_interval: float = 5.0
@export var test_mode: bool = true
# PackedScene for the pickup template — configure in the scene editor.
@export var pickup_scene: PackedScene
@onready var screensize: Vector2 = get_viewport().content_scale_size
@onready var spawn_timer: Timer = $SpawnTimer
@ -31,7 +34,8 @@ func _spawn_test_pickups() -> void:
for weapon: WeaponShot in all_weapon_shots:
if weapon == null or weapon.shot_name.is_empty():
continue
var pickup := _create_pickup(weapon)
var pickup := _spawn_pickup(weapon)
if not is_instance_valid(pickup):
continue # skipped — no valid icon
@ -61,45 +65,27 @@ func pickup_collected() -> void:
_pickups_spawned = 0
func _create_pickup(weapon: WeaponShot) -> Area2D:
var pickup := Area2D.new()
pickup.name = "PowerUpPickup"
func _spawn_pickup(weapon: WeaponShot) -> Area2D:
# Validate that the weapon has a pickup icon configured.
if weapon.pickup_icon == null:
printerr("[PowerUpSpawner] No pickup icon set for weapon: ", weapon.shot_name)
var dead := Area2D.new() # return a dead node — caller skips it
dead.queue_free()
return dead
# Icon texture — load from graphics/ matching the weapon's shot_name.
var icon_path := "res://graphics/" + weapon.shot_name.replace(" ", "") + ".png"
var icon_tex := load(icon_path) as Texture2D
if icon_tex == null:
printerr("[PowerUpSpawner] No texture found for weapon: ", weapon.shot_name)
pickup.queue_free() # return a dead node — caller skips it
return pickup
# Instantiate the scene template and attach weapon metadata.
var pickup := pickup_scene.instantiate() as Area2D
if pickup == null:
printerr("[PowerUpSpawner] Failed to instantiate pickup scene.")
return null
pickup.set_meta("weapon_shot", weapon)
# Add the icon node at runtime (scene has no TextureRect to avoid null-dependency issues).
var icon := TextureRect.new()
icon.texture = icon_tex
icon.texture = weapon.pickup_icon
icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
icon.size = Vector2(16, 16)
pickup.add_child(icon)
# Collision shape — small rectangle matching the icon.
var shape := RectangleShape2D.new()
shape.size = Vector2(14, 14)
var collision := CollisionShape2D.new()
collision.shape = shape
pickup.add_child(collision)
# Store the weapon reference so we know what to give the player.
pickup.set_meta("weapon_shot", weapon)
# Configure collision so HelpBoxArea (Layer 2, Mask 1) detects it.
pickup.collision_layer = 1
pickup.collision_mask = 0
return pickup
# --- Timer control ---
# Direct stop/start — no guard conditions. The timer's paused property
# can cause deadlock if the guard checks "not paused" after stop().