aboutsummaryrefslogtreecommitdiff
path: root/go/client/sealed.go
blob: c219dbf6f013034a328707630b0e056211e80894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"strings"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"

	"muhq.space/muhqs-game/go/game"
	"muhq.space/muhqs-game/go/ui"
)

const (
	SIDEBARD_WIDTH = 200
)

type Sealed struct {
	ui.Collection
	app *app

	pool game.PileOfCards
	deck game.PileOfCards
}

func NewSealed(width, height int, app *app) *Sealed {
	s := &Sealed{
		app:        app,
		Collection: ui.Collection{Width: width, Height: height},
	}

	s.AddWidget(ui.NewFixedTextBox(
		(s.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH,
		(s.Height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"sets:",
	).Centering(true))

	setInput := ui.NewTextInput(
		(s.Width-START_BUTTON_WIDTH)/2,
		(s.Height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"set list",
	)
	s.AddWidget(setInput)

	s.AddWidget(ui.NewSimpleButton(
		(s.Width-START_BUTTON_WIDTH)/2,
		(s.Height-START_BUTTON_HEIGHT)/2,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"Start",
		func(*ui.SimpleButton) {
			s.startSealed(setInput.Text())
		}))

	return s
}

func (s *Sealed) Update() error {
	if err := s.Collection.Update(); err != nil {
		return err
	}

	if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
		s.app.popActivity()
	}

	return nil
}

func (s *Sealed) Layout(width, height int) (int, int) {
	return s.Width, s.Height
}

func (s *Sealed) startSealed(_sets string) {
	s.pool = game.NewPileOfCards()
	s.deck = game.NewPileOfCards()

	s.Clear()

	sets := strings.Split(_sets, ",")
	for _, setName := range sets {
		set := game.SetNames[setName]
		setDeck := game.NewDeck()
		for _, c := range game.NewDeckFromCardPaths(set.CardPaths()).Cards() {
			if c.IsBuyable() {
				setDeck.AddCard(c)
			}
		}
		setDeck.MoveInto(s.pool)
	}

	deckList := ui.NewPocList(s.Width-SIDEBARD_WIDTH/2, s.Height-START_BUTTON_HEIGHT-20, s.deck)
	deckList.Bg(ui.Gray)
	s.AddWidget(deckList)

	grid := ui.NewCardGrid(0, 0, s.Width-SIDEBARD_WIDTH, s.Height, 0.5, s.pool).Columns(3)
	selectCard := func(x, y int) {
		_card := grid.FindObjectAt(x, y)
		if _card == nil {
			return
		}

		card := _card.(*game.Card)
		s.pool.MoveCard(card, s.deck)
		grid.ForceRedraw()
		deckList.ForceRedraw()
	}
	s.AddWidget(ui.NewClickWrapper(grid, selectCard))

	s.AddWidget(ui.NewSimpleButton(
		s.Width-SIDEBARD_WIDTH/2-START_BUTTON_WIDTH/2,
		s.Height-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"Done",
		func(*ui.SimpleButton) {
			s.done()
		}))
}

func (s *Sealed) done() {
	startMenu := s.app.activities[len(s.app.activities)-2].(*StartMenu)
	startMenu.deckInput.SetInput(s.deck.ToList())
	s.app.popActivity()
}