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

42 lines
896 B
GDScript3
Raw Normal View History

2023-09-30 18:16:14 +00:00
extends Node2D
class_name Spawner
2023-09-30 23:33:54 +00:00
2023-09-30 18:16:14 +00:00
@export var scene: PackedScene
2023-09-30 23:33:54 +00:00
2023-09-30 23:56:54 +00:00
var buffer: int = 0
@export var buffer_cap: int
2023-09-30 23:58:46 +00:00
@onready var area: Area2D = $Area
@onready var shape: CollisionShape2D = $Area/Shape
@onready var rng: RandomNumberGenerator = RandomNumberGenerator.new()
2023-09-30 23:58:46 +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:
print("Hit buffer!")
2023-09-30 23:59:28 +00:00
buffer = buffer_cap
2023-09-30 23:56:54 +00:00
func _select_spawn_position() -> Vector2:
var variance = shape.shape.size.x / 2
var x = rng.randf_range(-variance, +variance)
return Vector2(x, 0)
func _do_spawn():
var overlapping_bodies = area.get_overlapping_bodies()
for overlapping_body in overlapping_bodies:
if overlapping_body.collision_layer && 0b100:
return
var scene_instant = scene.instantiate()
scene_instant.position = _select_spawn_location()
add_child(scene_instant)
buffer -= 1
2023-09-30 23:56:54 +00:00
func _physics_process(_delta):
if buffer > 0:
_do_spawn()