starcheese/reward_spawner.gd

49 lines
1.6 KiB
GDScript

class_name RewardSpawner
extends Node
@onready var debug_console = get_node("/root/Main/DebugConsole")
@export var reward_table: Dictionary = {
"asteroid": preload("res://src/collectibles/cheese.tscn"),
"debug_asteroid": preload("res://asteroid.tscn")
}
signal collectible_spawned(collectible: Node)
var event_bus: EventBus
func _ready():
event_bus = get_node("/root/Main/MainEventBus")
event_bus.subscribe_to(
get_node("/root/Main/DestroyedEventScope"),
"DestroyedEvent",
Callable(self, "on_destroyed")
)
func on_destroyed(event: DestroyedEvent):
if event.object_type in reward_table:
spawn_reward(event.position, reward_table[event.object_type])
func spawn_reward(position: Vector2, reward_scene: PackedScene):
var reward_instance = reward_scene.instantiate()
reward_instance.global_position = position + Vector2(
randf() * 50 - 25,
randf() * 50 - 25
)
if reward_scene == reward_table.get("debug_asteroid"):
get_parent().add_child(reward_instance)
print("Debug asteroid detected.")
if debug_console:
debug_console.log_message("1x Debug asteroid was spawned...", Color(1, 0, 0))
return
reward_instance.add_to_group("collectibles")
get_parent().add_child(reward_instance)
emit_signal("collectible_spawned", reward_instance)
if debug_console:
debug_console.log_message("1x Debug cheese was spawned...", Color(1, 0, 0))
func spawn_reward_directly(object_type: String, position: Vector2):
if object_type in reward_table:
spawn_reward(position, reward_table[object_type])
else:
print("Error: Object type not in reward table:", object_type)