Added scene system.

This commit is contained in:
Marvin Blum
2016-05-03 22:15:47 +02:00
parent 364d7dd045
commit 74c5bf3505
3 changed files with 110 additions and 1 deletions

View File

@@ -90,6 +90,12 @@ func Stop() {
}
func cleanup() {
// cleanup scenes
for _, scene := range scenes {
scene.Cleanup()
}
// cleanup systems
for _, system := range systems {
system.Cleanup()
}

103
scene.go Normal file
View File

@@ -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
}

View File

@@ -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)