2013-12-18 10:05:28 +01:00
|
|
|
package blocks
|
|
|
|
|
|
|
|
import (
|
2014-12-05 14:16:58 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/ui"
|
2014-05-03 10:50:42 +02:00
|
|
|
_ "image/png"
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-11 12:44:36 +02:00
|
|
|
type Textures interface {
|
|
|
|
RequestTexture(name string, path string)
|
|
|
|
RequestRenderTarget(name string, size Size)
|
|
|
|
Has(name string) bool
|
|
|
|
GetTexture(name string) graphics.TextureId
|
|
|
|
GetRenderTarget(name string) graphics.RenderTargetId
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:05:28 +01:00
|
|
|
type Game struct {
|
|
|
|
sceneManager *SceneManager
|
2013-12-18 19:21:25 +01:00
|
|
|
input *Input
|
2014-05-11 12:44:36 +02:00
|
|
|
textures Textures
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 12:44:36 +02:00
|
|
|
func NewGame(textures Textures) *Game {
|
2013-12-18 10:05:28 +01:00
|
|
|
game := &Game{
|
|
|
|
sceneManager: NewSceneManager(NewTitleScene()),
|
2013-12-18 19:21:25 +01:00
|
|
|
input: NewInput(),
|
2014-05-11 12:44:36 +02:00
|
|
|
textures: textures,
|
2014-05-03 08:25:41 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
return game
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) isInitialized() bool {
|
2014-05-03 08:25:41 +02:00
|
|
|
for name, _ := range texturePaths {
|
|
|
|
if !game.textures.Has(name) {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
2014-05-03 08:25:41 +02:00
|
|
|
for name, _ := range renderTargetSizes {
|
|
|
|
if !game.textures.Has(name) {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-12 03:04:28 +02:00
|
|
|
func (game *Game) Update(state ui.InputState) {
|
2013-12-18 10:05:28 +01:00
|
|
|
if !game.isInitialized() {
|
|
|
|
return
|
|
|
|
}
|
2014-05-12 03:04:28 +02:00
|
|
|
game.input.Update(state.PressedKeys())
|
2013-12-18 19:21:25 +01:00
|
|
|
game.sceneManager.Update(&GameState{
|
|
|
|
SceneManager: game.sceneManager,
|
|
|
|
Input: game.input,
|
|
|
|
})
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Draw(context graphics.Context) {
|
|
|
|
if !game.isInitialized() {
|
|
|
|
return
|
|
|
|
}
|
2014-05-03 08:25:41 +02:00
|
|
|
game.sceneManager.Draw(context, game.textures)
|
2013-12-18 10:05:28 +01:00
|
|
|
}
|