SlimeoosOdyssey/godot/spawner.gd

31 lines
951 B
GDScript

extends Node3D
var rng = RandomNumberGenerator.new()
var spawnableItems: Array
# Called when the node enters the scene tree for the first time.
func _ready():
$SpawnTimer.wait_time = 10
$SpawnTimer.one_shot = true
spawnableItems.append(load("res://cultist.tscn"))
spawnableItems.append(load("res://villager_shared.tscn"))
$SpawnTimer.start()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_spawn_timer_timeout():
var item_to_spawn = rng.randi_range(0, spawnableItems.size() - 1)
var spawned_item = spawnableItems[item_to_spawn].instantiate()
get_parent_node_3d().add_child(spawned_item)
spawned_item.set_global_position(global_transform.origin)
spawned_item.set_velocity(Vector3(rng.randf_range(0, 3), rng.randf_range(1, 3), rng.randf_range(0, 3)))
# Start the timer again at a random rate.
$SpawnTimer.wait_time = rng.randi_range(120, 300)
$SpawnTimer.start()