semi-idle-arpg/scripts/WorldArea.gd

84 lines
2.4 KiB
GDScript3
Raw Normal View History

# defines the parameters for quests and spawning enemies within an area
2024-10-15 08:37:50 -06:00
class_name WorldArea
extends PanelContainer
enum GoalType {KILL, FIND}
2024-10-20 16:44:08 -06:00
@export var area_type := Globals.World.AreaType.WILDS
@export var spawnTimerValue = 10.0
@export var spawn_enemy_count := 1
var middleX: float
var playerPos: Vector2
func _ready() -> void:
middleX = get_viewport_rect().size.x / 2
playerPos = Vector2(middleX, get_viewport_rect().size.y * (0.75))
SignalBus.area_continue_pressed.connect(_on_area_continue_pressed)
_generate_quest()
$SpawnTimer.start(spawnTimerValue)
_spawn_enemies(spawn_enemy_count)
# _spawn_enemies(5)
func _process(_delta: float) -> void:
playerPos = Vector2(middleX, get_viewport_rect().size.y * (0.75))
# instantiate Quest
func _generate_quest() -> void:
var quest = load("res://scenes/quest.tscn").instantiate()
# quest.goalType = GoalType.FIND
SignalBus.quest_generated.emit(quest)
# spawn appropriate enemies
func _create_enemy():
var enemy = load("res://scenes/enemy.tscn").instantiate()
enemy.scale = Vector2.ONE * 0.5
enemy.position = Vector2(randf_range(0, get_viewport_rect().size.x), -50.0)
enemy.speed *= randf_range(0.67, 1.5)
enemy.target = playerPos
return enemy
func _get_enemies() -> Array:
var enemies := get_tree().get_nodes_in_group("enemies")
return enemies
func _get_num_enemies() -> int:
var enemies := get_tree().get_nodes_in_group("enemies")
return len(enemies)
func _spawn_enemies(numEnemies: int) -> void:
#await get_tree().create_timer(time).timeout
# var numEnemies = randi_range(1, maxEnemies)
for i in range(numEnemies):
var enemy = _create_enemy()
add_child(enemy)
# var randEnemyMoveSpeed := randf_range(0.5, 2.0)*enemyMoveSpeed
# create_tween().tween_property(enemy, "position", playerPos, randEnemyMoveSpeed)
func _on_spawn_timer_timeout() -> void:
_spawn_enemies(spawn_enemy_count)
func _on_area_continue_pressed() -> void:
$SpawnTimer.stop()
2024-10-21 13:09:51 -06:00
# Globals.debug_print("spawn timer stopped")
while get_child_count() > 1:
await get_tree().create_timer(1.0).timeout
2024-10-21 13:09:51 -06:00
# Globals.debug_print("waiting for children to go to 1 or less")
# Globals.debug_print(get_child_count())
self.queue_free()
2024-10-21 13:09:51 -06:00
# Globals.debug_print("area queued tor free")