CORE/smgr.gd

160 lines
5.5 KiB
GDScript3
Raw Normal View History

2023-03-18 16:34:12 +01:00
# smgr.gd
# Scene Manager
#
# This file is part of StarOpenSource CORE (SOSCORE)
# Made by the StarOpenSource Project and Contributers
# Licensed under GNU GPLv3
extends Node
2023-05-30 02:08:28 +02:00
@onready
var core = get_node(NodePath("/root/core"))
2023-03-18 16:34:12 +01:00
var ccr = null
var ccr_game = null
var ccr_game_inuse = false
var ccr_menu = null
var ccr_menu_names = []
var ccr_cutscene = null
var ccr_cutscene_inuse = false
var ccr_debug = null
2023-03-18 18:02:17 +01:00
var ccr_debug_names = []
2023-03-18 16:34:12 +01:00
2023-05-30 02:08:28 +02:00
func add_game(resname:String) -> bool:
Logger.diag("smgr","Adding game scene")
2023-03-18 16:34:12 +01:00
if ccr_game_inuse:
Logger.error("smgr","Failed adding game scene: Only one game scene is allowed")
return false
2023-05-30 02:08:28 +02:00
var res = core.resmgr.getres(resname)
if res == null or res.get_class() != "PackedScene":
Logger.error("smgr","Resource is not type of PackedScene")
return false
ccr_game.add_child(res.instantiate())
2023-03-18 16:34:12 +01:00
ccr_game_inuse = true
Logger.diag("smgr","Added game scene")
2023-03-18 16:34:12 +01:00
return true
func remove_game() -> bool:
Logger.diag("smgr","Removing game scene")
2023-03-18 16:34:12 +01:00
if !ccr_game_inuse:
Logger.error("smgr","Failed removing game scene: No game scene is active")
return false
ccr_game.remove_child(ccr_game.get_child(0))
ccr_game_inuse = false
Logger.diag("smgr","Removed game scene")
2023-03-18 16:34:12 +01:00
return true
2023-05-30 02:42:05 +02:00
func add_menu(resname:String) -> bool:
Logger.diag("smgr","Adding menu scene \"" + resname + "\"")
2023-05-30 02:42:05 +02:00
if ccr_menu_names.has(resname):
Logger.error("smgr","Failed adding menu scene: Menu scene with name \"" + resname + "\" already exists")
2023-03-18 16:34:12 +01:00
return false
2023-05-30 02:08:28 +02:00
var res = core.resmgr.getres(resname)
if res == null or res.get_class() != "PackedScene":
Logger.error("smgr","Resource is not type of PackedScene")
return false
ccr_menu.add_child(res.instantiate())
2023-05-30 02:42:05 +02:00
ccr_menu_names.append(resname)
Logger.diag("smgr","Added menu scene \"" + resname + "\"")
2023-03-18 16:34:12 +01:00
return true
func remove_menu(scene_name:String) -> bool:
Logger.diag("smgr","Removing menu scene \"" + scene_name + "\"")
2023-03-18 16:34:12 +01:00
if !ccr_menu_names.has(scene_name):
Logger.error("smgr","Failed removing menu scene: Menu scene with name \"" + scene_name + "\" does not exist")
return false
for i in ccr_menu.get_children():
if i.name == scene_name:
ccr_menu.remove_child(i)
break
ccr_menu_names.erase(scene_name)
Logger.diag("smgr","Removed menu scene \"" + scene_name + "\"")
2023-03-18 16:34:12 +01:00
return true
2023-05-30 02:08:28 +02:00
func add_cutscene(resname:String) -> bool:
Logger.diag("smgr","Adding cutscene scene")
2023-03-18 16:34:12 +01:00
if ccr_cutscene_inuse:
Logger.error("smgr","Failed adding cutscene scene: Only one cutscene scene is allowed")
return false
2023-05-30 02:08:28 +02:00
var res = core.resmgr.getres(resname)
if res == null or res.get_class() != "PackedScene":
Logger.error("smgr","Resource is not type of PackedScene")
return false
ccr_cutscene.add_child(res.instantiate())
2023-03-18 16:34:12 +01:00
ccr_cutscene_inuse = true
Logger.diag("smgr","Added cutscene scene")
2023-03-18 16:34:12 +01:00
return true
func remove_cutscene() -> bool:
Logger.diag("smgr","Removing cutscene scene")
2023-03-18 16:34:12 +01:00
if !ccr_cutscene_inuse:
Logger.error("smgr","Failed removing cutscene scene: No cutscene scene is active")
return false
ccr_cutscene.remove_child(ccr_cutscene.get_child(0))
ccr_cutscene_inuse = false
Logger.diag("smgr","Removed cutscene scene")
2023-03-18 16:34:12 +01:00
return true
2023-05-30 02:42:05 +02:00
func add_debug(resname:String) -> bool:
Logger.diag("smgr","Adding debug scene \"" + resname + "\"")
2023-05-30 02:42:05 +02:00
if ccr_debug_names.has(resname):
Logger.error("smgr","Failed adding debug scene: Debug scene with name \"" + resname + "\" already exists")
2023-03-18 18:02:17 +01:00
return false
2023-05-30 02:08:28 +02:00
var res = core.resmgr.getres(resname)
if res == null or res.get_class() != "PackedScene":
Logger.error("smgr","Resource is not type of PackedScene")
return false
ccr_debug.add_child(res.instantiate())
2023-05-30 02:42:05 +02:00
ccr_debug_names.append(resname)
Logger.diag("smgr","Added debug scene \"" + resname + "\"")
2023-03-18 18:02:17 +01:00
return true
func remove_debug(scene_name:String) -> bool:
Logger.diag("smgr","Removing debug scene \"" + scene_name + "\"")
2023-03-18 18:02:17 +01:00
if !ccr_menu_names.has(scene_name):
Logger.error("smgr","Failed removing debug scene: Debug scene with name \"" + scene_name + "\" does not exist")
return false
for i in ccr_debug.get_children():
if i.name == scene_name:
ccr_debug.remove_child(i)
break
ccr_debug_names.erase(scene_name)
Logger.diag("smgr","Removed debug scene \"" + scene_name + "\"")
2023-03-18 18:02:17 +01:00
return true
2023-03-18 16:34:12 +01:00
func _ready() -> void:
Logger.diag("smgr","Constructing CORE Scene Root (CCR)")
2023-03-18 16:34:12 +01:00
ccr = Control.new()
ccr.name = "ccr"
Logger.diag("smgr","Adding CCR to /root/")
2023-03-18 16:34:12 +01:00
get_tree().root.add_child(ccr)
Logger.diag("smgr","Migrating COREBackground to CCR")
2023-03-18 16:34:12 +01:00
var corebg = get_tree().current_scene
Logger.diag("smgr","Reconstructing COREBackground to CCR")
2023-03-18 16:34:12 +01:00
var corebg_log = corebg.get_node(NodePath("Background/Log")).text
corebg.get_node(NodePath("Background/Log")).rmconnect()
get_tree().current_scene.queue_free()
var corebg_new = ResourceLoader.load("res://CORE/background.tscn").instantiate()
ccr.add_child(corebg_new)
corebg_new.get_node(NodePath("Background/Log")).text = corebg_log
Logger.diag("smgr","COREBackground migration complete.")
Logger.diag("smgr","Adding overlays")
2023-03-18 16:34:12 +01:00
add_overlays()
2023-05-30 02:08:28 +02:00
core.setready()
2023-03-18 16:34:12 +01:00
func add_overlays() -> void:
Logger.diag("smgr","Adding game overlay")
2023-03-18 16:34:12 +01:00
ccr_game = Control.new()
ccr_game.name = "GameOverlay"
ccr.add_child(ccr_game)
Logger.diag("smgr","Adding menu overlay")
2023-03-18 16:34:12 +01:00
ccr_menu = Control.new()
ccr_menu.name = "MenuOverlay"
ccr.add_child(ccr_menu)
Logger.diag("smgr","Adding cutscene overlay")
2023-03-18 16:34:12 +01:00
ccr_cutscene = Control.new()
ccr_cutscene.name = "CutsceneOverlay"
ccr.add_child(ccr_cutscene)
Logger.diag("smgr","Adding debug overlay")
2023-03-18 16:34:12 +01:00
ccr_debug = Control.new()
ccr_debug.name = "DebugOverlay"
ccr.add_child(ccr_debug)