Files
8bitMyGoUI/game/treasurechest.go
T
ZacharyZhang-NY 86a6901333 Add game components 269-290; CommandMenu AutoFocus
Press start, title screen, save slots, options menu, game over with
CONTINUE, credits roll, ending card, loading tips, letterbox, mission
brief, achievements, result screen, crafting grid, loot popup,
treasure chest, gacha reel, interact prompt, turn order, battle log,
target selector, ATB gauge and stamina bar. Cued effects count runs,
so each cue change plays once and reports once. CommandMenu takes
AutoFocus when enabled. HealthBar, ManaBar and CountUp keep exact
colors, ticks and counts for the largest ints.
2026-10-12 02:48:20 -04:00

95 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package game
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type TreasureChestOptions struct {
Disabled bool
}
type TreasureChestResult struct {
Element *ui.Element
// Opened is true in the frame a press opens it.
Opened bool
}
type lidKey struct{}
// lid is whether the chest was open, and since when.
type lid struct {
open bool
at time.Time
}
// TreasureChest opens in frames when pressed; the item rises out.
func TreasureChest(c *ui.Context, text string, it data.Item, open *bool, o TreasureChestOptions) TreasureChestResult {
k := pixel.K(c)
if it.Sprite == nil {
panic(fmt.Sprintf("game: TreasureChest item %q has no sprite", it.Name))
}
shell, base, st := pixel.Shell(c, func() *ui.Element { return ui.ButtonBase(c).Label(text).Disabled(o.Disabled) })
res := TreasureChestResult{Element: shell}
if base.Clicked() && !*open {
*open, res.Opened = true, true
slog.Debug("8bit: chest opened", "label", text, "item", it.Name)
}
// First shown, the chest sits as open says.
s := ui.Local(base, lidKey{}, func() lid { return lid{open: *open} })
if *open != s.open {
s.open, s.at = *open, k.Now()
}
frame, rise := 0, 0
if *open {
frame, rise = 2, 10
if !k.Reduce() {
frame, rise = k.Since(c, s.at, 8, 2), k.Since(c, s.at, 16, 10)
}
}
if *open {
base.Description(it.String())
}
// Room above the chest holds the fully risen item.
sp := it.Sprite
top := sp.H + 13
shell.Size(k.D(24), k.D(top+14)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
if frame == 2 {
g.Sprite((24-sp.W)/2, top-3-sp.H-rise, sp, 0, pixel.Ink, 1)
}
chest(g, 2, top, frame)
g.Finish(st)
})
return res
}
// chest draws a 20×14 chest: closed, ajar or open.
func chest(g pixel.Canvas, x, y, frame int) {
g.Frame(x, y+5, 20, 9, pixel.Flat, pixel.Shade)
switch frame {
case 0:
g.Frame(x, y, 20, 6, pixel.Flat, pixel.Shade)
case 1:
g.Frame(x, y-3, 20, 6, pixel.Flat, pixel.Shade)
g.Rect(x+1, y+3, 18, 2, pixel.Ink)
g.Veil(x+1, y+3, 18, 2, pixel.Warn, 6)
default:
g.Frame(x, y-4, 20, 4, pixel.Flat, pixel.Edge)
g.Rect(x+1, y+5, 18, 3, pixel.Ink)
g.Veil(x+1, y+5, 18, 3, pixel.Warn, 8)
}
g.Rect(x+9, y+5, 2, 9, pixel.Warn)
if frame == 0 {
g.Rect(x+9, y, 2, 6, pixel.Warn)
g.Rect(x+8, y+4, 4, 4, pixel.Warn)
g.Dot(x+10, y+6, pixel.Edge)
}
}