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"
|
2014-12-14 09:28:19 +01:00
|
|
|
"sync"
|
2013-12-18 10:05:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Size struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2014-05-03 08:25:41 +02:00
|
|
|
// TODO: Should they be global??
|
2013-12-18 19:21:25 +01:00
|
|
|
var texturePaths = map[string]string{}
|
|
|
|
var renderTargetSizes = map[string]Size{}
|
|
|
|
|
2014-05-03 08:25:41 +02:00
|
|
|
const ScreenWidth = 256
|
|
|
|
const ScreenHeight = 240
|
2013-12-18 10:05:28 +01:00
|
|
|
|
2013-12-18 19:21:25 +01:00
|
|
|
type GameState struct {
|
|
|
|
SceneManager *SceneManager
|
|
|
|
Input *Input
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:05:28 +01:00
|
|
|
type Game struct {
|
2014-12-14 09:28:19 +01:00
|
|
|
once sync.Once
|
2013-12-18 10:05:28 +01:00
|
|
|
sceneManager *SceneManager
|
2014-12-10 17:06:38 +01:00
|
|
|
input *Input
|
2014-12-06 17:09:59 +01:00
|
|
|
textures *Textures
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
|
2014-12-06 17:09:59 +01:00
|
|
|
func NewGame() *Game {
|
2013-12-18 10:05:28 +01:00
|
|
|
game := &Game{
|
|
|
|
sceneManager: NewSceneManager(NewTitleScene()),
|
2014-12-10 17:06:38 +01:00
|
|
|
input: NewInput(),
|
2014-05-03 08:25:41 +02:00
|
|
|
}
|
2014-12-06 17:09:59 +01:00
|
|
|
return game
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:05:28 +01:00
|
|
|
func (game *Game) isInitialized() bool {
|
2014-12-10 17:06:38 +01:00
|
|
|
if game.textures == nil {
|
|
|
|
return false
|
|
|
|
}
|
2014-12-06 14:56:57 +01:00
|
|
|
for name := range texturePaths {
|
2014-05-03 08:25:41 +02:00
|
|
|
if !game.textures.Has(name) {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
2014-12-06 14:56:57 +01:00
|
|
|
for name := range renderTargetSizes {
|
2014-05-03 08:25:41 +02:00
|
|
|
if !game.textures.Has(name) {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-12-06 20:14:35 +01:00
|
|
|
func (game *Game) Update() error {
|
2014-12-14 09:28:19 +01:00
|
|
|
game.once.Do(func() {
|
|
|
|
game.textures = NewTextures()
|
|
|
|
for name, path := range texturePaths {
|
|
|
|
game.textures.RequestTexture(name, path)
|
|
|
|
}
|
|
|
|
for name, size := range renderTargetSizes {
|
|
|
|
game.textures.RequestRenderTarget(name, size)
|
|
|
|
}
|
|
|
|
})
|
2013-12-18 10:05:28 +01:00
|
|
|
if !game.isInitialized() {
|
2014-12-06 20:14:35 +01:00
|
|
|
return nil
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
2014-12-14 09:28:19 +01:00
|
|
|
game.input.Update()
|
2013-12-18 19:21:25 +01:00
|
|
|
game.sceneManager.Update(&GameState{
|
|
|
|
SceneManager: game.sceneManager,
|
2014-12-10 17:06:38 +01:00
|
|
|
Input: game.input,
|
2013-12-18 19:21:25 +01:00
|
|
|
})
|
2014-12-06 20:14:35 +01:00
|
|
|
return nil
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 17:06:38 +01:00
|
|
|
func (game *Game) Draw(g ebiten.GraphicsContext) error {
|
2013-12-18 10:05:28 +01:00
|
|
|
if !game.isInitialized() {
|
2014-12-06 20:14:35 +01:00
|
|
|
return nil
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
2014-12-10 17:06:38 +01:00
|
|
|
game.sceneManager.Draw(g, game.textures)
|
2014-12-06 20:14:35 +01:00
|
|
|
return nil
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|