aboutsummaryrefslogtreecommitdiff
path: root/go/client
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-12-27 12:40:19 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:58 +0100
commit4e2ab55d345fd5e5c5d2d5bc56948611b15564d5 (patch)
treec6998ac54687c442ad0baa541edd0fbf917b1308 /go/client
parent5aa8e7ddbc41aed55e7918e6afaec0b5c07fa510 (diff)
downloadmuhqs-game-4e2ab55d345fd5e5c5d2d5bc56948611b15564d5.tar.gz
muhqs-game-4e2ab55d345fd5e5c5d2d5bc56948611b15564d5.zip
introduce game.State interface abstraction
This allows to not use the internal game state directly from client code.
Diffstat (limited to 'go/client')
-rw-r--r--go/client/game.go47
-rw-r--r--go/client/startMenu.go12
2 files changed, 27 insertions, 32 deletions
diff --git a/go/client/game.go b/go/client/game.go
index 38a2971c..50f0400b 100644
--- a/go/client/game.go
+++ b/go/client/game.go
@@ -53,18 +53,19 @@ type Game struct {
triggers []*game.TriggeredAction
ui.Collection
- activePlayer *game.Player
+ activePlayerId int
playerCtrl *game.ChanPlayerControl
hasPriority bool
- gameState *game.State
+ gameState game.State
storesOnMap bool
}
-func newGame(app *app) *Game {
+func newGame(app *app, gameState game.State) *Game {
g := &Game{
app: app,
+ gameState: gameState,
keyBindings: make(keyBindings),
Collection: ui.Collection{
Width: app.windowWidth,
@@ -77,8 +78,6 @@ func newGame(app *app) *Game {
g.keyBindings[ebiten.KeySpace] = pass
g.keyBindings[ebiten.KeyEscape] = reset
- g.gameState = game.NewState()
-
g.passButton = ui.NewSimpleButton(g.Width-PASS_BUTTON_WIDTH,
g.Height-DEFAULT_BUTTON_HEIGHT,
PASS_BUTTON_WIDTH,
@@ -102,7 +101,7 @@ func (g *Game) loadMap(mapName string) *Game {
if err != nil {
log.Fatal(err)
}
- g.gameState.Map = m
+ g.gameState.SetMap(m)
g.mapView = ui.NewMapView(g.gameState)
g.AddWidget(g.mapView)
@@ -113,24 +112,18 @@ func (g *Game) loadMap(mapName string) *Game {
func (g *Game) addActivePlayer(name string, deckList string) *Game {
deck := game.NewDeckFromDeckList(deckList)
- color := playerColors[len(g.gameState.Players)]
- g.gameState.AddNewPlayer(name, deck)
- p := g.getPlayer(name)
- p.Color = color
+ color := playerColors[len(g.gameState.Players())]
+ g.gameState.AddNewPlayer(name, deck, color)
+ p := g.gameState.PlayerByName(name)
+ g.activePlayerId = p.Id
return g.initPlayerUi(p)
}
-func (g *Game) getPlayer(name string) *game.Player {
- for _, p := range g.gameState.Players {
- if p.Name == name {
- return p
- }
- }
- return nil
+func (g *Game) activePlayer() *game.Player {
+ return g.gameState.PlayerById(g.activePlayerId)
}
func (g *Game) initPlayerUi(player *game.Player) *Game {
- g.activePlayer = player
g.playerCtrl = game.NewChanPlayerControl(player)
player.Ctrl = g.playerCtrl
var x, y int
@@ -168,7 +161,7 @@ func (g *Game) initPlayerUi(player *game.Player) *Game {
x = g.discardPileButton.X + g.discardPileButton.Width/2
y = g.Height - DEFAULT_BUTTON_HEIGHT
- g.discardPileView = ui.NewPocList(x, y, g.activePlayer.DiscardPile)
+ g.discardPileView = ui.NewPocList(x, y, g.activePlayer().DiscardPile)
if !g.storesOnMap {
x = g.stateBar.Width + g.discardPileButton.Width
@@ -188,7 +181,7 @@ func (g *Game) initPlayerUi(player *game.Player) *Game {
x = g.storeButton.X + g.storeButton.Width/2
y = g.Height - DEFAULT_BUTTON_HEIGHT
- g.storeView = ui.NewPocList(x, y, g.activePlayer.Store)
+ g.storeView = ui.NewPocList(x, y, g.activePlayer().Store)
}
return g
@@ -406,7 +399,7 @@ func (g *Game) updatePassButton() {
}
func (g *Game) passPriority() {
- g.declareAction(game.NewPassPriority(g.activePlayer))
+ g.declareAction(game.NewPassPriority(g.activePlayer()))
}
func (g *Game) handlePlayerNotifications() {
@@ -423,14 +416,14 @@ func (g *Game) handlePlayerNotifications() {
}
a := n.Context.(game.Action)
g.stackBuffer.AddLine(a.String())
- if !g.gameState.Stack.IsEmpty() && g.FindWidget(g.stackBuffer) == -1 {
+ if !g.gameState.Stack().IsEmpty() && g.FindWidget(g.stackBuffer) == -1 {
g.AddWidget(g.stackBuffer)
}
case game.ResolvedActionNotification:
g.clearMapHighlights()
g.stackBuffer.RemoveLast()
- if g.gameState.Stack.IsEmpty() {
+ if g.gameState.Stack().IsEmpty() {
g.RemoveWidget(g.stackBuffer)
}
g.mapView.ForceRedraw()
@@ -494,13 +487,13 @@ func (g *Game) handleSelection(obj interface{}, x, y int) {
switch obj := obj.(type) {
case *game.Tile:
if g.storesOnMap && obj.Type == game.TileTypes.Store &&
- g.activePlayer.KnowsStore(obj.Position) {
+ g.activePlayer().KnowsStore(obj.Position) {
if g.storeView != nil {
g.hideStore()
}
- g.storeView = ui.NewPocList(x, y, g.gameState.Map.StoreOn(obj.Position))
+ g.storeView = ui.NewPocList(x, y, g.gameState.Map().StoreOn(obj.Position))
g.showStore()
}
@@ -516,13 +509,13 @@ func (g *Game) handleSelection(obj interface{}, x, y int) {
0,
func(nc *ui.NumberChoice) {
a := game.NewPlayActionVariadicCosts(
- g.activePlayer,
+ g.activePlayer(),
obj.C,
nc.GetChoosen())
g.progressPlayAction(a)
}))
} else {
- a := game.NewPlayAction(g.activePlayer, obj.C)
+ a := game.NewPlayAction(g.activePlayer(), obj.C)
g.progressPlayAction(a)
}
diff --git a/go/client/startMenu.go b/go/client/startMenu.go
index 94ec3aef..0fc7cbe7 100644
--- a/go/client/startMenu.go
+++ b/go/client/startMenu.go
@@ -3,7 +3,7 @@ package main
import (
"log"
- // "muhq.space/muhqs-game/go/game"
+ "muhq.space/muhqs-game/go/game"
"muhq.space/muhqs-game/go/ui"
)
@@ -132,17 +132,19 @@ func (m *StartMenu) Layout(width, height int) (int, int) {
}
func (m *StartMenu) startGame() {
+ var state game.State
if m.remote != "" {
log.Fatal("Remote games are currently not implemented")
+ } else {
+ state = game.NewLocalState()
}
- g := newGame(m.app).loadMap(m.mapPath).addActivePlayer(m.playerName, m.startDeck)
+ g := newGame(m.app, state).loadMap(m.mapPath).addActivePlayer(m.playerName, m.startDeck)
if m.mapPath == "the-kraken" {
- idx := len(g.gameState.Players)
+ idx := len(g.gameState.Players())
color := playerColors[idx]
- g.gameState.AddKraken()
- g.gameState.Players[idx].Color = color
+ g.gameState.AddNewAiPlayer("kraken", color)
}
m.app.pushActivity(g.Start())