Files
mmo/world/portal_spawner.gd
Marek Lenczewski 47f4fe3d90 update
2026-04-02 16:02:24 +02:00

45 lines
1.3 KiB
GDScript

extends Node
const PORTAL_SCENE: PackedScene = preload("res://portal/portal.tscn")
const GATE_SCENE: PackedScene = preload("res://portal/gate.tscn")
const SPAWN_INTERVAL := 30.0
const MAX_PORTALS := 3
const MIN_DISTANCE := 20.0
const MAX_DISTANCE := 40.0
var portals: Array[Node] = []
var timer := 0.0
func _ready() -> void:
if GameState.portal_position != Vector3.ZERO and not GameState.dungeon_cleared:
call_deferred("_restore_gate")
else:
if GameState.dungeon_cleared:
GameState.clear()
call_deferred("_spawn_portal")
func _restore_gate() -> void:
var gate: Node3D = GATE_SCENE.instantiate()
get_parent().add_child(gate)
gate.global_position = GameState.portal_position
func _process(delta: float) -> void:
timer += delta
if timer >= SPAWN_INTERVAL:
timer = 0.0
_cleanup_dead()
if portals.size() < MAX_PORTALS:
_spawn_portal()
func _spawn_portal() -> void:
var angle: float = randf() * TAU
var distance: float = randf_range(MIN_DISTANCE, MAX_DISTANCE)
var pos := Vector3(cos(angle) * distance, 0, sin(angle) * distance)
var portal: Node3D = PORTAL_SCENE.instantiate()
get_parent().add_child(portal)
portal.global_position = pos
portals.append(portal)
func _cleanup_dead() -> void:
portals = portals.filter(func(p: Node) -> bool: return is_instance_valid(p))