57 lines
1.3 KiB
GDScript
57 lines
1.3 KiB
GDScript
extends Node
|
|
|
|
|
|
const GAME: PackedScene = preload("res://scenes/core/game.tscn")
|
|
const HIGH_SCORE_FILE: String = "user://highscore"
|
|
var game_node: Game
|
|
@onready var ui: CanvasLayer = $UI
|
|
@onready var high_score: int:
|
|
set(value):
|
|
high_score = value
|
|
ui.high_counter.text = str(value)
|
|
|
|
|
|
func _ready() -> void:
|
|
load_high_score()
|
|
load_game()
|
|
Relay.game_over.connect(on_game_over)
|
|
Relay.game_restart.connect(on_game_reset)
|
|
Relay.reset_high_score.connect(func():
|
|
high_score = 0
|
|
save_high_score(0))
|
|
|
|
|
|
func _input(_event: InputEvent) -> void:
|
|
if Input.is_action_pressed("ui_cancel"):
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
|
|
|
|
func load_high_score() -> void:
|
|
var file := FileAccess.open(HIGH_SCORE_FILE, FileAccess.READ)
|
|
high_score = file.get_16() if file else 0
|
|
|
|
|
|
func save_high_score(save_score: int) -> void:
|
|
var file := FileAccess.open(HIGH_SCORE_FILE, FileAccess.WRITE)
|
|
file.store_16(save_score)
|
|
|
|
|
|
func on_game_over() -> void:
|
|
if game_node.current_score > high_score:
|
|
high_score = game_node.current_score
|
|
save_high_score(high_score)
|
|
|
|
|
|
#TODO: more methods here for handling game-state, instead of in Game
|
|
func on_game_reset() -> void:
|
|
game_node.queue_free()
|
|
load_game()
|
|
Relay.game_start.emit()
|
|
|
|
|
|
func load_game() -> void:
|
|
var game: Node2D = GAME.instantiate()
|
|
add_child(game)
|
|
game_node = game
|
|
get_tree().paused = true
|