CORE/src/sms.gd

139 lines
6.1 KiB
GDScript3
Raw Normal View History

2024-02-09 21:18:14 +01:00
##############################################################################
### CORE FRAMEWORK SOURCE FILE ###
### Copyright (c) 2024 The StarOpenSource Project & Contributors ###
### Licensed under the GNU General Public License v3 ###
### ###
### This program is free software: you can redistribute it and/or modify ###
### it under the terms of the GNU General Public License as published by ###
### the Free Software Foundation, either version 3 of the License, or ###
### (at your option) any later version. ###
### ###
### This program is distributed in the hope that it will be useful, ###
### but WITHOUT ANY WARRANTY; without even the implied warranty of ###
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ###
### GNU General Public License for more details. ###
### ###
### You should have received a copy of the GNU General Public License ###
### along with this program. If not, see <https://www.gnu.org/licenses/>. ###
##############################################################################
### src/sms.gd (Scene Management System) ###
### ###
### Makes scene management way smarter and easier. ###
##############################################################################
extends CoreBaseModule
# Objects
var scenes_debug: Node = Node.new()
var scenes_cutscene: Node = Node.new()
var scenes_menu: Node = Node.new()
var scenes_main: Node = Node.new()
var scenes_background: Node = Node.new()
# Variables
var scenes: Dictionary = {}
func _initialize() -> void:
scenes_debug.name = "DEBUG"
scenes_cutscene.name = "CUTSCENE"
scenes_menu.name = "MENU"
scenes_main.name = "MAIN"
scenes_background.name = "BACKGROUND"
add_child(scenes_background)
2024-02-09 23:03:41 +01:00
add_child(scenes_main)
add_child(scenes_menu)
add_child(scenes_cutscene)
add_child(scenes_debug)
2024-02-09 21:18:14 +01:00
func _pull_config() -> void:
if core.config.headless:
# Remove all scenes
logger.verb("Removing all scenes (triggered by headless mode)")
for scene in scenes: remove_scene(scene, true)
# Add a scene to some scene collection
func add_scene(sname: String, type: CoreTypes.SceneType, sclass: Node) -> bool:
if core.config.headless: return false
logger.verb("Adding scene \"" + sname + "\" of type " + str(type))
if exists(sname) != CoreTypes.SceneType.NONE:
logger.error("Scene with name \"" + sname + "\" already exists")
return true
sclass.name = sname
match(type):
CoreTypes.SceneType.DEBUG: scenes_debug.add_child(sclass)
CoreTypes.SceneType.CUTSCENE: scenes_cutscene.add_child(sclass)
CoreTypes.SceneType.MENU: scenes_menu.add_child(sclass)
CoreTypes.SceneType.MAIN: scenes_main.add_child(sclass)
CoreTypes.SceneType.BACKGROUND: scenes_background.add_child(sclass)
CoreTypes.SceneType.NONE:
logger.error("CoreTypes.SceneType.NONE is not a valid scene type")
return false
_: await logger.crash("Invalid SceneType " + str(type), true)
scenes.merge({ sname: { "type": type, "class": sclass } })
return true
# Remove a scene from some scene collection
func remove_scene(sname: String, force_remove: bool = false) -> bool:
if core.config.headless and !force_remove: return false
if force_remove: await logger.crash("force_remove = true is not allowed", true)
logger.verb("Removing scene \"" + sname + "\"")
match(exists(sname)):
CoreTypes.SceneType.DEBUG: scenes_debug.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.CUTSCENE: scenes_cutscene.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.MENU: scenes_menu.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.MAIN: scenes_main.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.BACKGROUND: scenes_background.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.NONE:
logger.error("Scene \"" + sname + "\" does not exist")
return false
_: await logger.crash("Invalid SceneType " + str(exists(sname)), true)
scenes.erase(sname)
return true
# Return a loaded scene
func get_scene(sname: String) -> Node:
if core.config.headless: return null
match(exists(sname)):
CoreTypes.SceneType.DEBUG: return scenes[sname]["class"]
CoreTypes.SceneType.CUTSCENE: return scenes[sname]["class"]
CoreTypes.SceneType.MENU: return scenes[sname]["class"]
CoreTypes.SceneType.MAIN: return scenes[sname]["class"]
CoreTypes.SceneType.BACKGROUND: return scenes[sname]["class"]
CoreTypes.SceneType.NONE: logger.error("Scene \"" + sname + "\" does not exist")
_: await logger.crash("Invalid SceneType " + str(exists(sname)), true)
return null
# Return a scene collection for scene manipulation
func get_scene_collection(type: CoreTypes.SceneType) -> Node:
if core.config.headless: return null
match(type):
CoreTypes.SceneType.DEBUG: return scenes_debug
CoreTypes.SceneType.CUTSCENE: return scenes_cutscene
CoreTypes.SceneType.MENU: return scenes_menu
CoreTypes.SceneType.MAIN: return scenes_main
CoreTypes.SceneType.BACKGROUND: return scenes_background
CoreTypes.SceneType.NONE: logger.error("No scene collection was found for CoreTypes.SceneType.NONE")
_: await logger.crash("Invalid SceneType " + str(type), true)
return null
# Return scenes in some scene collection
func get_scene_collection_list(type: CoreTypes.SceneType) -> Array[Node]:
var list: Array[Node] = []
for scene in scenes:
if scenes[scene]["type"] == type:
list.append(scene)
return list
# Return scene count in some scene collection
func get_scene_collection_count(type: CoreTypes.SceneType) -> int:
var amount: int = 0
for scene in scenes:
if scene[scene]["type"] == type:
amount = amount+1
return amount
# Return scene existance & scene collection
func exists(sname: String) -> CoreTypes.SceneType:
2024-02-09 23:03:41 +01:00
for scene in scenes:
if scene == sname: return scenes[scene]["type"]
return CoreTypes.SceneType.NONE