53 lines
2.0 KiB
GDScript
53 lines
2.0 KiB
GDScript
extends Node
|
|
|
|
const ENEMY_SCENE: PackedScene = preload("res://scenes/enemy/enemy.tscn")
|
|
|
|
var portal_data: Dictionary = {}
|
|
|
|
func _ready() -> void:
|
|
EventBus.health_changed.connect(_on_health_changed)
|
|
EventBus.entity_died.connect(_on_entity_died)
|
|
|
|
func _on_health_changed(entity: Node, current: float, max_val: float) -> void:
|
|
if not entity.is_in_group("portals"):
|
|
return
|
|
if entity not in portal_data:
|
|
var base: BaseStats = Stats.get_base(entity)
|
|
var thresholds: Array[float] = base.thresholds if base is PortalStats else [0.85, 0.70, 0.55, 0.40, 0.25, 0.10]
|
|
var triggered: Array[bool] = []
|
|
triggered.resize(thresholds.size())
|
|
triggered.fill(false)
|
|
portal_data[entity] = { "thresholds": thresholds, "triggered": triggered }
|
|
if current <= 0:
|
|
return
|
|
var data: Dictionary = portal_data[entity]
|
|
var ratio: float = current / max_val
|
|
var base: BaseStats = Stats.get_base(entity)
|
|
var spawn_count: int = base.spawn_count if base is PortalStats else 3
|
|
for i in range(data["thresholds"].size()):
|
|
if not data["triggered"][i] and ratio <= data["thresholds"][i]:
|
|
data["triggered"][i] = true
|
|
_spawn_enemies(entity, spawn_count)
|
|
|
|
func _spawn_enemies(portal: Node, count: int) -> void:
|
|
var spawned: Array = []
|
|
for j in range(count):
|
|
var entity: Node = ENEMY_SCENE.instantiate()
|
|
var offset := Vector3(randf_range(-2, 2), 0, randf_range(-2, 2))
|
|
portal.get_parent().add_child(entity)
|
|
entity.global_position = portal.global_position + offset
|
|
entity.spawn_position = portal.global_position
|
|
entity.portal = portal
|
|
spawned.append(entity)
|
|
var player: Node = get_tree().get_first_node_in_group("player")
|
|
if player:
|
|
var dist: float = portal.global_position.distance_to(player.global_position)
|
|
if dist <= 10.0:
|
|
for entity in spawned:
|
|
EventBus.enemy_detected.emit(entity, player)
|
|
EventBus.portal_spawn.emit(portal, spawned)
|
|
|
|
func _on_entity_died(entity: Node) -> void:
|
|
if entity.is_in_group("portals"):
|
|
portal_data.erase(entity)
|