2024-05-12 22:42:28 -04:00
|
|
|
extends Node2D
|
|
|
|
|
2024-05-13 08:15:21 -04:00
|
|
|
const COMET_SPAWNER = preload("res://scenes/comet_spawner.tscn")
|
|
|
|
|
2024-05-12 22:42:28 -04:00
|
|
|
@export var level : int = 1
|
|
|
|
|
2024-05-14 17:56:34 -04:00
|
|
|
var save_file := "user://highscore.dat"
|
|
|
|
|
2024-05-13 17:14:48 -04:00
|
|
|
var viewport_size : Vector2
|
2024-05-13 08:15:21 -04:00
|
|
|
var score : int = 0
|
2024-05-14 17:56:34 -04:00
|
|
|
var high_score : int
|
2024-05-12 22:42:28 -04:00
|
|
|
var points_per_comet := 300
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2024-05-14 17:56:34 -04:00
|
|
|
high_score = load_score()
|
2024-05-13 08:15:21 -04:00
|
|
|
new_level(level)
|
2024-05-13 17:14:48 -04:00
|
|
|
viewport_size = get_viewport_rect().size
|
|
|
|
$Background/Stars.emission_rect_extents = viewport_size
|
2024-05-12 22:42:28 -04:00
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
update_ui()
|
|
|
|
|
|
|
|
func new_level(current_level : int) -> void:
|
2024-05-13 08:15:21 -04:00
|
|
|
var comet_spawner := COMET_SPAWNER.instantiate()
|
|
|
|
comet_spawner.comet_count *= current_level
|
|
|
|
comet_spawner.comet_shot.connect(increase_score)
|
|
|
|
comet_spawner.screen_cleared.connect(level_finished)
|
|
|
|
self.add_child(comet_spawner)
|
|
|
|
|
|
|
|
func level_finished() -> void:
|
|
|
|
level += 1
|
|
|
|
new_level(level)
|
2024-05-12 22:42:28 -04:00
|
|
|
|
|
|
|
func game_over() -> void:
|
2024-05-13 17:14:48 -04:00
|
|
|
# Wait 1 second before showing the game over screen for "dramatic" effect
|
|
|
|
await get_tree().create_timer(1.0).timeout
|
|
|
|
$UI/GameOverScreen.visible = true
|
2024-05-14 17:56:34 -04:00
|
|
|
if score > high_score:
|
|
|
|
save_score()
|
2024-05-12 22:42:28 -04:00
|
|
|
|
|
|
|
func increase_score(comet_size : int) -> void:
|
|
|
|
# We add one since comet_size starts count at 0
|
|
|
|
comet_size += 1
|
|
|
|
score += points_per_comet / comet_size
|
|
|
|
|
|
|
|
func update_ui()-> void:
|
|
|
|
# Label strings must be all caps for font to render proper
|
|
|
|
$UI/Score.text = "SCORE: " + str(score)
|
|
|
|
$UI/Level.text = "LEVEL: " + str(level)
|
2024-05-14 17:56:34 -04:00
|
|
|
$UI/HighScore.text = "HIGH SCORE:\n" + str(high_score)
|
|
|
|
|
|
|
|
func save_score() -> void:
|
|
|
|
var file := FileAccess.open(save_file, FileAccess.WRITE)
|
|
|
|
file.store_var(score)
|
2024-05-13 20:02:52 -04:00
|
|
|
|
2024-05-14 17:56:34 -04:00
|
|
|
func load_score() -> int:
|
|
|
|
if FileAccess.file_exists(save_file):
|
|
|
|
var file := FileAccess.open(save_file, FileAccess.READ)
|
|
|
|
return file.get_var(score)
|
|
|
|
else:
|
|
|
|
return 0
|
2024-05-13 20:02:52 -04:00
|
|
|
|
|
|
|
|