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

import (
	"github.com/hajimehoshi/ebiten/v2"
	"muhq.space/muhqs-game/go/activities"
	"muhq.space/muhqs-game/go/game"
)

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

type challenges struct {
	bl  *activities.ButtonList
	app *app

	playerName string
}

func newChallengesMenu(app *app, playerName string) *challenges {
	var m *challenges
	labels := []string{}
	handlers := []func(){}
	for k, start := range availChallenges {
		labels = append(labels, k)
		handlers = append(handlers, func() {
			s := start(m.playerName)
			m.startChallenge(s)
		})
	}
	m = &challenges{
		bl:         activities.NewButtonList(app.windowWidth, app.windowHeight, labels, handlers),
		app:        app,
		playerName: playerName,
	}

	return m
}

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())
}

func (c *challenges) Draw(screen *ebiten.Image)  { c.bl.Draw(screen) }
func (c *challenges) Layout(w, h int) (int, int) { return c.bl.Layout(w, h) }
func (c *challenges) Update() error              { return c.bl.Update() }