~whereswaldon/pointstar

ref: d4484fe328f8a5dae55f0281406786b1c271700f pointstar/skin.go -rw-r--r-- 4.3 KiB
d4484fe3Chris Waldon feat: AI players can play one another over websockets 1 year, 10 months ago
                                                                                
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
152
153
154
155
package main

import (
	"fmt"
	"image/color"

	"gioui.org/layout"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"git.sr.ht/~whereswaldon/pointstar/gamestate"
)

// Skin provides methods for rendering the game UI
type Skin struct {
	*material.Theme
}

// LayoutTable renders the entire game interface into the provided context
func (s *Skin) LayoutTable(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	for i := range u.PlayButtons[:] {
		clicked := false
		for u.PlayButtons[i].Clicked(gtx) {
			clicked = true
		}
		if clicked {
			g.Players[0].PlayCardAt(i)
			g.Advance()
		}
	}
	passed := false
	for u.PassButton.Clicked(gtx) {
		passed = true
	}
	if passed {
		g.Players[0].IsPassing = true
		g.Advance()
	}
	var children []layout.FlexChild
	// populate the slice with functions to lay out each player's hand
	for i := range g.Players {
		player := g.Players[i]
		children = append(children, layout.Rigid(func() {
			s.LayoutPlayed(player, gtx)
		}))
		children = append(children, layout.Flexed(.25, func() {}))
	}
	children = append(children, layout.Flexed(1, func() {}))
	children = append(children, layout.Rigid(func() {
		s.LayoutHand(g.Players[0], u.PlayButtons[:], &u.PassButton, gtx)
	}))
	// lay out all of the rows
	layout.Flex{Axis: layout.Vertical}.Layout(gtx, children...)
}

// LayoutPlayed arranges a row of information about a player and the cards
// that they have played during the current battle
func (s *Skin) LayoutPlayed(player gamestate.Player, gtx *layout.Context) {
	var children []layout.FlexChild
	// prepend player info
	hand := player.Played
	children = append(children, layout.Rigid(func() {
		layout.Flex{Axis: layout.Vertical}.Layout(gtx,
			layout.Rigid(func() {
				s.Theme.H3(player.Name).Layout(gtx)
			}),
			layout.Rigid(func() {
				format := "Strength: %d"
				if player.IsPassing {
					format += " Passed"
				}
				s.Theme.H5(fmt.Sprintf(format, hand.Score())).Layout(gtx)
			}),
			layout.Rigid(func() {
				s.Theme.H6(fmt.Sprintf("Victories: %d", player.Victories)).Layout(gtx)
			}),
			layout.Rigid(func() {
				s.Theme.H6(fmt.Sprintf("Hand: %d", len(player.Hand))).Layout(gtx)
			}),
		)
	}))
	children = append(children, layout.Flexed(.5, func() {}))
	children = append(children, s.CardListAsFlexChildren(hand, gtx)...)
	children = append(children, layout.Flexed(.5, func() {}))
	children = append(children, layout.Rigid(func() {
	}))
	layout.Flex{Axis: layout.Horizontal}.Layout(gtx, children...)
}

// LayoutHand lays out the cards in a player's hand with buttons to play each card
// and to pass the turn.
func (s *Skin) LayoutHand(player gamestate.Player, playButtons []widget.Button, passButton *widget.Button, gtx *layout.Context) {
	var children []layout.FlexChild
	hand := player.Hand
	children = append(children, layout.Flexed(.5, func() {}))
	for i := range hand {
		card := hand[i]
		button := &playButtons[i]
		children = append(children, layout.Rigid(func() {
			layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
				layout.Flex{Axis: layout.Vertical}.Layout(gtx,
					layout.Rigid(func() {
						layout.Inset{Bottom: unit.Dp(8)}.Layout(gtx, func() {
							s.LayoutCard(card, gtx)
						})
					}),
					layout.Rigid(func() {
						if !player.IsPassing {
							s.Theme.Button("Play").Layout(gtx, button)
						}
					}),
				)
			})
		}))
	}
	children = append(children, layout.Rigid(func() {
		layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
			s.Theme.Button("Pass").Layout(gtx, passButton)
		})
	}))
	children = append(children, layout.Flexed(.5, func() {}))
	layout.Flex{Axis: layout.Horizontal}.Layout(gtx, children...)
}

func (s *Skin) LayoutCard(c gamestate.Card, gtx *layout.Context) {
	textColor := color.RGBA{A: 255}
	if gamestate.CardTypes[c] == gamestate.Special {
		textColor = color.RGBA{R: 155, A: 255}
	}
	layout.Flex{Axis: layout.Vertical}.Layout(gtx,
		layout.Rigid(func() {
			h := s.Theme.H2(gamestate.CardText[c])
			h.Color = textColor
			h.Layout(gtx)
		}),
		layout.Rigid(func() {
			b := s.Theme.Body2(gamestate.CardNames[c])
			b.Color = textColor
			b.Layout(gtx)
		}),
	)
}

func (s *Skin) CardListAsFlexChildren(h gamestate.CardList, gtx *layout.Context) []layout.FlexChild {
	var children []layout.FlexChild
	for i := range h {
		card := h[i]
		children = append(children, layout.Rigid(func() {
			layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
				s.LayoutCard(card, gtx)
			})
		}))
	}
	return children
}