42 lines
807 B
GDScript
42 lines
807 B
GDScript
class_name Spawner extends DeferredSpawnGroup
|
|
|
|
var limit: int = 32
|
|
var timeout: float = -1.0
|
|
|
|
var _spawned: Array[Node] = []
|
|
var _timer: float = -1.0
|
|
|
|
func spawn() -> void:
|
|
if _spawned.size() > limit:
|
|
despawn()
|
|
var what := root.duplicate()
|
|
_spawned.push_back(what)
|
|
add_child(what)
|
|
spawned.emit(what)
|
|
|
|
func despawn() -> void:
|
|
if not _spawned.is_empty():
|
|
var what = _spawned[0]
|
|
remove_child(what)
|
|
what.queue_free()
|
|
_spawned.remove_at(0)
|
|
despawned.emit(what)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if timeout > 0.0:
|
|
if _timer <= 0.0:
|
|
spawn()
|
|
_timer = timeout
|
|
_timer -= delta
|
|
|
|
func is_spawned() -> bool:
|
|
return not _spawned.is_empty()
|
|
|
|
func count() -> int:
|
|
return _spawned.size()
|
|
|
|
func dissolve() -> void:
|
|
for what in _spawned:
|
|
what.reparent(get_parent())
|
|
queue_free()
|