aboutsummaryrefslogtreecommitdiff
path: root/go/client/startMenu.go
blob: 234a1570398b607ceab5687ef71fec06b1c54126 (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
package main

import (
	"image/color"
	"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() {
	playerLabel := ui.NewCenteringFixedTextBox(
		(m.width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH,
		(m.height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"name:",
	)
	playerLabel.Bg = color.Black
	m.widgets = append(m.widgets, playerLabel)

	playerInput := ui.NewTextInput(
		(m.width-START_BUTTON_WIDTH)/2,
		(m.height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"player name",
	)

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

	m.widgets = append(m.widgets, playerInput)

	m.widgets = append(m.widgets, ui.NewSimpleButton(
		(m.width-START_BUTTON_WIDTH)/2,
		(m.height-START_BUTTON_HEIGHT)/2,
		START_BUTTON_WIDTH,
		START_BUTTON_HEIGHT,
		"Start",
		func(*ui.SimpleButton) {
			m.playerName = playerInput.Text()
			m.app.active = m.next()
		}))
}

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

	x, y := ebiten.CursorPosition()

	var activeWidget ui.Widget
	for _, w := range m.widgets {
		if !w.Contains(x, y) {
			continue
		}
		activeWidget = w
		break
	}

	if activeWidget == nil {
		return nil
	}

	scrollX, scrollY := ebiten.Wheel()
	if scrollX != 0 || scrollY != 0 {
		if scrollable, ok := activeWidget.(ui.Scrollable); ok {
			scrollable.Scroll(int(scrollX), int(scrollY))
		}
	}

	if textInput, ok := activeWidget.(*ui.TextInput); ok {
		// var input []rune
		// ebiten.AppendInputChars(input)
		input := ebiten.InputChars()
		if len(input) > 0 {
			textInput.AddInput(input)
		}
		textInput.HandleKey()
	}

	if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		if btn, ok := activeWidget.(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()
}