class_name MonsterSpawnpoint extends Node3D # a spawnpoint that can spawn monsters around the center point # with a given distance # monsters are only spawned, if the player is not looking @onready var spawn_timer: Timer = Timer.new() # list that holds all spawnable monsters @export var spawnable_monsters: Array[PackedScene] # the maximum amount of spawned monsters # no monsters will spawn, when this number is reached @export var max_monsters: int = 5 # the maximum distance a monster can spawn around the spawnpoint @export var max_distance_spawn: float = 10.0 # the maximum lifetime a monster can be in the world # a monster is then deleted if the following conditions are met: # the monster has exceeded its maximum lifetime and it is not on screen # TODO: figure out how to check for the exceeded lifetime and delete @export var max_lifetime: float = 60.0 # seconds @export var spawn_cooldown: float = 3.0 # seconds # a spawnpoint should keep track of spawned entities #var active_monsters: Array[Monster] = [] # Called when the node enters the scene tree for the first time. func _ready() -> void: spawn_timer.wait_time = spawn_cooldown spawn_timer.autostart = true spawn_timer.timeout.connect(spawn) add_child(spawn_timer) spawn_timer.start() func spawn() -> void: if $VisibleOnScreenNotifier3D.is_on_screen(): print("Spawnpoint is visible on screen. Not spawning a monster.") return # TODO: Add a check to not spawn enemies X units near the player # get a random position near the spawn point var spawn_point_x = randf_range(-max_distance_spawn, max_distance_spawn) var spawn_point_z = randf_range(-max_distance_spawn, max_distance_spawn) # instantiate a monster .tscn scene var monster_to_spawn = spawnable_monsters[0].instantiate() # calculate the position of the monster and add the offset of the spawnpoint itself var monster_position = Vector3(spawn_point_x, 0.2, spawn_point_z) monster_to_spawn.position = monster_position + position var debug_level_node = get_node("/root/Main/CurrentLevel/DebugLevel") debug_level_node.add_child(monster_to_spawn) print("spawning mob") # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: var time = Time.get_ticks_msec() $Diamond.rotation.y = sin(time / 800.0) * 2 $Diamond.position.y = 1.5 + sin(time / 800.0) * 0.3