Files
mmo/systems/spawn_system.gd
Marek Lenczewski f1d34ebf1d update
2026-04-04 00:00:15 +02:00

45 lines
1.5 KiB
GDScript

extends Node
const ENEMY_SCENE: PackedScene = preload("res://scenes/enemy/enemy.tscn")
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 current <= 0:
return
var data: Dictionary = PortalData.entities.get(entity, {})
if data.is_empty():
return
var ratio: float = current / max_val
var thresholds: Array = data["thresholds"]
var triggered: Array = data["triggered"]
var spawn_count: int = data["spawn_count"]
for i in range(thresholds.size()):
if not triggered[i] and ratio <= thresholds[i]:
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
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"):
PortalData.deregister(entity)