ebiten/example/blocks/scene_manager.go

76 lines
1.6 KiB
Go
Raw Normal View History

2013-12-18 10:05:28 +01:00
package blocks
import (
"github.com/hajimehoshi/go-ebiten/graphics"
2013-12-18 19:21:25 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2013-12-18 10:05:28 +01:00
)
2013-12-18 19:21:25 +01:00
func init() {
renderTargetSizes["scene_manager_transition_from"] =
Size{ScreenWidth, ScreenHeight}
renderTargetSizes["scene_manager_transition_to"] =
Size{ScreenWidth, ScreenHeight}
2013-12-18 10:05:28 +01:00
}
type Scene interface {
2013-12-18 19:21:25 +01:00
Update(state *GameState)
2013-12-18 10:05:28 +01:00
Draw(context graphics.Context)
}
2013-12-18 19:21:25 +01:00
const transitionMaxCount = 20
2013-12-18 10:05:28 +01:00
type SceneManager struct {
2013-12-18 19:21:25 +01:00
current Scene
next Scene
transitionCount int
2013-12-18 10:05:28 +01:00
}
func NewSceneManager(initScene Scene) *SceneManager {
return &SceneManager{
2013-12-18 19:21:25 +01:00
current: initScene,
transitionCount: -1,
2013-12-18 10:05:28 +01:00
}
}
2013-12-18 19:21:25 +01:00
func (s *SceneManager) Update(state *GameState) {
if s.transitionCount == -1 {
s.current.Update(state)
return
}
s.transitionCount++
if transitionMaxCount <= s.transitionCount {
2013-12-18 10:05:28 +01:00
s.current = s.next
s.next = nil
2013-12-18 19:21:25 +01:00
s.transitionCount = -1
2013-12-18 10:05:28 +01:00
}
}
func (s *SceneManager) Draw(context graphics.Context) {
2013-12-18 19:21:25 +01:00
if s.transitionCount == -1 {
s.current.Draw(context)
return
}
from := drawInfo.renderTargets["scene_manager_transition_from"]
context.SetOffscreen(from)
context.Clear()
2013-12-18 10:05:28 +01:00
s.current.Draw(context)
2013-12-18 19:21:25 +01:00
to := drawInfo.renderTargets["scene_manager_transition_to"]
context.SetOffscreen(to)
context.Clear()
s.next.Draw(context)
context.ResetOffscreen()
color := matrix.IdentityColor()
2014-05-01 15:45:48 +02:00
context.RenderTarget(from).Draw(matrix.IdentityGeometry(), color)
2013-12-18 19:21:25 +01:00
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha
2014-05-01 15:45:48 +02:00
context.RenderTarget(to).Draw(matrix.IdentityGeometry(), color)
2013-12-18 10:05:28 +01:00
}
func (s *SceneManager) GoTo(scene Scene) {
s.next = scene
2013-12-18 19:21:25 +01:00
s.transitionCount = 0
2013-12-18 10:05:28 +01:00
}