bricasse/main.gd

87 lines
2.6 KiB
GDScript

extends Node
var StartScreen = preload("res://screens/start_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")
# 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
func _ready():
goToStartScreen()
func _on_next_screen(next):
if next == "start":
goToStartScreen()
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 goToGameScreen():
var start_level = 0
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(start_level)
current_screen.connect("next_screen", self, "_on_next_screen")
add_child(current_screen)
func goToGameOverScreen(hasWon):
# Retrieve score
var score = 0
if current_state == "game":
score = current_screen.get_score()
elif game_screen != null:
score = game_screen.get_score()
# Clean up current scene
if current_screen != null:
current_screen.queue_free()
if game_screen != null:
game_screen.queue_free()
game_screen = null
# Prepare screen
current_state = "game_over"
current_screen = GameOverScreen.instance()
current_screen.init(hasWon, score)
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)