aboutsummaryrefslogtreecommitdiff
path: root/go/client
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2022-12-27 14:29:39 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:46:38 +0200
commitd6b5eeaa5e639c5ab89b5e1c93075a1b765794dc (patch)
treef87422bb91c2197f88aa8d29092592858764049d /go/client
parent3c2ee4ee48480a28dc606878503f314ec8862d45 (diff)
downloadmuhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.tar.gz
muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.zip
intermediate commit
Diffstat (limited to 'go/client')
-rw-r--r--go/client/game.go531
-rw-r--r--go/client/main.go95
-rw-r--r--go/client/startMenu.go92
3 files changed, 718 insertions, 0 deletions
diff --git a/go/client/game.go b/go/client/game.go
new file mode 100644
index 00000000..00ef4153
--- /dev/null
+++ b/go/client/game.go
@@ -0,0 +1,531 @@
+package main
+
+import (
+ "fmt"
+ "log"
+
+ "github.com/hajimehoshi/ebiten/v2"
+ "github.com/hajimehoshi/ebiten/v2/ebitenutil"
+ "github.com/hajimehoshi/ebiten/v2/inpututil"
+
+ "muhq.space/muhqs-game/go/game"
+ "muhq.space/muhqs-game/go/ui"
+)
+
+const (
+ DEFAULT_BUTTON_HEIGHT = 40
+ PASS_BUTTON_WIDTH = 100
+
+ RESOLVE_BUTTON_X = 500
+ STACK_BUFFER_WIDTH = 300
+
+ HOVER_THRESHOLD = 60
+
+ HAND_VIEW_HEIGHT = 500
+ HAND_VIEW_WIDTH = 500
+
+ STATE_BAR_WIDTH = 300
+)
+
+// A simple structure to detect if the cursor has not moved for HOVER_THRESHOLD
+// ticks.
+// hoverDetector.update must be called during each Update invocation to track the
+// frame and the current cursor position.
+type hoverDetector struct {
+ last_x, last_y int
+ ticks int
+ // ResetFunc is called when the cursor has moved.
+ // If resetFunc returns true the hoverDetector is reset
+ // otherwise the reset is skipped.
+ resetFunc func() bool
+}
+
+func (h *hoverDetector) update(x, y int) {
+ if x == h.last_x && y == h.last_y {
+ h.ticks++
+ } else {
+ h.last_x, h.last_y = x, y
+ h.reset()
+ }
+}
+
+func (h *hoverDetector) reset() {
+ if h.resetFunc != nil && !h.resetFunc() {
+ return
+ }
+ h.ticks = 0
+ h.resetFunc = nil
+}
+
+func (h *hoverDetector) startedHovering() bool {
+ return h.ticks == HOVER_THRESHOLD
+}
+
+type Game struct {
+ width, height int
+
+ selectedObject interface{}
+ hoverDetector hoverDetector
+
+ handLayer *ui.HandView
+ stateBar *ui.StateBar
+ mapView *ui.MapView
+ actionChoice *ui.Choice
+ passButton *ui.SimpleButton
+ discardPileView *ui.PocList
+ discardPileButton *ui.SimpleButton
+ storeView *ui.PocList
+ storeButton *ui.SimpleButton
+ stackBuffer *ui.Buffer
+ prompt *ui.Prompt
+ widgets []ui.Widget
+
+ activePlayer *game.Player
+
+ playerCtrl *game.ChanPlayerControl
+ hasPriority bool
+
+ gameState *game.State
+ storesOnMap bool
+}
+
+func newGame(app *app) *Game {
+ g := &Game{
+ width: app.windowWidth,
+ height: app.windowHeight,
+ }
+
+ g.widgets = []ui.Widget{}
+ g.gameState = game.NewState()
+
+ g.passButton = ui.NewSimpleButton(g.width-PASS_BUTTON_WIDTH,
+ g.height-DEFAULT_BUTTON_HEIGHT,
+ PASS_BUTTON_WIDTH,
+ DEFAULT_BUTTON_HEIGHT,
+ "Pass",
+ func(*ui.SimpleButton) {
+ if g.prompt != nil {
+ a := g.prompt.Confirm()
+ g.declareAction(a)
+ g.removeWidget(g.prompt)
+ g.prompt = nil
+
+ } else if g.hasPriority {
+ g.passPriority()
+ }
+ })
+
+ g.stackBuffer = ui.NewBuffer(g.width-STACK_BUFFER_WIDTH, g.passButton.Y-400,
+ STACK_BUFFER_WIDTH, 400)
+ return g
+}
+
+func (g *Game) loadMap(mapName string) *Game {
+ m, err := game.GetMap(mapName)
+ if err != nil {
+ log.Fatal(err)
+ }
+ g.gameState.Map = m
+ g.mapView = ui.NewMapView(g.gameState)
+
+ storeTiles := m.FilterTiles(func(t *game.Tile) bool { return t.Type == game.TileTypes.Store })
+ g.storesOnMap = len(storeTiles) > 0
+ return g
+}
+
+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
+ 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) initPlayerUi(player *game.Player) *Game {
+ g.activePlayer = player
+ g.playerCtrl = game.NewChanPlayerControl()
+ player.Ctrl = g.playerCtrl
+ var x, y int
+
+ g.handLayer = ui.NewHandView(0,
+ g.mapView.Height(),
+ HAND_VIEW_WIDTH,
+ g.height-g.mapView.Height()-DEFAULT_BUTTON_HEIGHT,
+ player.Hand,
+ true)
+ g.addWidget(g.handLayer)
+
+ g.stateBar = ui.NewStateBar(0,
+ g.height-DEFAULT_BUTTON_HEIGHT,
+ STATE_BAR_WIDTH,
+ DEFAULT_BUTTON_HEIGHT,
+ g.gameState,
+ player)
+ g.addWidget(g.stateBar)
+
+ g.discardPileButton = ui.NewSimpleButton(g.stateBar.Width,
+ g.height-DEFAULT_BUTTON_HEIGHT,
+ 150,
+ DEFAULT_BUTTON_HEIGHT,
+ "DiscardPile",
+ func(*ui.SimpleButton) {
+ if idx := g.findWidget(g.discardPileView); idx == -1 {
+ g.discardPileView.ForceRedraw()
+ g.addWidget(g.discardPileView)
+ } else {
+ g.removeWidget(g.discardPileView)
+ }
+ })
+ g.addWidget(g.discardPileButton)
+
+ x = g.discardPileButton.X + g.discardPileButton.Width/2
+ y = g.height - DEFAULT_BUTTON_HEIGHT
+ g.discardPileView = ui.NewPocList(x, y, g.activePlayer.DiscardPile)
+
+ if !g.storesOnMap {
+ x = g.stateBar.Width + g.discardPileButton.Width
+ y = g.height - DEFAULT_BUTTON_HEIGHT
+ g.storeButton = ui.NewSimpleButton(x, y,
+ 150,
+ DEFAULT_BUTTON_HEIGHT,
+ "Store",
+ func(*ui.SimpleButton) {
+ if idx := g.findWidget(g.storeView); idx == -1 {
+ g.showStore()
+ } else {
+ g.hideStore()
+ }
+ })
+ g.addWidget(g.storeView)
+
+ x = g.storeButton.X + g.storeButton.Width/2
+ y = g.height - DEFAULT_BUTTON_HEIGHT
+ g.storeView = ui.NewPocList(x, y, g.activePlayer.Store)
+ }
+
+ return g
+}
+
+func (g *Game) showStore() {
+ g.storeView.ForceRedraw()
+ g.addWidget(g.storeView)
+}
+
+func (g *Game) hideStore() {
+ g.removeWidget(g.storeView)
+}
+
+func (g *Game) addWidget(w ui.Widget) {
+ g.widgets = append(g.widgets, w)
+}
+
+func (g *Game) findWidget(widget ui.Widget) int {
+ for i, w := range g.widgets {
+ if w != widget {
+ continue
+ }
+ return i
+ }
+ return -1
+}
+
+func (g *Game) removeWidget(widget ui.Widget) {
+ idx := g.findWidget(widget)
+ if idx != -1 {
+ g.removeWidgetAt(idx)
+ }
+}
+
+func (g *Game) removeWidgetAt(idx int) {
+ nwidgets := len(g.widgets)
+ g.widgets[idx] = g.widgets[nwidgets-1]
+ g.widgets = g.widgets[:nwidgets-1]
+}
+
+func (g *Game) addActionChoice(perm game.Permanent, x, y int) {
+ actions := perm.AvailActions()
+ if len(actions) > 0 {
+ g.selectedObject = perm
+ onClick := func(c *ui.Choice, x, y int) {
+ g.removeActionChoice()
+ a := actions[c.GetChoosen(x, y)]
+ g.selectedObject = a
+ }
+ g.actionChoice = ui.NewActionChoice(x, y, actions, onClick)
+ g.addWidget(g.actionChoice)
+ }
+}
+
+func (g *Game) removeActionChoice() {
+ g.removeWidget(g.actionChoice)
+ g.actionChoice = nil
+}
+
+func (g *Game) AddHighlight(obj interface{}) {
+ switch obj := obj.(type) {
+ case ui.HandCard:
+ g.handLayer.AddHighlightCard(obj.C)
+ case *game.Tile:
+ g.mapView.AddHighlightTile(obj)
+ }
+}
+
+func (g *Game) clearMapHighlights() {
+ g.mapView.ClearPermanentsHighlights()
+ g.mapView.ClearTileHighlights()
+ g.mapView.ForceRedraw()
+}
+
+func (g *Game) declareAction(a game.Action) {
+ g.playerCtrl.SendAction(a)
+ g.clearMapHighlights()
+ g.handLayer.ClearHighlights()
+ g.hasPriority = false
+ g.hidePassButton()
+ g.stateBar.ForceRedraw()
+}
+
+func (g *Game) showPassButton() {
+ g.addWidget(g.passButton)
+}
+
+func (g *Game) hidePassButton() {
+ g.removeWidget(g.passButton)
+}
+
+func (g *Game) passPriority() {
+ g.declareAction(game.NewPassPriority(g.activePlayer))
+}
+
+func (g *Game) handlePlayerNotifications() {
+ n := g.playerCtrl.RecvNotification()
+ for n != nil {
+ switch n.Notification {
+ case game.PriorityNotification:
+ g.showPassButton()
+ g.hasPriority = true
+
+ case game.DeclaredActionNotification:
+ if n.Error != nil {
+ log.Fatal(n.Error)
+ }
+ a := n.Context.(game.Action)
+ g.stackBuffer.AddLine(a.String())
+
+ case game.ResolvedActionNotification:
+ g.clearMapHighlights()
+ g.stackBuffer.RemoveLast()
+ g.mapView.ForceRedraw()
+
+ case game.BuyPrompt:
+ if !g.storesOnMap {
+ g.showStore()
+ }
+
+ case game.HandCardSelectionPrompt:
+ ctx := n.Context.([]int)
+ min, max := ctx[0], ctx[1]
+ promptTxt := fmt.Sprintf("Select between %d and %d hand cards", min, max)
+ g.prompt = ui.NewHandCardPrompt(g.height/2, g.width, promptTxt)
+ g.addWidget(g.prompt)
+ g.showPassButton()
+ }
+
+ g.stateBar.ForceRedraw()
+ g.handLayer.ForceRedraw()
+
+ n = g.playerCtrl.RecvNotification()
+ }
+}
+
+func (g *Game) findObjectAt(x, y int) interface{} {
+ // Iterate the widget in reverse order to ensure that objects are found
+ // in the newer widget possibly drawn over older ones
+ for i := len(g.widgets) - 1; i >= 0; i-- {
+ w := g.widgets[i]
+ if obj := w.FindObjectAt(x, y); obj != nil {
+ return obj
+ }
+ }
+ if obj := g.mapView.FindObjectAt(x, y); obj != nil {
+ return obj
+ }
+
+ return nil
+}
+
+func (g *Game) handleTargetSelection(obj interface{}) {
+ log.Println("Handle target selection")
+ a := g.selectedObject.(game.Action)
+ a.Targets().AddTarget(obj)
+
+ if !a.Targets().RequireTargets() {
+ log.Println("declare", a)
+ g.declareAction(a)
+ g.selectedObject = nil
+ }
+}
+
+func (g *Game) handleSelection(obj interface{}, x, y int) {
+ if obj == nil {
+ fmt.Printf("No object found at cursor position (%d, %d)\n", x, y)
+ return
+ }
+
+ if _, ok := g.selectedObject.(game.Action); ok {
+ g.handleTargetSelection(obj)
+ } else {
+ switch obj := obj.(type) {
+ case ui.Button:
+ obj.Click(x, y)
+
+ case *game.Tile:
+ g.mapView.HighlightTile(obj)
+
+ case game.Permanent:
+ perm := obj
+ g.addActionChoice(perm, x, y)
+
+ case ui.HandCard:
+ g.selectedObject = game.NewPlayAction(g.activePlayer, obj.C)
+ g.handLayer.HighlightCard(obj.C)
+
+ case *game.Card:
+ if g.gameState.IsActivePlayer(g.activePlayer) &&
+ g.gameState.ActivePhase == game.Phases.BuyPhase {
+ g.declareAction(game.NewBuyAction(g.activePlayer, obj))
+ }
+
+ default:
+ log.Fatalf("Object of type %T not handled", obj)
+ }
+ }
+}
+
+func (g *Game) highlightTargets() {
+ a, ok := g.selectedObject.(game.Action)
+ if !ok {
+ return
+ }
+
+ options := a.Targets().NextOptions()
+ for _, option := range options {
+ g.AddHighlight(option)
+ }
+}
+
+func (g *Game) handleHover(obj interface{}, x, y int) {
+ var hoverHint ui.Widget
+ switch obj := obj.(type) {
+ case *game.Card:
+ wx, wy := x, y
+ if x+500 > g.width || y+700 > g.height {
+ wx = x - 500
+ wy = y - 700
+ }
+ hoverHint = ui.NewScaledCardView(wx, wy, 500, 700, obj.Path())
+ case *game.Unit:
+ hoverHint = ui.NewUnitInfo(x+ui.TILE_WIDTH, y, obj)
+ case game.Permanent:
+ hoverHint = ui.NewPermInfo(x+ui.TILE_WIDTH, y, obj)
+ case *game.Tile:
+ if g.storesOnMap && obj.Type == game.TileTypes.Store {
+ poc := g.gameState.Map.StoreOn(obj.Position)
+ hoverHint = ui.NewPocList(x, y, poc)
+ // Set a special hoverDetector resetFunc keeping the hovering store
+ // around and allow card selection until the cursor leaves the store widget.
+ g.hoverDetector.resetFunc = func() bool {
+ x, y := ebiten.CursorPosition()
+ if hoverHint.Contains(x, y) {
+ return false
+ }
+ g.removeWidget(hoverHint)
+ return true
+ }
+ }
+ }
+
+ if hoverHint != nil {
+ g.addWidget(hoverHint)
+ // Set the default reset function removing the hoverHint
+ if g.hoverDetector.resetFunc == nil {
+ g.hoverDetector.resetFunc = func() bool {
+ g.removeWidget(hoverHint)
+ return true
+ }
+ }
+ }
+}
+
+func (g *Game) Update() error {
+ g.handlePlayerNotifications()
+
+ x, y := ebiten.CursorPosition()
+ g.hoverDetector.update(x, y)
+
+ obj := g.findObjectAt(x, y)
+
+ scrollX, scrollY := ebiten.Wheel()
+ if scrollX != 0 || scrollY != 0 {
+ if scrollable, ok := obj.(ui.Scrollable); ok {
+ scrollable.Scroll(int(scrollX), int(scrollY))
+ }
+ }
+
+ g.highlightTargets()
+
+ if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
+ if g.prompt != nil {
+ g.prompt.Add(obj)
+ g.AddHighlight(obj)
+ } else {
+ g.handleSelection(obj, x, y)
+ }
+ } else if g.hoverDetector.startedHovering() && g.selectedObject == nil && obj != nil {
+ g.handleHover(obj, x, y)
+ } else if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
+ g.passButton.Click(0, 0)
+ } else if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
+ g.removeActionChoice()
+ g.selectedObject = nil
+ g.hoverDetector.reset()
+ g.clearMapHighlights()
+ g.handLayer.ClearHighlights()
+ }
+ return nil
+}
+
+func (g *Game) Draw(screen *ebiten.Image) {
+ g.mapView.Draw(screen)
+ if !g.gameState.Stack.IsEmpty() {
+ g.stackBuffer.Draw(screen)
+ }
+ for _, b := range g.widgets {
+ b.Draw(screen)
+ }
+ ebitenutil.DebugPrint(screen,
+ fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
+}
+
+func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
+ return g.width, g.height
+}
+
+func (g *Game) Start() *Game {
+ go func() {
+ w := g.gameState.Loop()
+ log.Println("Winners are", w)
+ }()
+
+ return g
+}
diff --git a/go/client/main.go b/go/client/main.go
new file mode 100644
index 00000000..3635404f
--- /dev/null
+++ b/go/client/main.go
@@ -0,0 +1,95 @@
+package main
+
+import (
+ "flag"
+ "image/color"
+ "io"
+ "log"
+ "os"
+
+ "github.com/hajimehoshi/ebiten/v2"
+
+ "muhq.space/muhqs-game/go/font"
+)
+
+const (
+ WINDOW_WIDTH = 800
+ WINDOW_HEIGHT = 1024
+)
+
+var playerColors = []color.RGBA{
+ {1, 0xff, 1, 0xff},
+ {1, 1, 0xff, 0xff},
+}
+
+var (
+ playerName string
+ startDeckPath string
+)
+
+type app struct {
+ windowWidth int
+ windowHeight int
+
+ active ebiten.Game
+ running bool
+}
+
+func (a *app) Update() error {
+ return a.active.Update()
+}
+
+func (a *app) Draw(screen *ebiten.Image) {
+ a.active.Draw(screen)
+}
+
+func (a *app) Layout(outsideWidth, outsideHeight int) (int, int) {
+ return a.windowWidth, a.windowHeight
+}
+
+func main() {
+ font.InitFont()
+
+ app := &app{}
+ startMenu := NewStartMenu(app)
+
+ flag.IntVar(&app.windowWidth, "windowWidth", WINDOW_WIDTH, "the window width")
+ flag.IntVar(&app.windowHeight, "windowHeight", WINDOW_HEIGHT, "the window height")
+
+ flag.StringVar(&startMenu.playerName, "player", "muhq", "player name")
+ flag.StringVar(&startDeckPath, "deck", "", "deck list to load")
+
+ flag.StringVar(&startMenu.mapPath, "map", "the-kraken", "the map to load")
+ flag.StringVar(&startMenu.remote, "remote", "", "address of remote game to connect to")
+ flag.Parse()
+
+ ebiten.SetWindowSize(app.windowWidth, app.windowHeight)
+ ebiten.SetWindowTitle("Muhq's Game")
+
+ startMenu.width, startMenu.height = app.windowWidth, app.windowHeight
+
+ if startDeckPath != "" {
+ file, err := os.Open(startDeckPath)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ defer file.Close()
+
+ deckList, err := io.ReadAll(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ startMenu.startDeck = string(deckList)
+ } else {
+ startMenu.startDeck = `1 nautics/barge
+1 nautics/captain
+1 nautics/fisher
+1 nautics/galley`
+ }
+
+ app.active = startMenu
+ if err := ebiten.RunGame(app); err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/go/client/startMenu.go b/go/client/startMenu.go
new file mode 100644
index 00000000..735753b0
--- /dev/null
+++ b/go/client/startMenu.go
@@ -0,0 +1,92 @@
+package main
+
+import (
+ "log"
+
+ "github.com/hajimehoshi/ebiten/v2"
+ "github.com/hajimehoshi/ebiten/v2/inpututil"
+ // "muhq.space/muhqs-game/go/game"
+ "muhq.space/muhqs-game/go/ui"
+)
+
+const (
+ START_BUTTON_WIDTH = 150
+ START_BUTTON_HEIGHT = 50
+)
+
+type StartMenu struct {
+ app *app
+ width, height int
+
+ playerName string
+ startDeck string
+ remote string
+ mapPath string
+ widgets []ui.Widget
+}
+
+func NewStartMenu(app *app) *StartMenu {
+ m := &StartMenu{app: app}
+
+ return m
+}
+
+func (m *StartMenu) build() {
+ m.widgets = append(m.widgets, ui.NewSimpleButton(
+ (m.width-START_BUTTON_WIDTH)/2,
+ (m.height-START_BUTTON_HEIGHT)/2,
+ 150,
+ 50,
+ "Start",
+ func(*ui.SimpleButton) {
+ m.app.active = m.next()
+ }))
+}
+
+func (m *StartMenu) Update() error {
+ if m.widgets == nil {
+ m.build()
+ }
+
+ x, y := ebiten.CursorPosition()
+ if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
+ for _, w := range m.widgets {
+ if !w.Contains(x, y) {
+ continue
+ }
+
+ if btn, ok := w.(ui.Button); ok {
+ btn.Click(x, y)
+ }
+ }
+ }
+
+ return nil
+}
+
+func (m *StartMenu) Draw(screen *ebiten.Image) {
+ for _, w := range m.widgets {
+ w.Draw(screen)
+ }
+}
+
+func (m *StartMenu) Layout(width, height int) (int, int) {
+ return m.width, m.height
+}
+
+func (m *StartMenu) next() ebiten.Game {
+ if m.remote != "" {
+ log.Fatal("Remote games are currently not implemented")
+ }
+
+ g := newGame(m.app).loadMap(m.mapPath).addActivePlayer(m.playerName, m.startDeck)
+
+ if m.mapPath == "the-kraken" {
+ idx := len(g.gameState.Players)
+ color := playerColors[idx]
+ g.gameState.AddKraken()
+ g.gameState.Players[idx].Color = color
+ }
+
+ return g.Start()
+}