2023-10-02 00:57:14 +02:00
|
|
|
extends Area2D
|
2023-09-30 20:16:14 +02:00
|
|
|
class_name Spawner
|
2023-10-01 01:33:54 +02:00
|
|
|
|
2023-10-02 00:57:14 +02:00
|
|
|
## A node which spawns things!
|
2023-10-01 01:33:54 +02:00
|
|
|
|
2023-10-02 00:57:14 +02:00
|
|
|
## The scene to spawn.
|
2023-09-30 20:16:14 +02:00
|
|
|
@export var scene: PackedScene
|
2023-10-02 00:57:14 +02:00
|
|
|
|
|
|
|
## The node to add new scenes to.
|
2023-10-01 23:56:25 +02:00
|
|
|
@export var target: Node
|
2023-10-01 01:33:54 +02:00
|
|
|
|
2023-10-02 00:57:14 +02:00
|
|
|
## Count of how many items should be spawned when possible.
|
2023-10-01 01:56:54 +02:00
|
|
|
var buffer: int = 0
|
2023-10-02 00:57:14 +02:00
|
|
|
|
|
|
|
## Maximum amount of items whose spawn can be buffered.
|
2023-10-01 01:56:54 +02:00
|
|
|
@export var buffer_cap: int
|
|
|
|
|
2023-10-02 00:57:14 +02:00
|
|
|
## Rect in which scenes can be spawned randomly in.
|
|
|
|
@export var spawn_rect: Rect2
|
|
|
|
|
|
|
|
## Minimum rotation degrees that an object can spawn with.
|
|
|
|
@export_range(0, 360) var spawn_rotation_degrees_min: float
|
2023-10-01 02:18:08 +02:00
|
|
|
|
2023-10-02 00:57:14 +02:00
|
|
|
## Maximum rotation degrees that an object can spawn with.
|
|
|
|
@export_range(0, 360) var spawn_rotation_degrees_max: float
|
|
|
|
|
|
|
|
## Maximum amount of bodies overlapping the spawner's area before the spawner stops spawning.
|
|
|
|
@export_range(0, 16, 1, "or_greater") var overlapping_body_count_limit: int
|
2023-10-01 01:58:46 +02:00
|
|
|
|
2023-10-02 22:09:19 +02:00
|
|
|
## Sound to play when spawning an item.
|
|
|
|
##
|
|
|
|
## Doesn't spawn anything if not set.
|
|
|
|
@export var sound_complete: AudioStreamPlayer
|
|
|
|
|
2023-10-02 22:29:40 +02:00
|
|
|
@export_range(0.01, 8.0, 0.01, "or_greater") var sound_complete_pitch_min: float = 1.00
|
|
|
|
@export_range(0.01, 8.0, 0.01, "or_greater") var sound_complete_pitch_max: float = 1.00
|
2023-10-02 22:09:19 +02:00
|
|
|
|
2023-10-01 02:38:27 +02:00
|
|
|
|
2023-10-01 02:57:06 +02:00
|
|
|
signal spawned(what: Node2D)
|
2023-10-01 02:41:39 +02:00
|
|
|
|
2023-09-30 20:16:14 +02:00
|
|
|
|
|
|
|
func spawn():
|
2023-10-01 01:59:28 +02:00
|
|
|
buffer += 1
|
|
|
|
if buffer > buffer_cap:
|
|
|
|
buffer = buffer_cap
|
2023-10-01 01:56:54 +02:00
|
|
|
|
2023-10-01 02:18:08 +02:00
|
|
|
func _select_spawn_position() -> Vector2:
|
2023-10-02 00:57:14 +02:00
|
|
|
return Vector2(
|
|
|
|
Randomizer.rng.randf_range(spawn_rect.position.x, spawn_rect.end.x),
|
|
|
|
Randomizer.rng.randf_range(spawn_rect.position.y, spawn_rect.end.y),
|
|
|
|
)
|
2023-10-01 02:27:49 +02:00
|
|
|
|
|
|
|
func _select_spawn_rotation() -> float:
|
2023-10-02 00:57:14 +02:00
|
|
|
return Randomizer.rng.randf_range(
|
|
|
|
spawn_rotation_degrees_min,
|
|
|
|
spawn_rotation_degrees_max
|
|
|
|
)
|
2023-10-01 02:18:08 +02:00
|
|
|
|
|
|
|
func _do_spawn():
|
2023-10-02 18:36:55 +02:00
|
|
|
var overlapping_bodies = get_overlapping_bodies()
|
|
|
|
if len(overlapping_bodies) > overlapping_body_count_limit:
|
2023-10-01 02:38:27 +02:00
|
|
|
return
|
2023-10-02 18:46:09 +02:00
|
|
|
if scene == null:
|
|
|
|
return
|
2023-10-02 21:32:08 +02:00
|
|
|
# if scene._bundled["names"][0]=="Coal":
|
|
|
|
# var scene = PackedScene.new()
|
|
|
|
# scene.pack(get_node(""))
|
|
|
|
# print("ehi")
|
2023-10-02 00:57:14 +02:00
|
|
|
var instantiated = scene.instantiate()
|
2023-10-02 02:53:12 +02:00
|
|
|
instantiated.global_position = global_position + _select_spawn_position()
|
2023-10-02 00:57:14 +02:00
|
|
|
instantiated.rotation_degrees = _select_spawn_rotation()
|
|
|
|
target.add_child(instantiated)
|
2023-10-02 22:09:19 +02:00
|
|
|
if sound_complete:
|
|
|
|
sound_complete.pitch_scale = Randomizer.rng.randf_range(sound_complete_pitch_min, sound_complete_pitch_max)
|
|
|
|
sound_complete.play()
|
2023-10-02 23:24:01 +02:00
|
|
|
spawned.emit(instantiated)
|
2023-10-01 02:18:08 +02:00
|
|
|
buffer -= 1
|
|
|
|
|
2023-10-01 01:56:54 +02:00
|
|
|
func _physics_process(_delta):
|
|
|
|
if buffer > 0:
|
2023-10-01 02:18:08 +02:00
|
|
|
_do_spawn()
|