bricasse/main.gd

115 lines
3.7 KiB
GDScript

extends Node
var StartScreen = preload("res://screens/start_screen.tscn")
var LevelSelectScreen = preload("res://screens/level_select_screen.tscn")
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")
var HighScores = preload("res://tools/highscores.gd")
var Levels = preload("res://tools/levels.gd")
# 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
var highscores = HighScores.new() # Object to keep the scores
var levels = Levels.new() # Object to keep the levels
func _ready():
# Read the high scores
highscores.load_scores()
# Go to the start screen
goToStartScreen()
func _on_next_screen(next):
if next == "start":
goToStartScreen()
if next == "select":
goToLevelSelectScreen()
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)
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)
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()
current_screen.init(levels.get_current())
current_screen.connect("next_screen", self, "_on_next_screen")
add_child(current_screen)
func goToGameOverScreen(hasWon):
# Retrieve score
var score = 0
var level_name = ""
if current_state == "game":
score = current_screen.get_score()
level_name = current_screen.get_node("LevelName").text
elif game_screen != null:
score = game_screen.get_score()
level_name = game_screen.get_node("LevelName").text
# Clean up current scene
if current_screen != null:
current_screen.queue_free()
if game_screen != null:
game_screen.queue_free()
game_screen = null
# 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)
# Prepare screen
current_state = "game_over"
current_screen = GameOverScreen.instance()
current_screen.init(hasWon, score, is_new_high)
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)