Refactoring for module packaging.
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cosmos/diego/groovy"
|
||||||
|
splashmenu "cosmos/diego/groovy/examples/splashmenu/scenes"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"groovy/examples/splashmenu"
|
|
||||||
"groovy/groovy"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
@@ -35,4 +36,6 @@ func loadScenes(m *groovy.Manager) {
|
|||||||
|
|
||||||
//sets current scene to the splash menu
|
//sets current scene to the splash menu
|
||||||
m.SetCurrentScene(0)
|
m.SetCurrentScene(0)
|
||||||
|
|
||||||
|
m.SetDimensions(groovy.Area{Width: 1280, Height: 720})
|
||||||
}
|
}
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
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]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package groovy
|
|
||||||
|
|
||||||
import "github.com/hajimehoshi/ebiten/v2"
|
|
||||||
|
|
||||||
type Scene interface {
|
|
||||||
Update() error
|
|
||||||
Draw(screen *ebiten.Image)
|
|
||||||
Completed() bool
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user