Files
8bitMyGoUI/game/hearts.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

44 lines
1.3 KiB
Go

package game
import (
"fmt"
"math"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type HeartsOptions struct {
// PerRow wraps the hearts; zero means 10.
PerRow int
}
// Hearts shows hp of full hearts, in quarters.
func Hearts(c *ui.Context, text string, hp, full float64, o HeartsOptions) *ui.Element {
k := pixel.K(c)
if full < 1 || full != math.Trunc(full) || !(hp >= 0 && hp <= full) || hp*4 != math.Trunc(hp*4) {
panic(fmt.Sprintf("game: Hearts %v of %v; hearts are whole, health in quarters", hp, full))
}
per := pixel.Or("Hearts PerRow", o.PerRow, 10)
n := int(full)
cols, rows := min(n, per), (n+per-1)/per
shown := strconv.FormatFloat(hp, 'f', -1, 64) + "/" + strconv.Itoa(n)
return ui.Box(c).Role(ui.RoleMeter).Label(text).Description(shown).Size(k.D(cols*11+1), k.D(rows*10+2)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range n {
x, y := i%per*11, i/per*10
// Quarters fill from the bottom left, clockwise.
q := min(4, max(0, int(hp*4)-4*i))
for j, part := range [][2]int{{0, 6}, {0, 0}, {6, 0}, {6, 6}} {
if j < q {
g.Clip(x+part[0], y+part[1], 6, 6, func() { g.Sprite(x, y, icon.Heart, 0, pixel.Bad, 1) })
}
}
g.Sprite(x, y, icon.HeartEmpty, 0, pixel.Edge, 1)
}
})
}