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.
134 lines
5.8 KiB
Go
134 lines
5.8 KiB
Go
package shop
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type SaleBadgeOptions struct{}
|
|
|
|
var (
|
|
// A sixteen-point starburst and its rim, to flash.
|
|
burst = pixel.MustSprite(`
|
|
................................................
|
|
.......................ee.......................
|
|
.......................ee.......................
|
|
...............ee.....e##e.....ee...............
|
|
...............e#e...e####e...e#e...............
|
|
..............e###ee.e####e.ee###e..............
|
|
..............e#####e######e#####e..............
|
|
.......e......e##################e......e.......
|
|
........eeeee.e##################e.eeeee........
|
|
........e####e####################e####e........
|
|
........e##############################e........
|
|
........e##############################e........
|
|
........e##############################e........
|
|
.........e############################e.........
|
|
.....eeee##############################eeee.....
|
|
...ee######################################ee...
|
|
...e########################################e...
|
|
....e######################################e....
|
|
.....e####################################e.....
|
|
.....e####################################e.....
|
|
......e##################################e......
|
|
....ee####################################ee....
|
|
...e########################################e...
|
|
.ee##########################################ee.
|
|
.ee##########################################ee.
|
|
...e########################################e...
|
|
....ee####################################ee....
|
|
......e##################################e......
|
|
.....e####################################e.....
|
|
.....e####################################e.....
|
|
....e######################################e....
|
|
...e########################################e...
|
|
...ee######################################ee...
|
|
.....eeee##############################eeee.....
|
|
.........e############################e.........
|
|
........e##############################e........
|
|
........e##############################e........
|
|
........e##############################e........
|
|
........e####e####################e####e........
|
|
........eeeee.e##################e.eeeee........
|
|
.......e......e##################e......e.......
|
|
..............e#####e######e#####e..............
|
|
..............e###ee.e####e.ee###e..............
|
|
...............e#e...e####e...e#e...............
|
|
...............ee.....e##e.....ee...............
|
|
.......................ee.......................
|
|
.......................ee.......................
|
|
................................................`)
|
|
rim = pixel.MustSprite(`
|
|
................................................
|
|
.......................##.......................
|
|
.......................##.......................
|
|
...............##.....#..#.....##...............
|
|
...............#.#...#....#...#.#...............
|
|
..............#...##.#....#.##...#..............
|
|
..............#.....#......#.....#..............
|
|
.......#......#..................#......#.......
|
|
........#####.#..................#.#####........
|
|
........#....#....................#....#........
|
|
........#..............................#........
|
|
........#..............................#........
|
|
........#..............................#........
|
|
.........#............................#.........
|
|
.....####..............................####.....
|
|
...##......................................##...
|
|
...#........................................#...
|
|
....#......................................#....
|
|
.....#....................................#.....
|
|
.....#....................................#.....
|
|
......#..................................#......
|
|
....##....................................##....
|
|
...#........................................#...
|
|
.##..........................................##.
|
|
.##..........................................##.
|
|
...#........................................#...
|
|
....##....................................##....
|
|
......#..................................#......
|
|
.....#....................................#.....
|
|
.....#....................................#.....
|
|
....#......................................#....
|
|
...#........................................#...
|
|
...##......................................##...
|
|
.....####..............................####.....
|
|
.........#............................#.........
|
|
........#..............................#........
|
|
........#..............................#........
|
|
........#..............................#........
|
|
........#....#....................#....#........
|
|
........#####.#..................#.#####........
|
|
.......#......#..................#......#.......
|
|
..............#.....#......#.....#..............
|
|
..............#...##.#....#.##...#..............
|
|
...............#.#...#....#...#.#...............
|
|
...............##.....#..#.....##...............
|
|
.......................##.......................
|
|
.......................##.......................
|
|
................................................`)
|
|
)
|
|
|
|
// SaleBadge is a SALE -n% starburst whose rim flashes.
|
|
func SaleBadge(c *ui.Context, percent int, o SaleBadgeOptions) *ui.Element {
|
|
k := pixel.K(c)
|
|
if percent < 1 || percent > 99 {
|
|
panic(fmt.Sprintf("shop: SaleBadge of %d%%", percent))
|
|
}
|
|
sale, off := k.Msg("sale"), "-"+strconv.Itoa(percent)+"%"
|
|
return ui.Box(c).Size(k.D(burst.W), k.D(burst.H)).Role(ui.RoleImage).Label(sale + " " + off).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Sprite(0, 0, burst, 0, pixel.Bad, 1)
|
|
// Each second the rim turns gold, unless motion is reduced.
|
|
if g.Step(4)/4%2 == 1 {
|
|
g.Sprite(0, 0, rim, 0, pixel.Warn, 1)
|
|
}
|
|
g.Text((burst.W-pixel.Measure(sale))/2, 8, sale, pixel.Lit, 1)
|
|
g.Text((burst.W-pixel.Measure(off))/2, 24, off, pixel.Lit, 1)
|
|
})
|
|
}
|