Change Game.Update

This commit is contained in:
Hajime Hoshi 2013-07-05 21:02:17 +09:00
parent 30cdcfd574
commit 572f4057a8
8 changed files with 24 additions and 14 deletions

View File

@ -29,12 +29,12 @@ type Game interface {
ScreenHeight() int
Fps() int
Init(tf graphics.TextureFactory)
Update(inputState InputState)
Draw(g graphics.Context)
Update(context GameContext)
Draw(context graphics.Context)
}
type UI interface {
Run()
type GameContext interface {
InputState() InputState
}
type InputState struct {

View File

@ -47,7 +47,7 @@ func (game *Blank) Fps() int {
func (game *Blank) Init(tf graphics.TextureFactory) {
}
func (game *Blank) Update(inputState ebiten.InputState) {
func (game *Blank) Update(context ebiten.GameContext) {
}
func (game *Blank) Draw(context graphics.Context) {

View File

@ -67,8 +67,8 @@ func (game *Input) Init(tf graphics.TextureFactory) {
}
}
func (game *Input) Update(inputState ebiten.InputState) {
game.inputState = inputState
func (game *Input) Update(context ebiten.GameContext) {
game.inputState = context.InputState()
}
func (game *Input) Draw(g graphics.Context) {

View File

@ -115,7 +115,7 @@ func (game *Monochrome) update() {
}
}
func (game *Monochrome) Update(inputState ebiten.InputState) {
func (game *Monochrome) Update(context ebiten.GameContext) {
game.ch <- true
<-game.ch
}

View File

@ -53,7 +53,7 @@ func (game *Rects) Init(tf graphics.TextureFactory) {
game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight())
}
func (game *Rects) Update(inputState ebiten.InputState) {
func (game *Rects) Update(context ebiten.GameContext) {
}
func (game *Rects) Draw(g graphics.Context) {

View File

@ -68,7 +68,7 @@ func (game *Rotating) Init(tf graphics.TextureFactory) {
}
}
func (game *Rotating) Update(inputState ebiten.InputState) {
func (game *Rotating) Update(context ebiten.GameContext) {
game.x++
}

View File

@ -125,7 +125,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
}
}
func (game *Sprites) Update(inputState ebiten.InputState) {
func (game *Sprites) Update(context ebiten.GameContext) {
for _, sprite := range game.sprites {
sprite.Update()
}

View File

@ -122,6 +122,14 @@ func (ui *GlutUI) Run() {
C.glutMainLoop()
}
type GameContext struct {
inputState ebiten.InputState
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
@ -188,12 +196,14 @@ func main() {
frameTime := time.Duration(
int64(time.Second) / int64(game.Fps()))
update := time.Tick(frameTime)
inputState := ebiten.InputState{-1, -1}
gameContext := &GameContext{
inputState: ebiten.InputState{-1, -1},
}
for {
select {
case inputState = <-input:
case gameContext.inputState = <-input:
case <-update:
game.Update(inputState)
game.Update(gameContext)
case drawing := <-draw:
ch := make(chan interface{})
drawing <- func(context graphics.Context) {