1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-24 17:14:23 +00:00
hella-farm/main.gd

92 lines
2.1 KiB
GDScript3
Raw Normal View History

2024-04-13 01:14:11 +00:00
extends Node
2024-04-13 01:08:16 +00:00
class_name Main
2024-04-13 02:04:43 +00:00
2024-04-13 03:29:43 +00:00
@onready var tree: SceneTree = get_tree()
2024-05-01 23:10:40 +00:00
@onready var ui_container: Control = %"SafeMarginContainer"
2024-04-13 02:04:43 +00:00
## The possible states the game can be in.
enum Stage {
NONE = 0,
MENU = 1,
2024-04-13 03:29:43 +00:00
OPTIONS = 2,
GAME = 3,
2024-04-13 02:04:43 +00:00
}
## The [Stage] the game is currently in.
##
## Changing this will automatically destroy and build the relevant scenes.
var current_stage: Stage:
get:
return current_stage
set(value):
# Do not rebuild scenes if the stage is set to the value it's already set to
if value == current_stage:
return
# Destroy the current scenes
match current_stage:
Stage.MENU:
destroy_menu()
2024-04-14 03:27:42 +00:00
Stage.GAME:
destroy_game()
2024-04-13 02:04:43 +00:00
# Update the current stage
current_stage = value
# Build the next scenes
match current_stage:
Stage.MENU:
build_menu()
2024-04-13 14:38:05 +00:00
Stage.GAME:
build_game()
2024-04-13 02:04:43 +00:00
## The [Stage] that [field current_stage] should be set to upon starting the game.
@export var starting_stage: Stage
## The main menu scene.
2024-04-14 02:58:38 +00:00
const SCENE_MENU: PackedScene = preload("res://scenes/menu/main_menu.tscn")
const SCENE_GAME: PackedScene = preload("res://scenes/game/main_game.tscn")
2024-04-13 02:04:43 +00:00
## The main menu node.
var scene_menu: MainMenu = null
2024-04-13 14:38:05 +00:00
var scene_game: MainGame = null
2024-04-13 02:04:43 +00:00
2024-04-14 03:27:42 +00:00
## Destroy the [MainMenu].
2024-04-13 02:04:43 +00:00
func destroy_menu() -> void:
scene_menu.queue_free()
scene_menu = null
2024-04-14 03:27:42 +00:00
## Build the [MainMenu].
2024-04-13 02:04:43 +00:00
func build_menu() -> void:
scene_menu = SCENE_MENU.instantiate()
2024-04-13 03:29:43 +00:00
scene_menu.selected_play.connect(_on_menu_selected_play)
scene_menu.selected_options.connect(_on_menu_selected_options)
2024-04-14 22:27:17 +00:00
ui_container.add_child(scene_menu)
2024-04-13 02:04:43 +00:00
2024-04-14 03:27:42 +00:00
## Destroy the [MainGame].
func destroy_game() -> void:
scene_game.queue_free()
scene_game = null
## Build the [MainGame].
2024-04-13 14:38:05 +00:00
func build_game() -> void:
scene_game = SCENE_GAME.instantiate()
2024-04-14 03:40:07 +00:00
add_child(scene_game)
2024-04-13 02:04:43 +00:00
func _ready() -> void:
current_stage = starting_stage
2024-04-13 03:29:43 +00:00
func _on_menu_selected_play() -> void:
current_stage = Stage.GAME
func _on_menu_selected_options() -> void:
current_stage = Stage.OPTIONS
2024-04-13 14:38:05 +00:00
func _on_game_selected_exit() -> void:
current_stage = Stage.MENU
2024-04-14 03:40:07 +00:00
func _input(event: InputEvent) -> void:
if event is InputEventKey:
if event.is_action(&"ui_home"):
current_stage = Stage.MENU