Files
8bitMyGoUI/game/healthbar.go
T
ZacharyZhang-NY b4b4d67b92 Audit keyboard, focus, veils, motion and clocks across all 500
A cross-cutting pass, each fix with a test that failed first.

Veils: MyGo still hands a key to a base that turned off this frame,
so every element key now goes through pixel.Shortcut, which an
archtest enforces. Input handlers record and the build applies them
only while enabled; the editor turns away a veil-frame letter or
copy; value bases get a copy and keep only enabled turns. Popups
give a held focus back to their opener when the veil lifts.

Motion: S5 is enforced at the clock. Every Tick, Since, Step,
Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps,
fast animations step at 15, and drawn time is quantized to frames.

Clocks stepping back: Since holds at zero without overflow, and
k.Elapsed moves a stored start to now, so nothing goes negative,
runs backward or indexes out of range.

Also: logs count typed text instead of holding it, gallery names
use glyphs the font has, removable rows are keyed, components take
their own Options (archtest), and sweeps over all 508 demos check
veiled keys, clicks, wheels, the clock stepping back and names.
2026-10-12 16:03:04 -04:00

85 lines
2.2 KiB
Go

package game
import (
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type HealthBarOptions struct {
// Width is in dots; zero means 96.
Width int
}
type trailKey struct{}
// trail is the last hp and full, and the trail.
type trail struct {
seen, full int
from float64
at time.Time
}
// hold is how long a trail waits before draining.
const hold = 300 * time.Millisecond
// HealthBar shows hp with a lagging trail; low HP blinks.
func HealthBar(c *ui.Context, text string, hp, full int, o HealthBarOptions) *ui.Element {
k := pixel.K(c)
amount("HealthBar hp", hp, full)
w := pixel.Or("HealthBar Width", o.Width, 96)
fits("HealthBar", w, text, ratio(full, full))
e := ui.Box(c).Role(ui.RoleMeter).Label(text).Description(ratio(hp, full))
tr := ui.Local(e, trailKey{}, func() trail { return trail{seen: hp, full: full, from: float64(hp)} })
// A new full makes the old trail meaningless.
if full != tr.full {
tr.seen, tr.full, tr.from = hp, full, float64(hp)
}
now := k.Now()
// end is the trail's end: held, then draining fast.
end := func() float64 {
// S5: the drain moves in whole frames, fifteen a second.
drained := max(0, now.Sub(tr.at)-hold).Truncate(time.Second / 15)
return max(float64(tr.seen), tr.from-drained.Seconds()*float64(full))
}
if hp < tr.seen {
tr.from, tr.at = end(), now
}
if hp > tr.seen {
tr.from = float64(hp)
}
tr.seen = hp
shown := end()
if k.Reduce() {
shown = float64(hp)
}
if shown > float64(hp) {
k.Tick(c, 15)
}
// Divide, not multiply: huge HP must not overflow.
ink := pixel.Good
switch {
case hp <= full/4:
ink = pixel.Bad
case hp <= full/2:
ink = pixel.Warn
}
low := hp > 0 && hp <= full/4
top := headH(text)
return e.Size(k.D(w), k.D(top+8)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
head(g, w, text, ratio(hp, full))
fill := ink
// Low HP blinks; reduced, it holds steady.
if low && !k.Reduce() && g.Busy(4)%2 == 1 {
fill = pixel.Lit
}
x := meter(g, 0, top, w, 8, float64(hp)/float64(full), fill)
if t := 2 + int(shown/float64(full)*float64(w-4)); t > x {
g.Rect(x, top+2, t-x, 4, pixel.AccentLit)
}
})
}