Implemented scene events and callback handlers for those events. Manager defaults also added.
This commit is contained in:
81
manager.go
81
manager.go
@@ -25,8 +25,10 @@ type GameInfo struct {
|
||||
type Manager struct {
|
||||
Info GameInfo
|
||||
currentScene Scene
|
||||
currentSceneId int
|
||||
currentSceneId uint
|
||||
nextSceneId uint
|
||||
scenes []Scene
|
||||
EventMap map[SceneEvent]func()
|
||||
}
|
||||
|
||||
// can be used to create default manager instance
|
||||
@@ -37,9 +39,11 @@ func NewManager() Manager {
|
||||
Version: "1.0",
|
||||
Dimension: Area{
|
||||
Width: defaultWidth,
|
||||
Height: defaultHeight},
|
||||
Height: defaultHeight,
|
||||
},
|
||||
},
|
||||
currentSceneId: 0,
|
||||
nextSceneId: 1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,29 +54,13 @@ func (m *Manager) Update() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.CheckTransitions()
|
||||
m.CheckExit()
|
||||
|
||||
//call the current scene's update method
|
||||
return m.currentScene.Update()
|
||||
|
||||
}
|
||||
|
||||
// check for exit condition
|
||||
func (m *Manager) CheckExit() {
|
||||
if m.currentSceneId >= len(m.scenes) {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
// check for scene completion and if reached, set up next scene (if available)
|
||||
func (m *Manager) CheckTransitions() {
|
||||
if m.currentScene.Completed() {
|
||||
m.currentSceneId++
|
||||
if m.currentSceneId < len(m.scenes) {
|
||||
m.currentScene = m.scenes[m.currentSceneId]
|
||||
}
|
||||
}
|
||||
func (m *Manager) Quit() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// calls current scene's draw method if the currentscene is valid
|
||||
@@ -89,18 +77,57 @@ func (m *Manager) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHe
|
||||
|
||||
// appends scene to the managed scenes
|
||||
func (m *Manager) AddScene(s Scene) {
|
||||
setDefaultHandlers(m, s)
|
||||
m.scenes = append(m.scenes, s)
|
||||
}
|
||||
|
||||
// sets the current scene, based on sceneindex
|
||||
func (m *Manager) SetCurrentScene(sceneId int) {
|
||||
if sceneId >= 0 && sceneId < len(m.scenes) {
|
||||
m.currentSceneId = sceneId
|
||||
m.currentScene = m.scenes[m.currentSceneId]
|
||||
}
|
||||
// sets the default callback handlers for a given scene within manager
|
||||
// Default Handling behaviours:
|
||||
//
|
||||
// reset: sets (scene, nextscene) to {0, 1}
|
||||
// scene completion: sets (scene, nextscene) to {nextscene, nextscene+1}
|
||||
// end game: shutdown groovy
|
||||
//
|
||||
// note: NOOP and RELOAD are purposefully not mapped; they are scene
|
||||
// specific and should be mapped to by user of groovy
|
||||
func setDefaultHandlers(m *Manager, s Scene) {
|
||||
s.SetEventHandler(RESET, func() { m.ResetScenes() })
|
||||
s.SetEventHandler(COMPLETED, func() { m.TransitionScene() })
|
||||
s.SetEventHandler(ENDGAME, func() { m.Quit() })
|
||||
}
|
||||
|
||||
// check for exit condition
|
||||
// we're going to reset the scene to the first one
|
||||
func (m *Manager) ResetScenes() {
|
||||
m.currentSceneId = 0
|
||||
m.nextSceneId = 1
|
||||
m.SetCurrentScene(0)
|
||||
}
|
||||
|
||||
// sets the current scene, based on sceneindex n
|
||||
// n > scenelist, quit
|
||||
// otherwise, scene = n
|
||||
func (m *Manager) SetCurrentScene(sceneId uint) {
|
||||
if sceneId >= uint(len(m.scenes)) {
|
||||
m.Quit()
|
||||
}
|
||||
m.currentSceneId = sceneId
|
||||
m.currentScene = m.scenes[sceneId]
|
||||
}
|
||||
|
||||
func (m *Manager) TransitionScene() {
|
||||
m.SetCurrentScene(m.nextSceneId)
|
||||
}
|
||||
|
||||
func (m *Manager) SetNextScene(sceneId uint) {
|
||||
m.nextSceneId = sceneId
|
||||
}
|
||||
|
||||
// sets sene dimensions
|
||||
func (m *Manager) SetDimensions(a Area) {
|
||||
m.Info.Dimension = a
|
||||
}
|
||||
|
||||
// report number of total scenes
|
||||
func (m *Manager) SceneCount() uint {
|
||||
return uint(len(m.scenes))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user