info = { type = "image", title = "Wheel pup", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A robot pup on wheels does a street trick", api = 33, } local tr = ({ sk = { ["Pup colour"] = "Farba psíka", ["Trick"] = "Trik", ["Jump"] = "Skok", ["Turn around"] = "Otočka", ["Hop and bark"] = "Poskok a štekot" }, cs = { ["Pup colour"] = "Barva pejska", ["Trick"] = "Trik", ["Jump"] = "Skok", ["Turn around"] = "Otočka", ["Hop and bark"] = "Poskok a štěkot" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Pup colour"), type = "color", default = "#F08A32" }, { key = "trick", label = T("Trick"), type = "int", default = "jump", opts = { keys = { "jump", "turn", "hop" }, T("Jump"), T("Turn around"), T("Hop and bark") } }, } local rect, px, blend_rect, round_rect = gfx.rect, gfx.px, gfx.blend_rect, gfx.round_rect local blit_idx, blit_rgb, snapshot = gfx.blit_indexed, gfx.blit_rgb, gfx.snapshot local floor, sin, min, max = math.floor, math.sin, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local KR, KG, KB = 255, 0, 255 local V = 40 local CAMF = 0.45 local A, PAUSE, TRICK, BUB = 3.3, 0.4, 3.2, 1.4 local TS = A + PAUSE local TB = TS + TRICK local TD = TB + BUB local TC = 12.8 local XS = 10 local DY = 40 local ACC = 2.5 local function dist(tau) if tau < A then local u = tau / A return V * A * (u - u * u / 2) end if tau < TD then return V * A / 2 end local td = tau - TD if td < ACC then return V * A / 2 + V * td * td / (2 * ACC) end return V * A / 2 + V * ACC / 2 + V * (td - ACC) end local DC = dist(TC) local function canvas(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return { w = w, h = h, g = g } end local function fill(c, v, x0, y0, x1, y1) for y = y0, y1 do for x = x0, x1 do c.g[y * c.w + x + 1] = v end end end local function pack(c, flip) local rows = {} for y = 0, c.h - 1 do local r = {} for x = 1, c.w do r[x] = c.g[y * c.w + (flip and c.w - x + 1 or x)] end rows[y + 1] = string.char(table.unpack(r)) end return table.concat(rows) end local PAL local SPR = {} local function dog_sprite(tail, mouth, blink, dir) local key = tail + mouth * 2 + blink * 4 + (dir < 0 and 8 or 0) local s = SPR[key] if s then return s end local c = canvas(26, 21) fill(c, 1, 3, 9, 18, 16); fill(c, 2, 3, 9, 18, 10); fill(c, 3, 3, 16, 18, 16) fill(c, 0, 3, 9, 3, 9); fill(c, 0, 18, 9, 18, 9); fill(c, 2, 6, 12, 12, 12); fill(c, 3, 6, 14, 12, 14) fill(c, 8, 13, 10, 15, 10) fill(c, 1, 14, 3, 23, 11); fill(c, 2, 14, 3, 23, 3); fill(c, 3, 14, 11, 23, 11) fill(c, 1, 24, 7, 25, 10); fill(c, 3, 24, 10, 25, 10); fill(c, 6, 25, 7, 25, 8) fill(c, 4, 16, 4, 22, 9) if blink == 1 then fill(c, 5, 17, 6, 18, 6); fill(c, 5, 20, 6, 21, 6) else fill(c, 5, 17, 5, 18, 6); fill(c, 5, 20, 5, 21, 6) end if mouth == 1 then fill(c, 5, 18, 8, 21, 9) else fill(c, 5, 18, 9, 21, 9) end fill(c, 3, 15, 0, 16, 2); fill(c, 3, 15, 1, 15, 3); fill(c, 3, 21, 0, 22, 2); fill(c, 3, 22, 1, 22, 3) fill(c, 3, 13, 2, 13, 3) if tail == 0 then fill(c, 9, 2, 8, 2, 8); fill(c, 9, 1, 6, 1, 7); fill(c, 8, 0, 4, 1, 5) else fill(c, 9, 2, 10, 2, 10); fill(c, 9, 0, 11, 1, 11); fill(c, 8, 0, 12, 0, 13) end s = pack(c, dir < 0) SPR[key] = s return s end local function lerpc(c, k, add) return min(255, floor(c * k + add)) end function on_settings() local c = cfg.tone or { r = 240, g = 138, b = 50 } PAL = { 0, 0, 0, c.r, c.g, c.b, lerpc(c.r, 1.2, 36), lerpc(c.g, 1.2, 36), lerpc(c.b, 1.2, 36), floor(c.r * 0.55), floor(c.g * 0.55), floor(c.b * 0.55), 22, 28, 46, 120, 236, 252, 28, 28, 36, 150, 158, 172, 220, 50, 50, 120, 126, 140 } SPR = {} end local BG, FAR, MID, MARK local FARH, MIDH = 28, 26 local FAR_Y, MID_Y = 17, 21 local function ell(cx, cy, rx, ry, r, g, b) for dy = -ry, ry do local hw = floor(rx * math.sqrt(1 - (dy * dy) / (ry * ry + 0.5)) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b) end end local function bake_far() rect(0, 0, 64, FARH, KR, KG, KB) local x, i = 0, 0 while x < 64 do i = i + 1 local h = hash32(i * 0x9E3779B1) local w = 6 + umod(h, 5) if x + w > 62 then w = 64 - x end local top = 2 + umod(h >> 8, 12) local tone = (i % 2 == 0) and { 150, 172, 212 } or { 132, 156, 202 } rect(x, top, w, FARH - top, tone[1], tone[2], tone[3]) rect(x, top, w, 1, 172, 190, 224) for wy = top + 3, FARH - 3, 3 do for wx = x + 1, x + w - 2, 3 do if umod(hash32(wx * 31 + wy * 17 + i), 3) ~= 0 then px(wx, wy, 184, 200, 230) end end end if umod(h >> 16, 3) == 0 then rect(x + w // 2, top - 3, 1, 3, 132, 156, 202) end x = x + w end FAR = snapshot(0, 0, 64, FARH) end local function bake_mid() rect(0, 0, 64, MIDH, KR, KG, KB) for x = 0, 63 do local top = 17 + floor(1.5 * sin(x * 0.55) + 0.5) rect(x, top, 1, MIDH - top, 40, 108, 66) px(x, top, 92, 168, 96) end for _, lx in ipairs({ 20, 48 }) do rect(lx, 3, 1, MIDH - 3, 110, 116, 130) rect(lx - 1, 3, 3, 1, 110, 116, 130) rect(lx - 2, 4, 5, 3, 255, 236, 150) rect(lx - 1, 7, 3, 1, 200, 180, 100) end local trees = { { 6, 11, 6 }, { 38, 10, 6 }, { 56, 12, 5 } } for _, t in ipairs(trees) do local cx, cy, r = t[1], t[2], t[3] rect(cx - 1, cy + r - 2, 2, MIDH - cy - r + 2, 96, 66, 44) for _, o in ipairs({ { 0, 0 }, { -r // 2, 2 }, { r // 2, 2 } }) do ell(cx + o[1], cy + o[2], r - 1, r - 2, 40, 108, 66) end for _, o in ipairs({ { 0, 0 }, { -r // 2, 2 }, { r // 2, 2 } }) do ell(cx + o[1] - 1, cy + o[2] - 1, r - 3, r - 4, 62, 146, 84) end ell(cx - 2, cy - 2, r - 5, r - 6, 112, 188, 104) end MID = snapshot(0, 0, 64, MIDH) end local function bake_mark() rect(0, 0, 64, 14, KR, KG, KB) for x = 0, 63, 16 do rect(x, 0, 1, 5, 168, 168, 178) end for x = 0, 63, 16 do rect(x + 3, 11, 9, 2, 232, 222, 156) end MARK = snapshot(0, 0, 64, 14) end local function build() bake_far(); bake_mid(); bake_mark() for y = 0, 45 do local k = y / 45 rect(0, y, W, 1, floor(70 + 120 * k + 0.5), floor(140 + 86 * k + 0.5), floor(230 + 20 * k + 0.5)) end rect(0, 46, W, 5, 192, 192, 200) rect(0, 46, W, 1, 214, 214, 222) rect(0, 51, W, 1, 118, 120, 132) rect(0, 52, W, 12, 68, 74, 92) rect(0, 62, W, 2, 60, 66, 82) BG = snapshot(0, 0, W, H) end local function scroll(strip, y, h, off) local o = off % 64 blit_rgb(strip, -o, y, 64, h, KR, KG, KB) if o > 0 then blit_rgb(strip, 64 - o, y, 64, h, KR, KG, KB) end end local CLOUDS = { { 4, 16, 2.0, 10 }, { 11, 20, 1.3, 40 }, { 2, 12, 1.0, 70 } } local function cloud_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, 255, 255, 255, a) blend_rect(il, y, 1, 1, 255, 255, 255, a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, 255, 255, 255, a * (xr - ir)) end local function wheel(cx, cy, wx) rect(cx - 2, cy - 3, 5, 7, 28, 28, 36); rect(cx - 3, cy - 2, 7, 5, 28, 28, 36) px(cx, cy, 150, 158, 172) local r, g, b = 120, 126, 140 if floor(wx / 2.5) % 2 == 0 then px(cx - 2, cy, r, g, b); px(cx + 2, cy, r, g, b); px(cx, cy - 2, r, g, b); px(cx, cy + 2, r, g, b) px(cx - 1, cy, r, g, b); px(cx + 1, cy, r, g, b); px(cx, cy - 1, r, g, b); px(cx, cy + 1, r, g, b) else px(cx - 1, cy - 1, r, g, b); px(cx + 1, cy - 1, r, g, b); px(cx - 1, cy + 1, r, g, b); px(cx + 1, cy + 1, r, g, b) px(cx - 2, cy - 2, r, g, b); px(cx + 2, cy - 2, r, g, b); px(cx - 2, cy + 2, r, g, b); px(cx + 2, cy + 2, r, g, b) end end local function pup_state(tau, trick) if tau < A then local u = tau / A return -26 + (XS + 26) * (2 * u - u * u), 0, 1, 0 end if tau < TS then return XS, 0, 1, 0 end if tau < TB then local tt = tau - TS if trick == "jump" then if tt < 0.5 then return XS, tt > 0.15 and 1 or 0, 1, 0 end if tt < 1.5 then local u = tt - 0.5 return XS + 26 * u, -16 * 4 * u * (1 - u), 1, (u > 0.2 and u < 0.8) and 1 or 0 end return XS + 26, 0, 1, 0 elseif trick == "turn" then if tt < 0.7 then return XS + 8 * tt / 0.7, 0, 1, 0 end if tt < 1.8 then return XS + 8 - 12 * (tt - 0.7) / 1.1, 0, -1, 0 end if tt < 2.6 then return XS - 4 + 4 * (tt - 1.8) / 0.8, 0, 1, 0 end return XS, 0, 1, 0 else local hop = 0 for _, h0 in ipairs({ 0.4, 1.4 }) do local u = (tt - h0) / 0.7 if u > 0 and u < 1 then hop = -6 * 4 * u * (1 - u) end end local open = ((tt > 0.55 and tt < 0.85) or (tt > 1.55 and tt < 1.85)) and 1 or 0 return XS, hop, 1, open end end local x = XS + (trick == "jump" and 26 or 0) if tau < TD then local tb = tau - TB local open = ((tb > 0.15 and tb < 0.5) or (tb > 0.8 and tb < 1.15)) and 1 or 0 return x - open, 0, 1, open end local td = tau - TD if td < ACC then return x + 0.55 * V * td * td / (2 * ACC), 0, 1, 0 end return x + 0.55 * V * ACC / 2 + 0.55 * V * (td - ACC), 0, 1, 0 end local BUB_W, BUB_H = 36, 13 local BUB_TXT = { font = "5x7", w = BUB_W - 2, align = "center" } local TXT_DY local function bubble(x, y) local bx = max(1, min(W - 1 - BUB_W, x - 4)) local by = y - BUB_H - 4 round_rect(bx, by, BUB_W, BUB_H, 255, 255, 255, 60, 64, 84, 2) local tx = min(max(bx + 6, x + 12), bx + BUB_W - 6) px(tx, by + BUB_H, 255, 255, 255); px(tx, by + BUB_H + 1, 255, 255, 255); px(tx + 1, by + BUB_H, 255, 255, 255) px(tx - 1, by + BUB_H, 60, 64, 84); px(tx + 2, by + BUB_H, 60, 64, 84); px(tx, by + BUB_H + 2, 60, 64, 84) px(tx + 1, by + BUB_H + 1, 60, 64, 84); px(tx - 1, by + BUB_H + 1, 60, 64, 84) if not TXT_DY then local top, bot = gfx.text_ink("WOOF!", BUB_TXT) TXT_DY = (BUB_H - (bot - top + 1)) // 2 - top end gfx.text(bx + 1, by + TXT_DY, "WOOF!", 44, 48, 70, BUB_TXT) end local function cone(x) local y = 60 rect(x - 1, y - 1, 7, 2, 60, 40, 30) rect(x + 2, y - 7, 1, 2, 240, 110, 30) rect(x + 1, y - 5, 3, 2, 244, 244, 244) rect(x + 1, y - 3, 3, 2, 240, 110, 30) rect(x, y - 2, 5, 1, 240, 110, 30) end function render() if not BG then build() end if not PAL then on_settings() end local t = frame / FPS local cyc = floor(t / TC) local tau = t - cyc * TC local trick = cfg.trick or "jump" local dtot = cyc * DC + dist(tau) local cam = CAMF * dtot blit_rgb(BG, 0, 0, W, H) for i = 1, 3 do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + 2 * L) - L cloud_row(x + L * 0.25, x + L * 0.75, c[1], 0.8) cloud_row(x, x + L, c[1] + 1, 0.85) cloud_row(x + L * 0.1, x + L * 0.9, c[1] + 2, 0.6) end scroll(FAR, FAR_Y, FARH, floor(cam * 0.1)) scroll(MID, MID_Y, MIDH, floor(cam * 0.35)) scroll(MARK, 46, 14, floor(cam)) local x, dy, dir, open = pup_state(tau, trick) if trick == "jump" then local cx = floor(35 + CAMF * (V * A / 2 - dist(tau)) + 0.5) if cx > -7 and cx < 64 then cone(cx) end end local ix = floor(x + 0.5) local iy = DY + floor(dy + 0.5) blend_rect(ix + 3, 61, 20, 1, 20, 24, 36, 0.4 - 0.3 * min(1, -dy / 16)) local wx = x + cam if ix > -26 and ix < W then local blink = (t % 3.2) < 0.15 and 1 or 0 local tail = floor(t * 6) % 2 blit_idx(dog_sprite(tail, open, blink, dir), PAL, ix, iy, 26, 21, 26, 21, true) wheel(ix + 6, iy + 17, wx); wheel(ix + 16, iy + 17, wx) if tau >= TB and tau < TD then bubble(ix, iy) end end end