From 4e4ce8b14c0a7daf49d77bb408e2ff7cd6181c68 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 9 Jan 2023 12:25:20 +0100 Subject: [PATCH] Refactor `BaseScene` --- base/Base.gd | 26 ------------------------- base/Base.tscn | 6 ------ base/BaseScene.gd | 46 +++++++++++++++++++++++++++++++++++++++++++++ base/BaseScene.tscn | 6 ++++++ project.godot | 8 +++++++- 5 files changed, 59 insertions(+), 33 deletions(-) delete mode 100644 base/Base.gd delete mode 100644 base/Base.tscn create mode 100644 base/BaseScene.gd create mode 100644 base/BaseScene.tscn diff --git a/base/Base.gd b/base/Base.gd deleted file mode 100644 index ee462bf..0000000 --- a/base/Base.gd +++ /dev/null @@ -1,26 +0,0 @@ -extends Node - - -var current_scn: Node = null -func change_scene(path: String): - var res: Resource = load(path) - var scn: Node = res.instantiate() - add_child(scn) - if current_scn: - current_scn.queue_free() - current_scn = scn - - -func start_game(): - print("Starting game...") - change_scene("res://island/Island.tscn") - - -func goto_menu(): - print("Going to the menu...") - change_scene("res://menu/Menu.tscn") - current_scn.connect("play_pressed", start_game) - - -func _ready(): - goto_menu() diff --git a/base/Base.tscn b/base/Base.tscn deleted file mode 100644 index c4dea27..0000000 --- a/base/Base.tscn +++ /dev/null @@ -1,6 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://vtsmwim5ayl8"] - -[ext_resource type="Script" path="res://base/Base.gd" id="1_fhviw"] - -[node name="Base" type="Node"] -script = ExtResource("1_fhviw") diff --git a/base/BaseScene.gd b/base/BaseScene.gd new file mode 100644 index 0000000..7a9b45f --- /dev/null +++ b/base/BaseScene.gd @@ -0,0 +1,46 @@ +extends Node +class_name BaseScene + +## The scene manager is about to change the scene to the Island. +signal moving_to_island() + +## The scene manager has changed the scene to the Island. +signal moved_to_island() + +## The scene manager is about the change the scene back to the main menu. +signal moving_to_menu() + +## The scene manager has changed the scene back to the main menu. +signal moved_to_menu() + + +## The currently loaded scene. +var current_scene: Node = null: + get: + return current_scene + set(new_scene): + if current_scene: + current_scene.queue_free() + current_scene = new_scene + add_child(current_scene) + + +## Change the current scene to the Island. +func move_to_island(): + print("Starting game...") + emit_signal("moving_to_island") + current_scene = load("res://island/Island.tscn").instantiate() + emit_signal("moved_to_island") + +## Change the current scene to the main menu. +func move_to_menu(): + print("Going to the menu...") + emit_signal("moving_to_menu") + current_scene = load("res://menu/Menu.tscn").instantiate() + current_scene.connect("play_pressed", move_to_island) + emit_signal("moved_to_menu") + + +func _ready(): + # By default, standby in the menu + move_to_menu() diff --git a/base/BaseScene.tscn b/base/BaseScene.tscn new file mode 100644 index 0000000..3f3c231 --- /dev/null +++ b/base/BaseScene.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://cj86x6gplsbrb"] + +[ext_resource type="Script" path="res://base/BaseScene.gd" id="1_n2o7j"] + +[node name="BaseScene" type="Node"] +script = ExtResource("1_n2o7j") diff --git a/project.godot b/project.godot index 1aee164..3f0ffa1 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=5 _global_script_classes=[{ +"base": "Node", +"class": &"BaseScene", +"language": &"GDScript", +"path": "res://base/BaseScene.gd" +}, { "base": "Node3D", "class": &"BaseWeapon", "language": &"GDScript", @@ -40,6 +45,7 @@ _global_script_classes=[{ "path": "res://player/PlayerInputData.gd" }] _global_script_class_icons={ +"BaseScene": "", "BaseWeapon": "", "CanSplash": "", "CropTile": "", @@ -51,7 +57,7 @@ _global_script_class_icons={ [application] config/name="LD52" -run/main_scene="res://base/Base.tscn" +run/main_scene="res://base/BaseScene.tscn" config/features=PackedStringArray("4.0", "GL Compatibility") boot_splash/bg_color=Color(0.231373, 0.490196, 0.309804, 1) config/icon="res://assets/icon.svg"