last_light/game/game.go

58 lines
985 B
Go
Raw Normal View History

2024-04-24 17:11:33 +03:00
package game
import (
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/engine"
2024-04-24 17:11:33 +03:00
"mvvasilev/last_light/game/state"
2024-06-06 23:17:22 +03:00
"mvvasilev/last_light/game/systems"
2024-04-24 17:11:33 +03:00
"github.com/gdamore/tcell/v2"
)
type Game struct {
2024-06-06 23:17:22 +03:00
turnSystem *systems.TurnSystem
inputSystem *systems.InputSystem
2024-05-30 23:39:54 +03:00
2024-04-24 17:11:33 +03:00
state state.GameState
quitGame bool
2024-04-24 17:11:33 +03:00
}
func CreateGame() *Game {
game := new(Game)
2024-06-06 23:17:22 +03:00
game.turnSystem = systems.CreateTurnSystem()
2024-05-30 23:39:54 +03:00
2024-06-06 23:17:22 +03:00
game.inputSystem = systems.CreateInputSystemWithDefaultBindings()
2024-05-30 23:39:54 +03:00
game.state = state.CreateMainMenuState(game.turnSystem, game.inputSystem)
2024-04-24 17:11:33 +03:00
return game
}
func (g *Game) Input(ev *tcell.EventKey) {
if ev.Key() == tcell.KeyCtrlC {
g.quitGame = true
}
2024-05-30 23:39:54 +03:00
g.inputSystem.Input(g.state.InputContext(), ev)
2024-04-24 17:11:33 +03:00
}
2024-05-30 23:39:54 +03:00
func (g *Game) Tick(dt int64) (continueGame bool) {
continueGame = !g.quitGame
2024-04-24 17:11:33 +03:00
s := g.state.OnTick(dt)
switch s.(type) {
case *state.QuitState:
2024-05-30 23:39:54 +03:00
g.quitGame = true
2024-04-24 17:11:33 +03:00
}
g.state = s
2024-05-30 23:39:54 +03:00
return
2024-04-24 17:11:33 +03:00
}
2024-05-06 20:43:35 +03:00
func (g *Game) CollectDrawables() []engine.Drawable {
return g.state.CollectDrawables()
2024-04-24 17:11:33 +03:00
}