2023-08-21 08:04:25 -04:00
|
|
|
package groovy
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultWidth = 1024
|
|
|
|
|
defaultHeight = 768
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Area struct {
|
|
|
|
|
Width int
|
|
|
|
|
Height int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GameInfo struct {
|
|
|
|
|
Name string
|
|
|
|
|
Version string
|
|
|
|
|
Dimension Area
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
|
Info GameInfo
|
|
|
|
|
currentScene Scene
|
2023-08-23 01:24:43 -04:00
|
|
|
currentSceneId uint
|
|
|
|
|
nextSceneId uint
|
2023-08-21 08:04:25 -04:00
|
|
|
scenes []Scene
|
2023-08-23 01:24:43 -04:00
|
|
|
EventMap map[SceneEvent]func()
|
2023-08-21 08:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// can be used to create default manager instance
|
|
|
|
|
func NewManager() Manager {
|
|
|
|
|
return Manager{
|
|
|
|
|
Info: GameInfo{
|
|
|
|
|
Name: "groovy",
|
|
|
|
|
Version: "1.0",
|
|
|
|
|
Dimension: Area{
|
|
|
|
|
Width: defaultWidth,
|
2023-08-23 01:24:43 -04:00
|
|
|
Height: defaultHeight,
|
|
|
|
|
},
|
2023-08-21 08:04:25 -04:00
|
|
|
},
|
|
|
|
|
currentSceneId: 0,
|
2023-08-23 01:24:43 -04:00
|
|
|
nextSceneId: 1,
|
2023-08-21 08:04:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ebitengine update proxy
|
|
|
|
|
// manages scene transition and exists, then calls scene's update method
|
|
|
|
|
func (m *Manager) Update() error {
|
|
|
|
|
if m.currentScene == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//call the current scene's update method
|
|
|
|
|
return m.currentScene.Update()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 01:24:43 -04:00
|
|
|
func (m *Manager) Quit() {
|
|
|
|
|
os.Exit(0)
|
2023-08-21 08:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calls current scene's draw method if the currentscene is valid
|
|
|
|
|
func (m *Manager) Draw(screen *ebiten.Image) {
|
|
|
|
|
if m.currentScene != nil {
|
|
|
|
|
m.currentScene.Draw(screen)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ebitengine proxy for layout
|
|
|
|
|
func (m *Manager) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
|
|
|
return m.Info.Dimension.Width, m.Info.Dimension.Height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// appends scene to the managed scenes
|
|
|
|
|
func (m *Manager) AddScene(s Scene) {
|
2023-08-23 01:24:43 -04:00
|
|
|
setDefaultHandlers(m, s)
|
2023-08-21 08:04:25 -04:00
|
|
|
m.scenes = append(m.scenes, s)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 01:24:43 -04:00
|
|
|
// 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() })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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()
|
2023-08-21 08:04:25 -04:00
|
|
|
}
|
2023-08-23 01:24:43 -04:00
|
|
|
m.currentSceneId = sceneId
|
|
|
|
|
m.currentScene = m.scenes[sceneId]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Manager) TransitionScene() {
|
|
|
|
|
m.SetCurrentScene(m.nextSceneId)
|
2023-08-21 08:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-23 01:24:43 -04:00
|
|
|
func (m *Manager) SetNextScene(sceneId uint) {
|
|
|
|
|
m.nextSceneId = sceneId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sets sene dimensions
|
2023-08-21 08:04:25 -04:00
|
|
|
func (m *Manager) SetDimensions(a Area) {
|
|
|
|
|
m.Info.Dimension = a
|
|
|
|
|
}
|
2023-08-23 01:24:43 -04:00
|
|
|
|
|
|
|
|
// report number of total scenes
|
|
|
|
|
func (m *Manager) SceneCount() uint {
|
|
|
|
|
return uint(len(m.scenes))
|
|
|
|
|
}
|