info = { type = "image", title = "Rubber duck", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A rubber duck bobs in a bath and quacks", api = 33, } local tr = ({ sk = { ["Duck colour"] = "Farba kačky", ["Quack"] = "Kvákanie", ["speech bubble"] = "bublina", ["notes"] = "noty", ["silent"] = "ticho", ["Waves"] = "Vlny" }, cs = { ["Duck colour"] = "Barva kachny", ["Quack"] = "Kvákání", ["speech bubble"] = "bublina", ["notes"] = "noty", ["silent"] = "ticho", ["Waves"] = "Vlny" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "duck", label = T("Duck colour"), type = "color", default = "#FFD42A" }, { key = "quack", label = T("Quack"), type = "int", default = 0, opts = { T("speech bubble"), T("notes"), T("silent") } }, { key = "wave", label = T("Waves"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local DX, WL = 27, 44 local WX0, WY0, WW, WH = 3, 28, 58, 28 local function c3(c) return c[1], c[2], c[3] end local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local BG = nil local TILE, GROUT = { 204, 238, 232 }, { 178, 218, 212 } local PORC, PORC_D, PORC_L = { 248, 250, 252 }, { 218, 230, 240 }, { 198, 214, 228 } local function disc(cx, cy, r, c) for y = floor(cy - r), floor(cy + r) do local dy = y + 0.5 - cy local k = r * r - dy * dy if k > 0 then local w = sqrt(k) local x0 = floor(cx - w + 0.5) rect(x0, y, floor(cx + w + 0.5) - x0, 1, c3(c)) end end end local function build_bg() rect(0, 0, W, 27, c3(TILE)) for x = 0, W - 1, 8 do rect(x, 0, 1, 27, c3(GROUT)) end for y = 0, 24, 8 do rect(0, y, W, 1, c3(GROUT)) end rect(9, 9, 6, 6, 188, 226, 240) rect(41, 1, 6, 6, 188, 226, 240) rect(10, 10, 4, 4, 168, 212, 236) rect(42, 2, 4, 4, 168, 212, 236) rect(0, 26, W, 2, 244, 248, 250) rect(0, 27, W, 1, c3(PORC_D)) for y = WY0, WY0 + WH - 1 do rect(WX0, y, WW, 1, lerp(152, 96, (y - WY0) / (WH - 1)), lerp(224, 186, (y - WY0) / (WH - 1)), lerp(247, 234, (y - WY0) / (WH - 1))) end rect(0, 28, WX0, 28, c3(PORC)); rect(WX0 + WW, 28, W - WX0 - WW, 28, c3(PORC)) rect(WX0, 28, 1, 28, c3(PORC_L)); rect(WX0 + WW - 1, 28, 1, 28, c3(PORC_L)) rect(0, 56, W, 8, c3(PORC)) rect(0, 56, W, 1, c3(PORC_L)) rect(0, 58, W, 1, c3(PORC_D)) rect(0, 62, W, 2, c3(PORC_D)) disc(7, 31, 2.6, { 250, 253, 255 }); disc(11, 30.5, 2, { 250, 253, 255 }); disc(5, 34, 2, { 250, 253, 255 }) disc(56, 53, 2.6, { 250, 253, 255 }); disc(52, 54, 2, { 250, 253, 255 }); disc(58, 50, 1.6, { 250, 253, 255 }) px(6, 30, 196, 228, 252); px(55, 52, 196, 228, 252); px(10, 29, 196, 228, 252) BG = gfx.snapshot(0, 0, W, H) end local function rell(cx, cy, a, b, phi, c) local cs, sn = cos(phi), sin(phi) local a2, b2 = a * a, b * b local A = cs * cs / a2 + sn * sn / b2 local k1 = 2 * (cs * sn / a2 - sn * cs / b2) local k2 = sn * sn / a2 + cs * cs / b2 local hh = sqrt(a2 * sn * sn + b2 * cs * cs) local r, g, bl = c[1], c[2], c[3] for y = floor(cy - hh), floor(cy + hh) do local dy = y + 0.5 - cy local B, C = k1 * dy, k2 * dy * dy - 1 local d = B * B - 4 * A * C if d > 0 then local sq = sqrt(d) local x0 = floor(cx + (-B - sq) / (2 * A) + 0.5) local x1 = floor(cx + (-B + sq) / (2 * A) + 0.5) if x1 > x0 then rect(x0, y, x1 - x0, 1, r, g, bl) end end end end local BODY, SHADE, WING, LIGHT = { 255, 212, 42 }, { 208, 162, 28 }, { 232, 186, 34 }, { 255, 240, 140 } local BEAK, MOUTH, EYE, CHEEK = { 255, 120, 28 }, { 128, 36, 30 }, { 40, 28, 24 }, { 255, 150, 96 } function on_settings() local c = cfg.duck or { r = 255, g = 212, b = 42 } BODY = { c.r, c.g, c.b } SHADE = { c.r * 4 // 5, c.g * 4 // 5, c.b * 4 // 5 } WING = { c.r * 9 // 10, c.g * 9 // 10, c.b * 9 // 10 } LIGHT = { c.r + (255 - c.r) * 6 // 10, c.g + (255 - c.g) * 6 // 10, c.b + (255 - c.b) * 6 // 10 } end local function rot(x, y, ox, oy, a) local cs, sn = cos(a), sin(a) local dx, dy = x - ox, y - oy return ox + dx * cs - dy * sn, oy + dx * sn + dy * cs end local OPEN = false local function duck(phi, hh, bx, by) local function P(x, y, head) if head then x, y = rot(x, y, 5, -8, hh) end x, y = rot(x, y, 0, -2, phi) return bx + x, by + y end local x, y x, y = P(-13, -8.2); rell(x, y, 4.6, 2.7, 0.55 + phi, BODY) x, y = P(-1, -3); rell(x, y, 13.8, 8, phi, SHADE) x, y = P(-1, -4.3); rell(x, y, 13, 7.2, phi, BODY) x, y = P(-4, -4.2); rell(x, y, 7, 3.5, 0.12 + phi, WING) x, y = P(2.5, -9.6); rell(x, y, 6, 1.5, -0.08 + phi, LIGHT) x, y = P(9, -13.5, true); rell(x, y, 6.6, 6.6, 0, BODY) x, y = P(7, -17.4, true); rell(x, y, 3, 1.2, 0, LIGHT) if OPEN then x, y = P(15.6, -11.8, true); rell(x, y, 3.6, 1.6, hh + phi, MOUTH) x, y = P(16.6, -13.6, true); rell(x, y, 4.2, 1.5, -0.12 + hh + phi, BEAK) x, y = P(15.8, -10.2, true); rell(x, y, 3.4, 1.2, 0.18 + hh + phi, BEAK) else x, y = P(16.6, -12.2, true); rell(x, y, 4.4, 2.2, hh + phi, BEAK) end x, y = P(10.2, -10.6, true); rell(x, y, 1.6, 1.1, 0, CHEEK) x, y = P(11.4, -15.2, true) rect(floor(x + 0.5), floor(y + 0.5), 1, 2, c3(EYE)) end local function ring(cx, cy, rx, ry, a, pass) local ryi = ry - 0.9 local rxi = rx - 1.5 for y = floor(cy - ry), floor(cy + ry) do local dy = y + 0.5 - cy if (pass < 0 and dy < 0) or (pass > 0 and dy >= 0) then local k = 1 - (dy / ry) ^ 2 if k > 0 then local xo = rx * sqrt(k) local xi = 0 if ryi > 0 and rxi > 0 then local ki = 1 - (dy / ryi) ^ 2 if ki > 0 then xi = rxi * sqrt(ki) end end local xa, xb = floor(cx - xo + 0.5), floor(cx - xi + 0.5) if xb <= xa then xb = xa + 1 end blend_rect(xa, y, xb - xa, 1, 240, 252, 255, a) local xc, xd = floor(cx + xi + 0.5), floor(cx + xo + 0.5) if xd <= xc then xc = xd - 1 end blend_rect(xc, y, xd - xc, 1, 240, 252, 255, a) end end end end local RP, RL = 75, 150 local function rings(t, pass) gfx.clip(WX0, WY0, WW, WH) for k = 0, 1 do local age = (t + k * RP) % (RP * 2) if age < RL then local rx = 5 + age * 0.34 local a = 0.62 * (1 - age / RL) ring(DX + 1, WL + 2, rx, rx * 0.36, a, pass) end end gfx.clip() end local function dash(x, y, len, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 1, 240, 252, 255, a * (1 - f)) blend_rect(ix + 1, y, len - 1, 1, 240, 252, 255, a) blend_rect(ix + len, y, 1, 1, 240, 252, 255, a * f) end local DR = { 31, 35, 39, 49, 52 } local function streaks(t) gfx.clip(WX0, WY0, WW, WH) for i = 1, #DR do local v = (i % 2 == 0 and -1 or 1) * (3 + i) / STEP_HZ local x = (i * 17 + t * v) % (WW + 16) - 8 + WX0 dash(x, DR[i], 6 + i, 0.42) if i <= 2 then dash((x - WX0 + 29) % (WW + 16) - 8 + WX0, DR[i], 4 + i % 3, 0.32) end end gfx.clip() end local QP = 210 local BUB_X, BUB_Y, BUB_W, BUB_H = 24, 7, 37, 12 local TY = nil local function bubble(q) if q < 1 then return end local w, h = BUB_W, BUB_H if q < 3 then w, h = floor(w * (0.35 + q * 0.2)), floor(h * (0.35 + q * 0.2)) end local x = BUB_X + BUB_W - w gfx.round_rect(x, BUB_Y + BUB_H - h, w, h, 255, 255, 255, 74, 100, 150, 3) rect(45, BUB_Y + BUB_H, 3, 1, 255, 255, 255) rect(46, BUB_Y + BUB_H + 1, 2, 1, 255, 255, 255) rect(47, BUB_Y + BUB_H + 2, 1, 1, 255, 255, 255) px(44, BUB_Y + BUB_H, 74, 100, 150); px(48, BUB_Y + BUB_H, 74, 100, 150) px(45, BUB_Y + BUB_H + 1, 74, 100, 150); px(48, BUB_Y + BUB_H + 1, 74, 100, 150) px(46, BUB_Y + BUB_H + 2, 74, 100, 150); px(48, BUB_Y + BUB_H + 2, 74, 100, 150) px(47, BUB_Y + BUB_H + 3, 74, 100, 150) if q >= 3 then if not TY then local top, bot = gfx.text_ink("QUACK", { font = "5x7" }) TY = BUB_Y + (BUB_H - (bot - top + 1)) // 2 - top end gfx.text(BUB_X, TY, "QUACK", 46, 60, 110, { font = "5x7", w = BUB_W, align = "center" }) end end local function note(x, y, a) blend_rect(floor(x), floor(y), 3, 2, 90, 70, 170, a) blend_rect(floor(x) + 2, floor(y) - 6, 1, 7, 90, 70, 170, a) blend_rect(floor(x) + 3, floor(y) - 6, 2, 1, 90, 70, 170, a) blend_rect(floor(x) + 4, floor(y) - 5, 1, 2, 90, 70, 170, a) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local mode = cfg.quack or 0 local wv = cfg.wave or 2 local q = (t % QP) - 150 OPEN = mode ~= 2 and q >= 0 and ((q >= 2 and q < 10) or (q >= 14 and q < 22)) local ph = TAU * t / 110 local bob = (0.7 + 0.5 * wv) * sin(ph) local phi = (0.05 + 0.03 * wv) * sin(ph + 1.1) local hh = OPEN and -0.16 or 0.02 * sin(ph * 1.7) local wl = WL + floor(0.5 * bob + 0.5) streaks(t) rings(t, -1) gfx.clip(0, 0, W, wl) duck(phi, hh, DX, WL + bob) gfx.clip() for k = 1, 5 do local w = 22 - k * 3 + floor(1.5 * sin(t * 0.13 + k) + 0.5) blend_rect(DX - 2 - w // 2, wl + k - 1, w, 1, BODY[1], BODY[2], BODY[3], 0.34 - k * 0.05) end rings(t, 1) for s = 0, 11 do blend_rect(DX - 17 + s * 3, wl - 1 + floor(1.2 * sin(s * 1.1 + t * 0.12) + 0.5), 3, 1, 240, 252, 255, 0.8) end if mode == 0 then if q >= 0 and q < 40 then bubble(q) end elseif mode == 1 then for n = 0, 1 do local age = q - n * 11 if age >= 0 and age < 46 then local a = age > 30 and (46 - age) / 16 or 1 note(DX + 20 + 0.25 * age + 2.2 * sin(age * 0.24 + n), wl - 18 - 0.48 * age, 0.9 * a) end end end end