ebiten/example/blocks/scene_manager.go

86 lines
1.8 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)
2014-05-11 12:44:36 +02:00
Draw(context graphics.Context, textures Textures)
2013-12-18 10:05:28 +01:00
}
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
}
}
2014-05-11 12:44:36 +02:00
func (s *SceneManager) Draw(context graphics.Context, textures Textures) {
2013-12-18 19:21:25 +01:00
if s.transitionCount == -1 {
2014-05-03 08:25:41 +02:00
s.current.Draw(context, textures)
2013-12-18 19:21:25 +01:00
return
}
2014-05-03 08:25:41 +02:00
from := textures.GetRenderTarget("scene_manager_transition_from")
2013-12-18 19:21:25 +01:00
context.SetOffscreen(from)
context.Clear()
2014-05-03 08:25:41 +02:00
s.current.Draw(context, textures)
2013-12-18 19:21:25 +01:00
2014-05-03 08:25:41 +02:00
to := textures.GetRenderTarget("scene_manager_transition_to")
2013-12-18 19:21:25 +01:00
context.SetOffscreen(to)
context.Clear()
2014-05-03 08:25:41 +02:00
s.next.Draw(context, textures)
2013-12-18 19:21:25 +01:00
context.ResetOffscreen()
color := matrix.IdentityColor()
2014-05-03 06:58:18 +02:00
graphics.DrawWhole(
context.RenderTarget(from),
ScreenWidth,
ScreenHeight,
matrix.IdentityGeometry(),
color)
2013-12-18 19:21:25 +01:00
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha
2014-05-03 06:58:18 +02:00
graphics.DrawWhole(
context.RenderTarget(to),
ScreenWidth,
ScreenHeight,
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
}