added welcome scene with text rendering using etxt
This commit is contained in:
23
game/game.go
23
game/game.go
@@ -13,7 +13,8 @@ type Game struct {
|
||||
World *ecs.World
|
||||
Bounds image.Rectangle
|
||||
ScreenWidth, ScreenHeight int
|
||||
Scenes map[int]Scene
|
||||
Scenes map[SceneName]Scene
|
||||
CurrentScene SceneName
|
||||
Observer *observers.GameObserver
|
||||
}
|
||||
|
||||
@@ -25,20 +26,26 @@ func NewGame(width, height, cellsize, startlevel int, startscene int) *Game {
|
||||
World: &world,
|
||||
ScreenWidth: width,
|
||||
ScreenHeight: height,
|
||||
Scenes: map[int]Scene{},
|
||||
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[Play] = NewLevelScene(game, startlevel)
|
||||
game.CurrentScene = Welcome
|
||||
|
||||
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
|
||||
@@ -49,8 +56,12 @@ func (game *Game) Update() error {
|
||||
gameobserver.Score++ // FIXME: use level.Score(), see TODO
|
||||
}
|
||||
|
||||
for _, scene := range game.Scenes {
|
||||
scene.Update()
|
||||
scene := game.GetCurrentScene()
|
||||
scene.Update()
|
||||
|
||||
next := scene.GetNext()
|
||||
if next != game.CurrentScene {
|
||||
game.CurrentScene = next
|
||||
}
|
||||
|
||||
timer.Update()
|
||||
@@ -59,9 +70,7 @@ func (game *Game) Update() error {
|
||||
}
|
||||
|
||||
func (game *Game) Draw(screen *ebiten.Image) {
|
||||
for _, scene := range game.Scenes {
|
||||
scene.Draw(screen)
|
||||
}
|
||||
game.GetCurrentScene().Draw(screen)
|
||||
}
|
||||
|
||||
func (g *Game) Layout(newWidth, newHeight int) (int, int) {
|
||||
|
||||
@@ -11,14 +11,14 @@ type LevelScene struct {
|
||||
Game *Game
|
||||
CurrentLevel int
|
||||
Levels []*Level
|
||||
Next int
|
||||
Whoami int
|
||||
Next SceneName
|
||||
Whoami SceneName
|
||||
UseCache bool
|
||||
}
|
||||
|
||||
// Implements the actual playing Scene
|
||||
func NewLevelScene(game *Game, startlevel int) Scene {
|
||||
scene := &LevelScene{CurrentLevel: startlevel, Whoami: Play, Game: game}
|
||||
scene := &LevelScene{CurrentLevel: startlevel, Whoami: Play, Next: Play, Game: game}
|
||||
|
||||
scene.GenerateLevels(game)
|
||||
scene.Levels[scene.CurrentLevel].SetupGrid(game)
|
||||
@@ -33,12 +33,13 @@ func (scene *LevelScene) GenerateLevels(game *Game) {
|
||||
}
|
||||
|
||||
// Interface methods
|
||||
func (scene *LevelScene) SetNext() int {
|
||||
if scene.Whoami != scene.Next {
|
||||
return scene.Next
|
||||
}
|
||||
func (scene *LevelScene) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
return 0
|
||||
func (scene *LevelScene) GetNext() SceneName {
|
||||
// FIXME: set to winner or options screen
|
||||
return scene.Next
|
||||
}
|
||||
|
||||
func (scene *LevelScene) Update() error {
|
||||
|
||||
@@ -18,7 +18,10 @@ const (
|
||||
// to render its content onto the running level, e.g. the options scene
|
||||
// etc.
|
||||
type Scene interface {
|
||||
SetNext() int
|
||||
SetNext(SceneName)
|
||||
GetNext() SceneName
|
||||
Update() error
|
||||
Draw(screen *ebiten.Image)
|
||||
}
|
||||
|
||||
type SceneName int
|
||||
|
||||
62
game/welcome_scene.go
Normal file
62
game/welcome_scene.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"log/slog"
|
||||
"openquell/assets"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/tinne26/etxt"
|
||||
)
|
||||
|
||||
type WelcomeScene struct {
|
||||
Game *Game
|
||||
Next SceneName
|
||||
Whoami SceneName
|
||||
UseCache bool
|
||||
}
|
||||
|
||||
func NewWelcomeScene(game *Game) Scene {
|
||||
scene := &WelcomeScene{Whoami: Welcome, Game: game, Next: Welcome}
|
||||
return scene
|
||||
}
|
||||
|
||||
func (scene *WelcomeScene) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
func (scene *WelcomeScene) GetNext() SceneName {
|
||||
return scene.Next
|
||||
}
|
||||
|
||||
func (scene *WelcomeScene) Update() error {
|
||||
switch {
|
||||
case ebiten.IsKeyPressed(ebiten.KeyEnter):
|
||||
slog.Debug("welcome.Update() next")
|
||||
scene.SetNext(Play)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (scene *WelcomeScene) Draw(screen *ebiten.Image) {
|
||||
screen.Clear()
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
|
||||
background := assets.Assets["background-lila"]
|
||||
screen.DrawImage(background, op)
|
||||
|
||||
blue := color.RGBA{0, 255, 128, 255}
|
||||
|
||||
assets.FontRenderer.Renderer.SetTarget(screen)
|
||||
assets.FontRenderer.Renderer.SetColor(blue)
|
||||
assets.FontRenderer.Renderer.SetAlign(etxt.YCenter, etxt.XCenter)
|
||||
assets.FontRenderer.Renderer.SetSizePx(45)
|
||||
assets.FontRenderer.Renderer.Draw("Welcome to Open Quell!", 320, 200)
|
||||
|
||||
assets.FontRenderer.Renderer.SetAlign(etxt.Top, etxt.Left)
|
||||
assets.FontRenderer.Renderer.SetSizePx(32)
|
||||
assets.FontRenderer.Renderer.Draw("[press enter to start]", 100, 300)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user