Fifteen shop components: PriceTag, Wallet, ShopItem, Cart, Receipt, Checkout, CurrencyConvert, SaleBadge, Quantity, PricingTiers, Coupon, DailyReward, BidTicker, TradeWindow and VendingMachine. Receipt prints dot by dot with feed holes and a torn edge; Coupon cuts real notches; SaleBadge is a sixteen-point starburst; VendingMachine sells A1 to D4 by code and drops the snack into its tray. Sums are checked against overflow, TradeWindow withdraws both confirms on any change to an offer, and a disabled Checkout veils its inert content. text.Odometer now counts digits as text, steps at 15 fps as S5 asks, draws exact digits at rest and lands rolls that floats would stall.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Package shop holds prices, carts, rewards and trading components.
|
|
package shop
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
// Currency is what a price is counted in.
|
|
type Currency int
|
|
|
|
const (
|
|
Gold Currency = iota
|
|
Gems
|
|
)
|
|
|
|
// coin is a currency's icon and word; unknown ones panic.
|
|
func coin(k pixel.Kit, cur Currency) (*pixel.Sprite, string) {
|
|
switch cur {
|
|
case Gold:
|
|
return icon.Coin, k.Msg("coins")
|
|
case Gems:
|
|
return icon.Gem, k.Msg("gems")
|
|
}
|
|
panic(fmt.Sprintf("shop: unknown currency %d", cur))
|
|
}
|
|
|
|
// amount checks n is not negative and prints it.
|
|
func amount(name string, n int) string {
|
|
if n < 0 {
|
|
panic(fmt.Sprintf("shop: %s amount %d is negative", name, n))
|
|
}
|
|
return strconv.Itoa(n)
|
|
}
|
|
|
|
// money draws a currency icon and amount, named for readers.
|
|
func money(c *ui.Context, k pixel.Kit, cur Currency, n int, slot pixel.Slot) *ui.Element {
|
|
sp, word := coin(k, cur)
|
|
s := amount("money", n)
|
|
return ui.Box(c).Size(k.D(14+pixel.Measure(s)), k.D(pixel.LineHeight)).Role(ui.RoleText).Label(s + " " + word).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Sprite(0, 2, sp, 0, pixel.Warn, 1)
|
|
g.Text(14, 0, s, slot, 1)
|
|
})
|
|
}
|