Files
8bitMyGoUI/game/scoreboard.go
T
ZacharyZhang-NY f48075230f Add game components 246-268; data.DrawSlot, Canvas.TextVeil
Health, mana and experience bars, quarter hearts, lives, a spinning
coin counter, score board, stage timer, minimap with fog, compass,
hotbar, paper-doll equipment, skill tree, skill buttons with cooldown
wipes, status effects, a boss bar, damage numbers, quest log and
tracker, party frames, character sheet, level-up screen and shop.
data exports DrawSlot so every slot looks alike; pixel adds TextVeil
for dissolving text. Messages the font cannot draw are fixed and
TestMessagesHaveGlyphs holds every message to the font.
2026-10-12 01:53:20 -04:00

46 lines
1.3 KiB
Go

package game
import (
"fmt"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ScoreBoardOptions struct {
// Digits pads both scores; zero means 6.
Digits int
}
// ScoreBoard shows SCORE and HI-SCORE in big digits.
func ScoreBoard(c *ui.Context, score, hi int, o ScoreBoardOptions) *ui.Element {
k := pixel.K(c)
if score < 0 || hi < 0 {
panic(fmt.Sprintf("game: ScoreBoard %d and %d", score, hi))
}
digits := pixel.Or("ScoreBoard Digits", o.Digits, 6)
// A score past the best becomes the best, lit.
best, beat := max(hi, score), score > hi
cols := []struct {
name, n string
ink pixel.Slot
}{{k.Msg("score"), fmt.Sprintf("%0*d", digits, score), pixel.Faint}, {k.Msg("hi_score"), fmt.Sprintf("%0*d", digits, best), pixel.Faint}}
if beat {
cols[1].ink = pixel.Warn
}
w, top := 0, 0
for _, col := range cols {
w, top = max(w, tagW(col.name), 2*pixel.Measure(col.n)), max(top, tagH(col.name)+2)
}
label := fmt.Sprintf("%s %d, %s %d", cols[0].name, score, cols[1].name, best)
return ui.Box(c).Role(ui.RoleText).Label(label).Size(k.D(2*w+16), k.D(top+2*pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i, col := range cols {
x := i * (w + 16)
tag(g, x, 0, col.name, col.ink)
g.Text(x, top-2, col.n, pixel.Ink, 2)
}
})
}