2021-09-19 23:35:49 +02:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
var StartScreen = preload("res://screens/start_screen.tscn")
|
2021-11-07 21:16:26 +01:00
|
|
|
var LevelSelectScreen = preload("res://screens/level_select_screen.tscn")
|
2021-09-19 23:35:49 +02:00
|
|
|
var GameScreen = preload("res://screens/game_screen.tscn")
|
|
|
|
var GameOverScreen = preload("res://screens/game_over_screen.tscn")
|
|
|
|
var PauseScreen = preload("res://screens/pause_screen.tscn")
|
2021-11-07 21:16:26 +01:00
|
|
|
var HighScores = preload("res://tools/highscores.gd")
|
|
|
|
var Levels = preload("res://tools/levels.gd")
|
2021-09-19 23:35:49 +02:00
|
|
|
|
|
|
|
# Variables
|
|
|
|
var current_screen = null # Current displayed screen
|
|
|
|
var current_state = "" # Current state
|
|
|
|
var game_screen = null # To store the game screen while in pause
|
2021-11-07 21:16:26 +01:00
|
|
|
var highscores = HighScores.new() # Object to keep the scores
|
|
|
|
var levels = Levels.new() # Object to keep the levels
|
2021-09-19 23:35:49 +02:00
|
|
|
|
|
|
|
func _ready():
|
2021-11-07 21:16:26 +01:00
|
|
|
# Read the high scores
|
|
|
|
highscores.load_scores()
|
|
|
|
# Go to the start screen
|
2021-09-19 23:35:49 +02:00
|
|
|
goToStartScreen()
|
|
|
|
|
|
|
|
func _on_next_screen(next):
|
|
|
|
if next == "start":
|
|
|
|
goToStartScreen()
|
2021-11-07 21:16:26 +01:00
|
|
|
if next == "select":
|
|
|
|
goToLevelSelectScreen()
|
2021-09-19 23:35:49 +02:00
|
|
|
elif next == "game":
|
|
|
|
goToGameScreen()
|
|
|
|
elif next == "game_won":
|
|
|
|
goToGameOverScreen(true)
|
|
|
|
elif next == "game_lost":
|
|
|
|
goToGameOverScreen(false)
|
|
|
|
elif next == "pause":
|
|
|
|
goToPauseScreen()
|
|
|
|
|
|
|
|
func goToStartScreen():
|
|
|
|
if current_screen != null:
|
|
|
|
current_screen.queue_free()
|
|
|
|
if game_screen != null:
|
|
|
|
game_screen.queue_free()
|
|
|
|
game_screen = null
|
|
|
|
current_state = "start"
|
|
|
|
current_screen = StartScreen.instance()
|
|
|
|
current_screen.init()
|
|
|
|
current_screen.connect("next_screen", self, "_on_next_screen")
|
|
|
|
add_child(current_screen)
|
|
|
|
|
2021-11-07 21:16:26 +01:00
|
|
|
func goToLevelSelectScreen():
|
|
|
|
# Clean up current scene
|
|
|
|
if current_screen != null:
|
|
|
|
current_screen.queue_free()
|
|
|
|
if game_screen != null:
|
|
|
|
game_screen.queue_free()
|
|
|
|
game_screen = null
|
|
|
|
current_state = "select"
|
|
|
|
current_screen = LevelSelectScreen.instance()
|
|
|
|
current_screen.init(levels, highscores)
|
|
|
|
current_screen.connect("next_screen", self, "_on_next_screen")
|
|
|
|
add_child(current_screen)
|
|
|
|
|
2021-09-19 23:35:49 +02:00
|
|
|
func goToGameScreen():
|
|
|
|
if current_screen != null:
|
|
|
|
current_screen.queue_free()
|
|
|
|
current_state = "game"
|
|
|
|
if game_screen != null:
|
|
|
|
current_screen = game_screen
|
|
|
|
current_screen.unpause()
|
|
|
|
game_screen = null
|
|
|
|
else:
|
|
|
|
current_screen = GameScreen.instance()
|
2021-11-07 21:16:26 +01:00
|
|
|
current_screen.init(levels.get_current())
|
2021-09-19 23:35:49 +02:00
|
|
|
current_screen.connect("next_screen", self, "_on_next_screen")
|
|
|
|
add_child(current_screen)
|
|
|
|
|
|
|
|
func goToGameOverScreen(hasWon):
|
|
|
|
# Retrieve score
|
|
|
|
var score = 0
|
2021-11-07 21:16:26 +01:00
|
|
|
var level_name = ""
|
2021-09-19 23:35:49 +02:00
|
|
|
if current_state == "game":
|
|
|
|
score = current_screen.get_score()
|
2021-11-07 21:16:26 +01:00
|
|
|
level_name = current_screen.get_node("LevelName").text
|
2021-09-19 23:35:49 +02:00
|
|
|
elif game_screen != null:
|
|
|
|
score = game_screen.get_score()
|
2021-11-07 21:16:26 +01:00
|
|
|
level_name = game_screen.get_node("LevelName").text
|
2021-09-19 23:35:49 +02:00
|
|
|
# Clean up current scene
|
|
|
|
if current_screen != null:
|
|
|
|
current_screen.queue_free()
|
|
|
|
if game_screen != null:
|
|
|
|
game_screen.queue_free()
|
|
|
|
game_screen = null
|
2021-11-07 21:16:26 +01:00
|
|
|
# Highscores are updated only when a level is won (terminated)
|
|
|
|
var is_new_high = false
|
|
|
|
if hasWon:
|
|
|
|
is_new_high = highscores.set_score(level_name, score)
|
2021-09-19 23:35:49 +02:00
|
|
|
# Prepare screen
|
|
|
|
current_state = "game_over"
|
|
|
|
current_screen = GameOverScreen.instance()
|
2021-11-07 21:16:26 +01:00
|
|
|
current_screen.init(hasWon, score, is_new_high)
|
2021-09-19 23:35:49 +02:00
|
|
|
current_screen.connect("next_screen", self, "_on_next_screen")
|
|
|
|
add_child(current_screen)
|
|
|
|
|
|
|
|
func goToPauseScreen():
|
|
|
|
if current_state == "game":
|
|
|
|
game_screen = current_screen
|
|
|
|
game_screen.pause()
|
|
|
|
elif current_screen != null:
|
|
|
|
current_screen.queue_free()
|
|
|
|
current_state = "pause"
|
|
|
|
current_screen = PauseScreen.instance()
|
|
|
|
current_screen.init()
|
|
|
|
current_screen.connect("next_screen", self, "_on_next_screen")
|
|
|
|
add_child(current_screen)
|