aboutsummaryrefslogtreecommitdiff
path: root/go/client/startMenu.go
blob: 0fc7cbe7a130820ff4be303db6c8508298f2281f (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

import (
	"log"

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

const (
	START_BUTTON_WIDTH  = 150
	START_BUTTON_HEIGHT = 50

	DECK_LIST_WIDTH  = 300
	DECK_LIST_HEIGHT = 500
)

type StartMenu struct {
	ui.Collection
	app *app

	deckInput *ui.TextInput

	playerName string
	startDeck  string
	remote     string
	mapPath    string
}

func NewStartMenu(app *app) *StartMenu {
	m := &StartMenu{app: app}

	return m
}

func (m *StartMenu) build() {
	deckInput := ui.NewTextInput(
		(m.Width-DECK_LIST_WIDTH)/2,
		(m.Height-DECK_LIST_HEIGHT)/2,
		DECK_LIST_WIDTH,
		DECK_LIST_HEIGHT,
		"deck",
	)
	deckInput.Bg(ui.Gray)

	if m.startDeck != "" {
		deckInput.AddInput([]rune(m.startDeck))
	}

	m.deckInput = deckInput
	m.AddWidget(deckInput)

	m.AddWidget(ui.NewFixedTextBox(
		(m.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH,
		deckInput.Y-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"map:",
	).Centering(true))

	mapInput := ui.NewTextInput(
		(m.Width-START_BUTTON_WIDTH)/2,
		deckInput.Y-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"map",
	)

	if m.mapPath != "" {
		mapInput.AddInput([]rune(m.mapPath))
	}

	m.AddWidget(mapInput)

	m.AddWidget(ui.NewFixedTextBox(
		(m.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH,
		mapInput.Y-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"name:",
	).Centering(true))

	playerInput := ui.NewTextInput(
		(m.Width-START_BUTTON_WIDTH)/2,
		mapInput.Y-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"player name",
	)

	if m.playerName != "" {
		playerInput.AddInput([]rune(m.playerName))
	}

	m.AddWidget(playerInput)

	m.AddWidget(ui.NewSimpleButton(
		(m.Width-START_BUTTON_WIDTH)/2+START_BUTTON_WIDTH/2,
		deckInput.Y+deckInput.Height,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"Sealed",
		func(*ui.SimpleButton) {
			sealed := NewSealed(m.Width, m.Height, m.app)
			m.app.pushActivity(sealed)
		}))

	m.AddWidget(ui.NewSimpleButton(
		(m.Width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH/2,
		deckInput.Y+deckInput.Height,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"Start",
		func(*ui.SimpleButton) {
			m.playerName = playerInput.Text()
			m.mapPath = mapInput.Text()
			m.startDeck = deckInput.Text()
			m.startGame()
		}))
}

func (m *StartMenu) Update() error {
	if m.Widgets() == nil {
		m.build()
	}

	return m.Collection.Update()
}

func (m *StartMenu) Layout(width, height int) (int, int) {
	return m.Width, m.Height
}

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, state).loadMap(m.mapPath).addActivePlayer(m.playerName, m.startDeck)

	if m.mapPath == "the-kraken" {
		idx := len(g.gameState.Players())
		color := playerColors[idx]
		g.gameState.AddNewAiPlayer("kraken", color)
	}

	m.app.pushActivity(g.Start())
}