ebiten/example/blocks/scenemanager.go

101 lines
2.3 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-12-08 17:35:35 +01:00
package blocks
2013-12-18 10:05:28 +01:00
import (
2014-12-09 14:09:22 +01:00
"github.com/hajimehoshi/ebiten"
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-12-09 14:09:22 +01:00
Draw(context ebiten.GraphicsContext, 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-12-09 14:09:22 +01:00
func (s *SceneManager) Draw(context ebiten.GraphicsContext, 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()
2014-12-09 14:09:22 +01:00
color := ebiten.ColorMatrixI()
ebiten.DrawWhole(
2014-05-03 06:58:18 +02:00
context.RenderTarget(from),
ScreenWidth,
ScreenHeight,
2014-12-09 14:09:22 +01:00
ebiten.GeometryMatrixI(),
2014-05-03 06:58:18 +02:00
color)
2013-12-18 19:21:25 +01:00
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha
2014-12-09 14:09:22 +01:00
ebiten.DrawWhole(
2014-05-03 06:58:18 +02:00
context.RenderTarget(to),
ScreenWidth,
ScreenHeight,
2014-12-09 14:09:22 +01:00
ebiten.GeometryMatrixI(),
2014-05-03 06:58:18 +02:00
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
}