80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package game
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"openquell/observers"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/mlange-42/arche/ecs"
|
|
)
|
|
|
|
type Game struct {
|
|
World *ecs.World
|
|
Bounds image.Rectangle
|
|
ScreenWidth, ScreenHeight int
|
|
Scenes map[SceneName]Scene
|
|
CurrentScene SceneName
|
|
Observer *observers.GameObserver
|
|
}
|
|
|
|
func NewGame(width, height, cellsize, startlevel int, startscene SceneName) *Game {
|
|
world := ecs.NewWorld()
|
|
|
|
game := &Game{
|
|
Bounds: image.Rectangle{},
|
|
World: &world,
|
|
ScreenWidth: width,
|
|
ScreenHeight: height,
|
|
Scenes: map[SceneName]Scene{},
|
|
}
|
|
|
|
observers.NewPlayerObserver(&world)
|
|
observers.NewParticleObserver(&world)
|
|
game.Observer = observers.NewGameObserver(&world, startlevel, width, height, cellsize)
|
|
|
|
game.Scenes[Welcome] = NewWelcomeScene(game)
|
|
game.Scenes[Select] = NewSelectScene(game)
|
|
game.Scenes[Play] = NewLevelScene(game, startlevel)
|
|
game.CurrentScene = startscene
|
|
|
|
fmt.Println(game.World.Stats().String())
|
|
|
|
return game
|
|
}
|
|
|
|
func (game *Game) GetCurrentScene() Scene {
|
|
return game.Scenes[game.CurrentScene]
|
|
}
|
|
|
|
func (game *Game) Update() error {
|
|
gameobserver := observers.GetGameObserver(game.World)
|
|
timer := gameobserver.StopTimer
|
|
|
|
if timer.IsReady() {
|
|
timer.Reset()
|
|
gameobserver.CurrentLevel++
|
|
gameobserver.Score++ // FIXME: use level.Score(), see TODO
|
|
}
|
|
|
|
scene := game.GetCurrentScene()
|
|
scene.Update()
|
|
|
|
next := scene.GetNext()
|
|
if next != game.CurrentScene {
|
|
game.CurrentScene = next
|
|
}
|
|
|
|
timer.Update()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (game *Game) Draw(screen *ebiten.Image) {
|
|
game.GetCurrentScene().Draw(screen)
|
|
}
|
|
|
|
func (g *Game) Layout(newWidth, newHeight int) (int, int) {
|
|
return g.ScreenWidth, g.ScreenHeight
|
|
}
|