2024-04-16 01:02:19 +00:00
|
|
|
@icon("res://behaviours/spawner.svg")
|
2024-04-19 02:30:35 +00:00
|
|
|
extends Node2D
|
2024-04-14 17:48:13 +00:00
|
|
|
class_name Spawner
|
|
|
|
|
2024-04-19 02:30:35 +00:00
|
|
|
|
|
|
|
## Emitted when a new scene is spawned.
|
2024-04-14 17:48:13 +00:00
|
|
|
signal spawned(entity: Node2D)
|
|
|
|
|
2024-04-30 03:49:36 +00:00
|
|
|
## Emitted when a spawn fails due to the spawner being disabled.
|
2024-04-19 02:30:35 +00:00
|
|
|
signal spawn_blocked
|
|
|
|
|
|
|
|
|
|
|
|
## The scene to spawn.
|
2024-04-14 17:48:13 +00:00
|
|
|
@export var scene: PackedScene
|
2024-04-19 02:30:35 +00:00
|
|
|
|
|
|
|
## Where the scene should be parented to in the tree.
|
2024-04-19 01:47:34 +00:00
|
|
|
@export var target: Node2D
|
2024-04-14 17:48:13 +00:00
|
|
|
|
2024-04-30 03:49:36 +00:00
|
|
|
## Whether calling [method spawn] does instantiate a scene.
|
|
|
|
@export var enabled: bool = true
|
2024-04-19 02:30:35 +00:00
|
|
|
|
2024-04-16 01:02:19 +00:00
|
|
|
|
2024-04-30 03:49:36 +00:00
|
|
|
func enable():
|
|
|
|
enabled = true
|
|
|
|
|
|
|
|
func disable():
|
|
|
|
enabled = false
|
|
|
|
|
2024-04-19 02:30:35 +00:00
|
|
|
## Spawn [field scene] at [field target] and the position of this node.
|
2024-04-19 01:47:34 +00:00
|
|
|
func spawn():
|
2024-04-30 03:49:36 +00:00
|
|
|
if not enabled:
|
|
|
|
spawn_blocked.emit()
|
|
|
|
return
|
2024-04-16 01:02:19 +00:00
|
|
|
if not target:
|
2024-04-19 02:30:35 +00:00
|
|
|
target = MainGame.get_via_group(self).default_spawn_parent
|
2024-04-19 01:48:25 +00:00
|
|
|
if not target:
|
|
|
|
target = self
|
2024-04-28 23:23:31 +00:00
|
|
|
if not scene:
|
|
|
|
Log.w(self, "Not spawning, no scene is set.")
|
|
|
|
return
|
2024-04-14 17:48:13 +00:00
|
|
|
var entity = scene.instantiate()
|
|
|
|
entity.global_position = global_position
|
2024-04-16 01:02:19 +00:00
|
|
|
target.add_child.call_deferred(entity) # Not sure why this is needed.
|