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

import (
	"muhq.space/muhqs-game/go/activities"
	"muhq.space/muhqs-game/go/game"
	"muhq.space/muhqs-game/go/ui"
)

type challenges struct {
	ui.Collection
	app *app

	playerName string
}

func NewChallengesMenu(app *app, playerName string) *challenges {
	m := &challenges{app: app, playerName: playerName}
	m.Width = app.windowWidth
	m.Height = app.windowHeight

	return m
}

var availChallenges map[string]func(string) *game.LocalState

func init() {
	availChallenges = make(map[string]func(string) *game.LocalState)
	availChallenges["Missionary"] = game.NewMissionaryChallenge
	availChallenges["Cav. Archer"] = game.NewCavArcherChallenge
	availChallenges["Random"] = game.NewRandomChallenge
}

func (m *challenges) build() {
	i := 0
	for ch, f := range availChallenges {

		m.AddWidget(ui.NewSimpleButton(
			(m.Width-START_BUTTON_WIDTH)/2,
			i*START_BUTTON_HEIGHT+50+i*25,
			START_BUTTON_WIDTH,
			START_BUTTON_HEIGHT,
			ch,
			func(*ui.SimpleButton) {
				s := f(m.playerName)
				m.startChallenge(s)
			}))
		i = i + 1
	}
}

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

	if err := ui.Update(); err != nil {
		return err
	}

	return m.Collection.Update()
}

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

func (m *challenges) startChallenge(state game.State) {
	g := newGame(m.app, state)

	g.initMapUi()
	p := state.PlayerByName(m.playerName)
	g.activePlayerId = p.Id
	g.initPlayerUi(p)

	activities.PushActivity(g.Start())
}