Files
8bitMyGoUI/tabletop/roulette.go
T
ZacharyZhang-NY 81003b21ee Add tabletop components 486-500 and ten game icons
Fifteen pieces of a games night: Dice, DiceTray, PlayingCard, CardHand,
Deck, Chessboard, GoBoard, MahjongTile, Domino, Roulette, CoinFlip,
BingoCard, TicTacToe, ScoreTrack and Meeple. Component number 500 is
a meeple, which seems right.

Dice tumble, coins turn and wheels spin on a cue, a frame at a time,
and land at once under Reduce Motion. Hands lift the card you point
at and play the one drawn on top; pieces and stones pick the lighter
of Lit and Ink for White, so they read in every palette; the computer
plays a fair O; FREE is a star in every language.

New 12x12 icons: Spade, Club, Diamond, Meeple, King, Queen, Rook,
Bishop, Knight and Pawn.
2026-10-12 14:03:58 -04:00

62 lines
1.9 KiB
Go

package tabletop
import (
"fmt"
"math"
"slices"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RouletteOptions struct {
// Cue spins it: each new cue lands it on result.
Cue int
}
const wheel = 28
// Roulette spins its wheel, on a new Cue, to result.
func Roulette(c *ui.Context, text string, segments []string, result int, o RouletteOptions) *ui.Element {
k := pixel.K(c)
n := len(segments)
if n < 2 || n > 64 || result < 0 || result >= n || slices.Contains(segments, "") {
panic(fmt.Sprintf("tabletop: Roulette lands on %d of %d segments, or one is blank", result, n))
}
e := ui.Column(c).Gap(k.D(2)).AlignItems(ui.Center).AlignSelf(ui.Start).Role(ui.RoleGroup).Label(text).Description(segments[result])
// A spin: a lap plus twelve, a segment a frame.
total := n + 12
f := k.Shot(c, e, o.Cue, 15, total)
under := ((result-(total-f))%n + n) % n
return e.Children(func() {
ui.Box(c).Size(k.D(2*wheel+1), k.D(2*wheel+6)).Role(ui.RoleNone).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
cx, cy, step := wheel, wheel+5, 2*math.Pi/float64(n)
for dy := -wheel; dy <= wheel; dy++ {
for dx := -wheel; dx <= wheel; dx++ {
if dx*dx+dy*dy > wheel*wheel {
continue
}
// Wedges run clockwise from the top; the pointer reads one.
t := math.Mod(math.Atan2(float64(dx), float64(-dy))+step/2+2*math.Pi, 2*math.Pi)
i := (int(t/step)%n + under) % n
ink := []pixel.Slot{pixel.Bad, pixel.Ink}[i%2]
if i == 0 {
ink = pixel.Good
}
g.Dot(cx+dx, cy+dy, ink)
}
}
g.Circle(cx, cy, wheel, pixel.Edge)
g.Disc(cx, cy, 4, pixel.Warn)
g.Circle(cx, cy, 4, pixel.Edge)
// The pointer hangs over the rim at twelve o'clock.
for j := range 4 {
g.HLine(cx-3+j, j, 7-2*j, pixel.Warn)
}
})
pixel.Text(c, segments[under], pixel.TextOptions{})
})
}