Module'd up.
This commit is contained in:
106
manager.go
Normal file
106
manager.go
Normal file
@@ -0,0 +1,106 @@
|
||||
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
|
||||
currentSceneId int
|
||||
scenes []Scene
|
||||
}
|
||||
|
||||
// can be used to create default manager instance
|
||||
func NewManager() Manager {
|
||||
return Manager{
|
||||
Info: GameInfo{
|
||||
Name: "groovy",
|
||||
Version: "1.0",
|
||||
Dimension: Area{
|
||||
Width: defaultWidth,
|
||||
Height: defaultHeight},
|
||||
},
|
||||
currentSceneId: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
||||
// check for exit condition
|
||||
func (m *Manager) SetDimensions(a Area) {
|
||||
m.Info.Dimension = a
|
||||
}
|
||||
Reference in New Issue
Block a user