1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2025-02-16 14:13:58 +00:00
swear-jar/spawner/spawner.gd

67 lines
1.7 KiB
GDScript3
Raw Normal View History

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-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-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
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
)
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:
return
2023-10-02 18:46:09 +02:00
if scene == null:
return
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 02:53:12 +02:00
spawned.emit()
buffer -= 1
2023-10-01 01:56:54 +02:00
func _physics_process(_delta):
if buffer > 0:
_do_spawn()