update
This commit is contained in:
44
scripts/components/spawner.gd
Normal file
44
scripts/components/spawner.gd
Normal file
@@ -0,0 +1,44 @@
|
||||
extends Node
|
||||
|
||||
@export var spawn_scene: PackedScene
|
||||
@export var spawn_count := 3
|
||||
@export var thresholds: Array[float] = [0.85, 0.70, 0.55, 0.40, 0.25, 0.10]
|
||||
|
||||
var triggered: Array[bool] = []
|
||||
|
||||
@onready var parent: Node3D = get_parent()
|
||||
@onready var health: Node = get_parent().get_node("Health")
|
||||
|
||||
func _ready() -> void:
|
||||
triggered.resize(thresholds.size())
|
||||
triggered.fill(false)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if not spawn_scene or health.current_health <= 0:
|
||||
return
|
||||
var ratio: float = health.current_health / health.max_health
|
||||
for i in range(thresholds.size()):
|
||||
if not triggered[i] and ratio <= thresholds[i]:
|
||||
triggered[i] = true
|
||||
_spawn()
|
||||
|
||||
func _spawn() -> void:
|
||||
var spawned: Array = []
|
||||
for j in range(spawn_count):
|
||||
var entity: Node = spawn_scene.instantiate()
|
||||
var offset := Vector3(randf_range(-2, 2), 0, randf_range(-2, 2))
|
||||
parent.get_parent().add_child(entity)
|
||||
entity.global_position = parent.global_position + offset
|
||||
if "spawn_position" in entity:
|
||||
entity.spawn_position = parent.global_position
|
||||
if "portal" in entity:
|
||||
entity.portal = parent
|
||||
spawned.append(entity)
|
||||
var player: Node = get_tree().get_first_node_in_group("player")
|
||||
if player:
|
||||
var dist: float = parent.global_position.distance_to(player.global_position)
|
||||
if dist <= 10.0:
|
||||
for entity in spawned:
|
||||
if entity.has_method("_engage"):
|
||||
entity._engage(player)
|
||||
EventBus.portal_spawn.emit(parent, spawned)
|
||||
Reference in New Issue
Block a user