ebiten/examples/blocks/blocks/scenemanager.go

105 lines
2.3 KiB
Go
Raw Normal View History

// 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-09 15:16:04 +01:00
2016-10-15 17:09:03 +02:00
// +build example
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
)
var (
transitionFrom *ebiten.Image
transitionTo *ebiten.Image
)
2013-12-18 19:21:25 +01:00
func init() {
var err error
transitionFrom, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
transitionTo, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
2013-12-18 10:05:28 +01:00
}
type Scene interface {
2014-12-29 05:36:29 +01:00
Update(state *GameState) error
Draw(screen *ebiten.Image) error
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
}
}
2014-12-29 05:36:29 +01:00
func (s *SceneManager) Update(state *GameState) error {
2013-12-18 19:21:25 +01:00
if s.transitionCount == -1 {
2014-12-29 05:36:29 +01:00
return s.current.Update(state)
2013-12-18 19:21:25 +01:00
}
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-29 05:36:29 +01:00
return nil
2013-12-18 10:05:28 +01:00
}
2014-12-29 05:36:29 +01:00
func (s *SceneManager) Draw(r *ebiten.Image) error {
2013-12-18 19:21:25 +01:00
if s.transitionCount == -1 {
2014-12-29 05:36:29 +01:00
return s.current.Draw(r)
2013-12-18 19:21:25 +01:00
}
2016-08-02 18:42:46 +02:00
if err := transitionFrom.Clear(); err != nil {
return err
}
2014-12-29 05:36:29 +01:00
if err := s.current.Draw(transitionFrom); err != nil {
return err
}
2013-12-18 19:21:25 +01:00
2016-08-02 18:42:46 +02:00
if err := transitionTo.Clear(); err != nil {
return err
}
2014-12-29 05:36:29 +01:00
if err := s.next.Draw(transitionTo); err != nil {
return err
}
2013-12-18 19:21:25 +01:00
2014-12-29 05:36:29 +01:00
if err := r.DrawImage(transitionFrom, nil); err != nil {
return err
}
2013-12-18 19:21:25 +01:00
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, alpha)
return r.DrawImage(transitionTo, op)
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
}