2024-10-23 14:57:06 +02:00
|
|
|
class_name RewardSpawner
|
|
|
|
|
extends Node
|
|
|
|
|
|
2025-01-04 02:25:39 +01:00
|
|
|
@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)
|
2024-10-23 14:57:06 +02:00
|
|
|
var event_bus: EventBus
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
|
event_bus = get_node("/root/Main/MainEventBus")
|
2025-01-04 02:25:39 +01:00
|
|
|
event_bus.subscribe_to(
|
|
|
|
|
get_node("/root/Main/DestroyedEventScope"),
|
|
|
|
|
"DestroyedEvent",
|
|
|
|
|
Callable(self, "on_destroyed")
|
|
|
|
|
)
|
2024-10-23 14:57:06 +02:00
|
|
|
|
|
|
|
|
func on_destroyed(event: DestroyedEvent):
|
2025-01-04 02:25:39 +01:00
|
|
|
if event.object_type in reward_table:
|
|
|
|
|
spawn_reward(event.position, reward_table[event.object_type])
|
2024-10-23 14:57:06 +02:00
|
|
|
|
2025-01-04 02:25:39 +01:00
|
|
|
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)
|