From 74c5bf3505996cc49f7ed6fa1871a95b764b5a45 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Tue, 3 May 2016 22:15:47 +0200 Subject: [PATCH] Added scene system. --- game.go | 6 ++++ scene.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ system.go | 2 +- 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 scene.go diff --git a/game.go b/game.go index 51f7102..1b4a127 100644 --- a/game.go +++ b/game.go @@ -90,6 +90,12 @@ func Stop() { } func cleanup() { + // cleanup scenes + for _, scene := range scenes { + scene.Cleanup() + } + + // cleanup systems for _, system := range systems { system.Cleanup() } diff --git a/scene.go b/scene.go new file mode 100644 index 0000000..c732dd8 --- /dev/null +++ b/scene.go @@ -0,0 +1,103 @@ +package goga + +// A scene used to switch between game states. +// The Cleanup() method is called when a scene is removed +// or the program is stopped. It can be used to cleanup open resources +// (like GL objects). +type Scene interface { + Pause() + Resume() + Cleanup() + Resize(int, int) + GetName() string +} + +var ( + scenes []Scene + activeScene Scene +) + +// Adds a scene to game. +// Returns false if the scene exists already. +func AddScene(scene Scene) bool { + for _, s := range scenes { + if s == scene { + return false + } + } + + scenes = append(scenes, scene) + + return true +} + +// Removes a given scene. +// Returns false if it could not be found. +func RemoveScene(scene Scene) bool { + for i, s := range scenes { + if s == scene { + s.Cleanup() + scenes = append(scenes[:i], scenes[i+1:]...) + return true + } + } + + return false +} + +// Removes all scenes. +func RemoveAllScenes() { + for _, s := range scenes { + s.Cleanup() + } + + scenes = make([]Scene, 0) +} + +// Finds and returns a scene by name, or nil if not found. +func GetSceneByName(name string) Scene { + for _, s := range scenes { + if s.GetName() == name { + return s + } + } + + return nil +} + +// Switches to given scene. +// This will pause the currently active scene. +func SwitchScene(scene Scene) { + if activeScene != nil { + activeScene.Pause() + } + + activeScene = scene + + for _, s := range scenes { + if s == activeScene { + activeScene.Resume() + break + } + } +} + +// Switches to given existing scene by name. +// Returns false if the scene does not exist. +func SwitchSceneByName(name string) bool { + scene := GetSceneByName(name) + + if scene == nil { + return false + } + + SwitchScene(scene) + + return true +} + +// Returns the currently active scene. +// Can be nil if no scene was set. +func GetActiveScene() Scene { + return activeScene +} diff --git a/system.go b/system.go index 2bd0544..ac274b8 100644 --- a/system.go +++ b/system.go @@ -5,7 +5,7 @@ import () // A system provides logic for actors satisfying required components. // They are automatically updated on each frame. // When a system is removed from systems, the Cleanup() method will be called. -// This will also happen on program stop. This can be used to cleanup open resources +// This will also happen on program stop. It can be used to cleanup open resources // (like GL objects). type System interface { Update(float64)