2024-10-08 20:51:11 -06:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
|
|
|
|
@export var debug = false
|
|
|
|
@export var spawnTimerValue = 5.0
|
|
|
|
|
|
|
|
var deltaCount := 0
|
|
|
|
var enemyMoveSpeed := 5.0
|
|
|
|
var questsComplete := false
|
|
|
|
|
2024-10-09 20:55:46 -06:00
|
|
|
var middleX
|
|
|
|
var playerPos
|
|
|
|
|
2024-10-08 20:51:11 -06:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2024-10-09 20:55:46 -06:00
|
|
|
middleX = get_viewport_rect().size.x/2
|
|
|
|
playerPos = Vector2(middleX, get_viewport_rect().size.y*(0.75))
|
2024-10-08 20:51:11 -06:00
|
|
|
if not debug:
|
|
|
|
$MarginContainer/Menus/MenuButtons/DebugMenu.hide()
|
2024-10-09 20:55:46 -06:00
|
|
|
$SpawnTimer.start(spawnTimerValue)
|
2024-10-08 20:51:11 -06:00
|
|
|
|
|
|
|
func _create_enemy():
|
|
|
|
var enemy = load("res://scenes/npc.tscn").instantiate()
|
|
|
|
enemy.scale = Vector2.ONE*0.5
|
|
|
|
enemy.position = Vector2(randi_range(0, get_viewport_rect().size.x), -50)
|
|
|
|
#enemy.get_node("%NPCNameLabel").text = "Test"
|
|
|
|
|
|
|
|
return enemy
|
2024-10-09 20:55:46 -06:00
|
|
|
|
|
|
|
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 _get_distance_to_player(enemy: Node) -> float:
|
|
|
|
return enemy.position.distance_squared_to(playerPos)
|
|
|
|
|
|
|
|
|
|
|
|
func _get_target() -> Node:
|
|
|
|
# arrays of enemies by difficulty
|
|
|
|
var enemies := _get_enemies()
|
|
|
|
var numEnemies := _get_num_enemies()
|
|
|
|
#var highestDifficulty
|
|
|
|
var enemyDists
|
|
|
|
var closestEnemy
|
|
|
|
# get array of difficulties using map
|
|
|
|
if not enemies.is_empty():
|
|
|
|
# get index of largest non-zero val in numEnemies array, ignoring minions
|
|
|
|
#highestDifficulty = numEnemies.find(0, 1) - 1
|
|
|
|
enemyDists = enemies.map(_get_distance_to_player)
|
|
|
|
closestEnemy = enemies[enemyDists.find(enemyDists.min())]
|
|
|
|
#var closestDist = INF
|
|
|
|
#for enemy in enemies:
|
|
|
|
#var enemyPos = enemy.position
|
|
|
|
#var enemyDist = playerPos.distance_squared_to(enemyPos)
|
|
|
|
#if enemyDist < closestDist:
|
|
|
|
#closestEnemy = enemy
|
|
|
|
print(enemies)
|
|
|
|
#print(highestDifficulty)
|
|
|
|
print(enemyDists)
|
|
|
|
print(closestEnemy)
|
|
|
|
|
|
|
|
return closestEnemy
|
2024-10-08 20:51:11 -06:00
|
|
|
|
|
|
|
|
2024-10-09 20:55:46 -06:00
|
|
|
func _spawn_enemies(maxEnemies: int) -> void:
|
2024-10-08 20:51:11 -06:00
|
|
|
#await get_tree().create_timer(time).timeout
|
2024-10-09 20:55:46 -06:00
|
|
|
var numEnemies = randi_range(1, maxEnemies)
|
2024-10-08 20:51:11 -06:00
|
|
|
for i in range(numEnemies):
|
|
|
|
var enemy = _create_enemy()
|
|
|
|
%SpawnArea.add_child(enemy)
|
|
|
|
var randEnemyMoveSpeed := randf_range(0.5, 2.0)*enemyMoveSpeed
|
2024-10-09 20:55:46 -06:00
|
|
|
create_tween().tween_property(enemy, "position", playerPos, randEnemyMoveSpeed)
|
2024-10-08 20:51:11 -06:00
|
|
|
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2024-10-09 20:55:46 -06:00
|
|
|
#var closestEnemy = _get_closest_enemy()
|
2024-10-08 20:51:11 -06:00
|
|
|
deltaCount += 1
|
2024-10-09 20:55:46 -06:00
|
|
|
#if numEnemies != 0:
|
|
|
|
#print(playerPos.distance_squared_to(closestEnemy.position))
|
2024-10-08 20:51:11 -06:00
|
|
|
# change movement test, not sure it's working right...
|
|
|
|
#create_tween().tween_property(enemies[randi_range(0, numEnemies-1)], "position", Vector2(100, 100), 1.0)
|
2024-10-09 20:55:46 -06:00
|
|
|
#if deltaCount % 200 == 0:
|
|
|
|
#%SpawnArea.remove_child(closestEnemy)
|
|
|
|
if not _get_enemies().is_empty() and deltaCount % 100 == 0:
|
|
|
|
var targetEnemy = _get_target()
|
|
|
|
$SpawnArea.remove_child(targetEnemy)
|
|
|
|
# change to signal
|
2024-10-08 20:51:11 -06:00
|
|
|
if questsComplete:
|
2024-10-09 20:55:46 -06:00
|
|
|
$SpawnTimer.stop()
|
|
|
|
if _get_enemies().is_empty():
|
|
|
|
print("Area Complete!")
|
2024-10-08 20:51:11 -06:00
|
|
|
|
|
|
|
|
|
|
|
func _on_button_test_pressed() -> void:
|
|
|
|
# remove all existing enemies from scene and array
|
|
|
|
#for enemy in enemies:
|
|
|
|
#remove_child(enemy)
|
|
|
|
#enemies = []
|
|
|
|
|
|
|
|
questsComplete = true
|
|
|
|
|
|
|
|
|
|
|
|
func _on_button_exit_pressed() -> void:
|
|
|
|
get_tree().quit()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_spawn_timer_timeout() -> void:
|
2024-10-09 20:55:46 -06:00
|
|
|
_spawn_enemies(5)
|