1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2024-11-24 16:44:20 +00:00
swear-jar/spawner/spawner.gd

64 lines
1.6 KiB
GDScript3
Raw Normal View History

2023-10-01 22:57:14 +00:00
extends Area2D
2023-09-30 18:16:14 +00:00
class_name Spawner
2023-09-30 23:33:54 +00:00
2023-10-01 22:57:14 +00:00
## A node which spawns things!
2023-09-30 23:33:54 +00:00
2023-10-01 22:57:14 +00:00
## The scene to spawn.
2023-09-30 18:16:14 +00:00
@export var scene: PackedScene
2023-10-01 22:57:14 +00:00
## The node to add new scenes to.
2023-10-01 21:56:25 +00:00
@export var target: Node
2023-09-30 23:33:54 +00:00
2023-10-01 22:57:14 +00:00
## Count of how many items should be spawned when possible.
2023-09-30 23:56:54 +00:00
var buffer: int = 0
2023-10-01 22:57:14 +00:00
## Maximum amount of items whose spawn can be buffered.
2023-09-30 23:56:54 +00:00
@export var buffer_cap: int
2023-10-01 22:57:14 +00: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 22:57:14 +00: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-09-30 23:58:46 +00:00
2023-10-01 00:57:06 +00:00
signal spawned(what: Node2D)
2023-10-01 00:41:39 +00:00
2023-09-30 18:16:14 +00:00
func spawn():
2023-09-30 23:59:28 +00:00
buffer += 1
if buffer > buffer_cap:
buffer = buffer_cap
2023-09-30 23:56:54 +00:00
func _select_spawn_position() -> Vector2:
2023-10-01 22:57:14 +00: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 00:27:49 +00:00
func _select_spawn_rotation() -> float:
2023-10-01 22:57:14 +00:00
return Randomizer.rng.randf_range(
spawn_rotation_degrees_min,
spawn_rotation_degrees_max
)
func _do_spawn():
2023-10-01 22:57:14 +00:00
if len(get_overlapping_bodies()) > overlapping_body_count_limit:
return
2023-10-01 22:57:14 +00:00
var instantiated = scene.instantiate()
2023-10-02 00:53:12 +00:00
instantiated.global_position = global_position + _select_spawn_position()
2023-10-01 22:57:14 +00:00
instantiated.rotation_degrees = _select_spawn_rotation()
target.add_child(instantiated)
2023-10-02 00:53:12 +00:00
spawned.emit()
buffer -= 1
2023-09-30 23:56:54 +00:00
func _physics_process(_delta):
if buffer > 0:
_do_spawn()