87 lines
3.0 KiB
GDScript
87 lines
3.0 KiB
GDScript
extends Node2D
|
|
|
|
signal game_over
|
|
signal level_up()
|
|
signal boss
|
|
signal update_ui(ui_data: UIDataResource)
|
|
|
|
const MOB = preload("res://scenes/mob.tscn")
|
|
const TREE = preload("res://scenes/tree.tscn")
|
|
const BOSS = preload("res://scenes/boss.tscn")
|
|
|
|
@onready var path_follow_2d: PathFollow2D = %PathFollow2D
|
|
@onready var game_over_layer: CanvasLayer = %GameOver
|
|
@onready var game_over_sound: AudioStreamPlayer2D = %GameOverSound
|
|
@onready var background_color: ColorRect = %BackgroundColor
|
|
@onready var spawn_mob_timer: Timer = %SpawnMobTimer
|
|
|
|
@export var map_size := Vector2(8000, 8000)
|
|
@export var tree_density := 100000
|
|
|
|
# Import the GameData and UIData classes
|
|
const GameDataResource = preload("res://scripts/game_data.gd")
|
|
const UIDataResource = preload("res://scripts/ui_data.gd")
|
|
const MobDataResource = preload("res://scripts/mob_data.gd")
|
|
|
|
var game_data = GameDataResource.new(0, 3, 1, 3, 0)
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
background_color.size = map_size
|
|
background_color.position = Vector2(-map_size.x / 2, -map_size.y / 2)
|
|
randomly_spawn_trees(int(map_size.x * map_size.y / tree_density))
|
|
update_ui_data()
|
|
|
|
func spawn_mob() -> void:
|
|
var new_mob = MOB.instantiate()
|
|
path_follow_2d.progress_ratio = randf()
|
|
new_mob.global_position = path_follow_2d.global_position
|
|
new_mob.connect("mob_died", Callable(self, "_on_mob_died"))
|
|
add_child(new_mob)
|
|
|
|
func _on_player_health_depleted() -> void:
|
|
game_over_layer.visible = true
|
|
game_over.emit()
|
|
|
|
func _on_death_sound_finished() -> void:
|
|
get_tree().paused = true
|
|
|
|
func randomly_spawn_trees(tree_count: int) -> void:
|
|
for i in range(tree_count):
|
|
var new_tree = TREE.instantiate()
|
|
new_tree.position = Vector2(randi_range(int(-map_size.x / 2), int(map_size.x / 2)), randi_range(int(-map_size.y / 2), int(map_size.y / 2)))
|
|
new_tree.modulate = Color(randf(), randf(), randf())
|
|
add_child(new_tree)
|
|
|
|
func _on_mob_died(mob_xp: int) -> void:
|
|
game_data.kill_count += 1
|
|
game_data.xp += mob_xp
|
|
print("XP: " + str(game_data.xp) + "/" + str(game_data.xp_next_level))
|
|
if game_data.xp >= game_data.xp_next_level:
|
|
game_data.xp = game_data.xp - game_data.xp_next_level
|
|
game_data.xp_next_level *= 1.5
|
|
game_data.level += 1
|
|
level_up.emit()
|
|
print("Level up to " + str(game_data.level))
|
|
if game_data.level >= game_data.boss_level:
|
|
boss.emit()
|
|
update_ui_data()
|
|
|
|
func spawn_boss() -> void:
|
|
print("Spawn Boss")
|
|
var new_boss = BOSS.instantiate()
|
|
new_boss.mob_data = MobDataResource.new(10, 50, 20)
|
|
path_follow_2d.progress_ratio = randf()
|
|
new_boss.global_position = path_follow_2d.global_position
|
|
new_boss.connect("mob_died", Callable(self, "_on_mob_died"))
|
|
add_child(new_boss)
|
|
|
|
func _on_level_up() -> void:
|
|
# spawn_mob_timer.wait_time = 1 - (game_data.level * 0.1)
|
|
level_up.emit(game_data.level)
|
|
update_ui_data()
|
|
|
|
func update_ui_data() -> void:
|
|
var ui_data = UIDataResource.new(game_data.xp, game_data.xp_next_level, game_data.level, game_data.boss_level, game_data.kill_count)
|
|
update_ui.emit(ui_data)
|