diff options
Diffstat (limited to 'go/client')
| -rw-r--r-- | go/client/draft.go | 199 | ||||
| -rw-r--r-- | go/client/startMenu.go | 11 |
2 files changed, 210 insertions, 0 deletions
diff --git a/go/client/draft.go b/go/client/draft.go new file mode 100644 index 00000000..3950094f --- /dev/null +++ b/go/client/draft.go @@ -0,0 +1,199 @@ +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" +) + +type Draft struct { + ui.Collection + app *app + player *game.Player + ctrl *game.ChanPlayerControl + draft *game.Draft + + setInput *ui.TextInput + descInput *ui.TextInput + deckList *ui.PocList +} + +func NewDraft(width, height int, app *app) *Draft { + d := &Draft{ + app: app, + Collection: ui.Collection{Width: width, Height: height}, + } + + d.AddWidget(ui.NewFixedTextBox( + (d.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH, + (d.Height-START_BUTTON_HEIGHT)/2-3*START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "sets:", + ).Centering(true)) + + d.setInput = ui.NewTextInput( + (d.Width-START_BUTTON_WIDTH)/2, + (d.Height-START_BUTTON_HEIGHT)/2-3*START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "base,magic,equipments", + ) + d.AddWidget(d.setInput) + + d.AddWidget(ui.NewFixedTextBox( + (d.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH, + (d.Height-START_BUTTON_HEIGHT)/2-2*START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "draft:", + ).Centering(true)) + + d.descInput = ui.NewTextInput( + (d.Width-START_BUTTON_WIDTH)/2, + (d.Height-START_BUTTON_HEIGHT)/2-2*START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "3x[2;8]", + ) + d.AddWidget(d.descInput) + + d.AddWidget(ui.NewFixedTextBox( + (d.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH, + (d.Height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "AI:", + ).Centering(true)) + + aiNInput := ui.NewNumberChoice( + (d.Width-START_BUTTON_WIDTH)/2, + (d.Height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT, + 1, + nil, + ) + d.AddWidget(aiNInput) + + d.AddWidget(ui.NewSimpleButton( + (d.Width-START_BUTTON_WIDTH)/2, + (d.Height-START_BUTTON_HEIGHT)/2, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "Start", + func(*ui.SimpleButton) { + d.startDraft(d.descInput.TextOrLabel(), d.setInput.TextOrLabel(), aiNInput.GetChoosen()) + })) + + startMenu := d.app.activities[len(d.app.activities)-1].(*StartMenu) + d.player = game.NewDraftPlayer(startMenu.playerName) + d.ctrl = game.NewChanPlayerControl(d.player) + d.player.Ctrl = d.ctrl + + return d +} + +func (d *Draft) Update() error { + if n := d.ctrl.RecvNotification(); n != nil { + log.Println("Received draft pick: %v\n", n) + d.presentPack(n) + } + + if err := ui.TouchManager.Update(); err != nil { + return err + } + + if err := d.Collection.Update(); err != nil { + return err + } + + if inpututil.IsKeyJustPressed(ebiten.KeyEscape) { + d.app.popActivity() + } + + return nil +} + +func (d *Draft) presentPack(n *game.PlayerNotification) { + pack := n.Context.(game.PileOfCards) + grid := ui.NewCardGrid(0, 0, d.Width-SEALED_DECK_STRIP_WIDTH, d.Height, 0.5, pack).Columns(3) + selectCard := func(x, y int) { + _card := grid.FindObjectAt(x, y) + if _card == nil { + return + } + + card := _card.(*game.Card) + d.RemoveWidget(grid) + d.ctrl.SendAction(game.NewDraftPick(d.player, pack, card)) + } + grid.RegisterHandler("click", selectCard) + d.AddWidget(grid) +} + +func (d *Draft) Layout(width, height int) (int, int) { + return d.Width, d.Height +} + +func (d *Draft) startDraft(desc string, setList string, aiN int) { + + log.Printf("starting %s draft from '%s'\n", desc, setList) + + var sets []game.SetIdentifier + var err error + if sets, err = game.SetListToSets(setList); err != nil { + d.setInput.Bg(ui.WarningBg) + d.setInput.SetInput("") + return + } + + d.draft = game.NewDraft([]*game.Player{d.player}, desc, sets) + + log.Println(aiN) + for _ = range aiN { + log.Println("add ai") + d.draft.AddRandomAi() + } + + if !d.draft.Valid() { + d.descInput.Bg(ui.WarningBg) + d.descInput.SetInput("") + return + } + d.Clear() + + d.deckList = ui.NewPocList(d.Width-SEALED_DECK_STRIP_WIDTH/2, d.Height-START_BUTTON_HEIGHT-20, d.player.Deck) + d.deckList.Bg(ui.Gray) + { + cardsInDeck := 0 + d.deckList.RegisterHandler("update", func() error { + if n := d.player.Deck.Size(); n > cardsInDeck { + d.deckList.ForceRedraw() + cardsInDeck = n + } + return nil + }) + } + d.AddWidget(d.deckList) + + d.AddWidget(ui.NewSimpleButton( + d.Width-SEALED_DECK_STRIP_WIDTH/2-START_BUTTON_WIDTH/2, + d.Height-START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, + "Done", + func(*ui.SimpleButton) { + d.done() + })) + + go d.draft.Run() +} + +func (d *Draft) done() { + startMenu := d.app.activities[len(d.app.activities)-2].(*StartMenu) + startMenu.deckInput.SetInput(d.player.Deck.ToList()) + d.app.popActivity() +} diff --git a/go/client/startMenu.go b/go/client/startMenu.go index 04b1ed31..10fa8e1f 100644 --- a/go/client/startMenu.go +++ b/go/client/startMenu.go @@ -110,6 +110,17 @@ func (m *StartMenu) build() { deckInput.Y+deckInput.Height, START_BUTTON_WIDTH, START_BUTTON_HEIGHT, + "Draft", + func(*ui.SimpleButton) { + draft := NewDraft(m.Width, m.Height, m.app) + m.app.pushActivity(draft) + })) + + m.AddWidget(ui.NewSimpleButton( + (m.Width-START_BUTTON_WIDTH)/2, + deckInput.Y+deckInput.Height+2*START_BUTTON_HEIGHT, + START_BUTTON_WIDTH, + START_BUTTON_HEIGHT, "Start", func(*ui.SimpleButton) { m.playerName = playerInput.Text() |
