50 lines
1.1 KiB
GDScript
50 lines
1.1 KiB
GDScript
extends Node3D
|
|
|
|
@export var rat : Resource
|
|
@export var spawn_time_max = 5
|
|
@export var max_rats : float = 2
|
|
@export var spawn_rat_pickups = 1
|
|
|
|
var spawn_timer
|
|
var holes = []
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
#get holes
|
|
for node in self.get_children():
|
|
if node is RatHole:
|
|
holes.append(node)
|
|
spawn_rat_at_random_hole()
|
|
reset_spawn_timer()
|
|
|
|
func _process(delta: float) -> void:
|
|
if spawn_timer > 0:
|
|
spawn_timer -= delta
|
|
else:
|
|
if can_spawn_more_rats():
|
|
reset_spawn_timer()
|
|
spawn_rat_at_random_hole()
|
|
|
|
func spawn_rat_at_random_hole():
|
|
if holes.size() >= 2:
|
|
var hole_options = holes.duplicate()
|
|
hole_options.shuffle()
|
|
var spawn_hole = hole_options.pop_front()
|
|
var destination_hole = hole_options.pop_front()
|
|
spawn_hole.spawn_rat(destination_hole,spawn_rat_pickups)
|
|
|
|
func can_spawn_more_rats():
|
|
var rat_count = 0
|
|
#count rats
|
|
for node in get_children():
|
|
if node is Rat:
|
|
rat_count += 1
|
|
#compare
|
|
if rat_count < max_rats:
|
|
return true
|
|
else:
|
|
return false
|
|
|
|
func reset_spawn_timer():
|
|
spawn_timer = randf_range(1,spawn_time_max)
|