2025-11-13 21:30:44 +01:00
|
|
|
package cmd
|
2024-05-23 14:27:42 +02:00
|
|
|
|
2024-05-28 13:37:32 +02:00
|
|
|
import (
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
2024-05-23 14:27:42 +02:00
|
|
|
|
|
|
|
|
type Game struct {
|
2024-05-30 10:11:44 +02:00
|
|
|
ScreenWidth, ScreenHeight, ReadlWidth, Cellsize int
|
|
|
|
|
Scenes map[SceneName]Scene
|
|
|
|
|
CurrentScene SceneName
|
|
|
|
|
Config *Config
|
|
|
|
|
Scale float32
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 12:29:43 +02:00
|
|
|
func NewGame(config *Config, startscene SceneName) *Game {
|
|
|
|
|
game := &Game{
|
|
|
|
|
Config: config,
|
|
|
|
|
Scenes: map[SceneName]Scene{},
|
|
|
|
|
ScreenWidth: config.ScreenWidth,
|
|
|
|
|
ScreenHeight: config.ScreenHeight,
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 13:08:36 +02:00
|
|
|
// setup scene[s]
|
2024-05-26 12:29:43 +02:00
|
|
|
game.CurrentScene = startscene
|
|
|
|
|
game.Scenes[Play] = NewPlayScene(game, config)
|
2024-05-30 12:32:58 +02:00
|
|
|
game.Scenes[Menu] = NewMenuScene(game, config)
|
2024-05-30 19:45:13 +02:00
|
|
|
game.Scenes[Options] = NewOptionsScene(game, config)
|
2024-06-06 19:55:16 +02:00
|
|
|
game.Scenes[Keybindings] = NewKeybindingsScene(game, config)
|
2024-05-23 14:27:42 +02:00
|
|
|
|
2024-05-26 13:08:36 +02:00
|
|
|
// setup environment
|
|
|
|
|
ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight)
|
|
|
|
|
ebiten.SetWindowTitle("golsky - conway's game of life")
|
|
|
|
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
2024-05-31 21:20:13 +02:00
|
|
|
ebiten.SetScreenClearedEveryFrame(true)
|
2024-05-31 14:19:30 +02:00
|
|
|
|
2024-05-26 12:29:43 +02:00
|
|
|
return game
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 12:29:43 +02:00
|
|
|
func (game *Game) GetCurrentScene() Scene {
|
|
|
|
|
return game.Scenes[game.CurrentScene]
|
2024-05-24 17:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 12:29:43 +02:00
|
|
|
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
2024-05-30 10:11:44 +02:00
|
|
|
game.ReadlWidth = outsideWidth
|
|
|
|
|
game.Scale = float32(game.ScreenWidth) / float32(outsideWidth)
|
2024-05-26 12:29:43 +02:00
|
|
|
return game.ScreenWidth, game.ScreenHeight
|
2024-05-24 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 12:29:43 +02:00
|
|
|
func (game *Game) Update() error {
|
|
|
|
|
scene := game.GetCurrentScene()
|
2024-06-03 18:38:18 +02:00
|
|
|
|
|
|
|
|
if quit := scene.Update(); quit != nil {
|
|
|
|
|
return quit
|
|
|
|
|
}
|
2024-05-24 20:27:08 +02:00
|
|
|
|
2024-06-01 00:33:40 +02:00
|
|
|
next := scene.GetNext()
|
|
|
|
|
if next != game.CurrentScene {
|
2024-06-03 17:44:17 +02:00
|
|
|
game.Scenes[next].SetPrevious(game.CurrentScene)
|
2024-06-01 00:33:40 +02:00
|
|
|
scene.ResetNext()
|
|
|
|
|
game.CurrentScene = next
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 20:22:28 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2024-06-01 00:33:40 +02:00
|
|
|
|
2024-06-01 20:22:28 +02:00
|
|
|
func (game *Game) Draw(screen *ebiten.Image) {
|
|
|
|
|
// first draw primary scene[s], although there are only 1
|
|
|
|
|
for current, scene := range game.Scenes {
|
|
|
|
|
if scene.IsPrimary() {
|
|
|
|
|
// primary scenes always draw
|
|
|
|
|
scene.Draw(screen)
|
|
|
|
|
|
|
|
|
|
if current == game.CurrentScene {
|
|
|
|
|
// avoid to redraw it in the next step
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-01 00:33:40 +02:00
|
|
|
}
|
2024-06-01 20:22:28 +02:00
|
|
|
|
|
|
|
|
scene := game.GetCurrentScene()
|
|
|
|
|
scene.Draw(screen)
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|