info = { type = "image", title = "Banana dance", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "A peeled banana dances and waves its peel arms", api = 33, } local tr = ({ sk = { ["Background"] = "Pozadie", ["Party"] = "Oslava", ["Ocean"] = "Oceán", ["Mint"] = "Mätová" }, cs = { ["Background"] = "Pozadí", ["Party"] = "Oslava", ["Ocean"] = "Oceán", ["Mint"] = "Mátová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tempo", label = T("Tempo"), type = "int", min = 1, max = 3, default = 2 }, { key = "bg", label = T("Background"), type = "int", default = 0, opts = { T("Party"), T("Ocean"), T("Mint") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, exp, atan = math.floor, math.ceil, math.sin, math.cos, math.exp, math.atan local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI, TAU = 3.14159265, 6.28318531 local BGS = { { 108, 34, 158, 168, 52, 172 }, { 22, 92, 168, 40, 128, 200 }, { 20, 128, 112, 40, 166, 138 }, } local BGP = BGS[1] local FLR, FLG, FLB = 255, 242, 190 local PER, PEG, PEB = 250, 210, 56 local SHR, SHG, SHB = 226, 170, 32 local STR, STG, STB = 112, 76, 40 local function erow(y, a, b, r, g, bb) local L, R = ceil(a), floor(b) if R > L then rect(L, y, R - L, 1, r, g, bb) if L - a > 0.12 then blend_rect(L - 1, y, 1, 1, r, g, bb, L - a) end if b - R > 0.12 then blend_rect(R, y, 1, 1, r, g, bb, b - R) end elseif b - a > 0.12 then blend_rect(floor(a), y, 1, 1, r, g, bb, b - a) end end local function cx_at(P, i0, i1, yc) local x0, y0, x1, y1 = P[2 * i0 - 1], P[2 * i0], P[2 * i1 - 1], P[2 * i1] if y1 == y0 then return x1 end return x0 + (yc - y0) * (x1 - x0) / (y1 - y0) end local function poly(P, r, g, b) local n = #P // 2 local ti, bi, yt, yb = 1, 1, P[2], P[2] for i = 2, n do local y = P[2 * i] if y < yt then yt, ti = y, i end if y > yb then yb, bi = y, i end end local a0, a1 = ti, ti % n + 1 local c0, c1 = ti, (ti - 2) % n + 1 for y = ceil(yt - 0.5), ceil(yb - 0.5) - 1 do local yc = y + 0.5 while P[2 * a1] < yc and a1 ~= bi do a0, a1 = a1, a1 % n + 1 end while P[2 * c1] < yc and c1 ~= bi do c0, c1 = c1, (c1 - 2) % n + 1 end local xa, xb = cx_at(P, a0, a1, yc), cx_at(P, c0, c1, yc) if xa > xb then xa, xb = xb, xa end local x0, x1 = ceil(xa - 0.5), ceil(xb - 0.5) - 1 if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, r, g, b) end end end local FX, FY, BH = 32, 61, 46 local SS = { 0, 0.05, 0.12, 0.22, 0.35, 0.5, 0.65, 0.78, 0.88, 0.94, 0.98, 1 } local SW = { 1.6, 2.6, 4.6, 6.6, 7.8, 8.3, 8.1, 7.2, 5.8, 4.2, 2.4, 0.3 } local function spx(s) return -5 * sin(PI * s) + 4 * s * s end local function wid(s) for i = 2, #SS do if s <= SS[i] then return SW[i - 1] + (SW[i] - SW[i - 1]) * (s - SS[i - 1]) / (SS[i] - SS[i - 1]) end end return SW[#SW] end local FL0, FL1, PL0, PL1 = {}, {}, {}, {} for r = 0, BH - 1 do local s = (r + 0.5) / BH local c, w = spx(s), wid(s) if s >= 0.45 then FL0[r], FL1[r] = c - w, c + w end if s <= 0.56 then PL0[r], PL1[r] = c - w - 0.6, c + w + 0.6 end end local FLAP = { -3.2, -1, 3.2, -1, 2.6, 8, 1.6, 14, 0, 17, -1.6, 14, -2.6, 8 } local FTIP = { -1.9, 12, 1.9, 12, 1.6, 14, 0, 17, -1.6, 14 } local RS = 0.53 local ROOT_LX, ROOT_RX, ROOT_H = spx(RS) - wid(RS) * 0.85, spx(RS) + wid(RS) * 0.85, BH * RS local EYE_S, MOUTH_S, CHEEK_S = 0.8, 0.69, 0.735 local KA, SX, SY, BASE = 0, 1, 1, FY local function tf(x, h) return FX + x * SX + h * SY * KA, BASE - h * SY end local FA, FB_ = {}, {} local function flap(rx, ry, ang) local ca, sa = cos(ang), sin(ang) for i = 1, #FLAP, 2 do local x, y = FLAP[i], FLAP[i + 1] FA[i], FA[i + 1] = rx + x * ca - y * sa, ry + x * sa + y * ca end poly(FA, 240, 186, 38) for i = 1, #FTIP, 2 do local x, y = FTIP[i], FTIP[i + 1] FB_[i], FB_[i + 1] = rx + x * ca - y * sa, ry + x * sa + y * ca end poly(FB_, 128, 86, 36) end local RCX, RCY, NR = 32, 30, 10 local BG = nil local CFX, CFY, CFP, CFC = {}, {}, {}, {} local CONF = { 90, 230, 255, 255, 240, 90, 130, 255, 130, 255, 255, 255, 255, 150, 210 } for k = 1, 14 do local h = hash32(k * 0x9E3779B1) CFX[k], CFY[k], CFP[k], CFC[k] = umod(h, W), umod(h >> 8, 56), umod(h >> 16, 40), 3 * (k % 5) end local function build_bg() local cot = {} for k = 0, NR - 1 do local a = (k + 0.5) * PI / NR cot[k + 1] = cos(a) / sin(a) end local xs = {} for y = 0, H - 1 do local dy = y + 0.5 - RCY local n = 0 for k = 1, NR do local x = RCX + dy * cot[k] if x > 0 and x < W then n = n + 1 local i = n while i > 1 and xs[i - 1] > x do xs[i] = xs[i - 1]; i = i - 1 end xs[i] = x end end local ang = atan(dy, 0.5 - RCX) if ang < 0 then ang = ang + TAU end local par = floor(ang / (PI / NR) - 0.5) % 2 local x0 = 0 for i = 1, n + 1 do local x1 = (i <= n) and floor(xs[i] + 0.5) or W if x1 > x0 then if par == 0 then rect(x0, y, x1 - x0, 1, BGP[1], BGP[2], BGP[3]) else rect(x0, y, x1 - x0, 1, BGP[4], BGP[5], BGP[6]) end end x0, par = x1, 1 - par end end BG = gfx.snapshot(0, 0, W, H) end function on_settings() BGP = BGS[(cfg.bg or 0) + 1] or BGS[1] BG = nil end local BEATS = { 22, 18, 15 } local LW = 240 local function smooth(p) if p <= 0 then return 0 elseif p >= 1 then return 1 end return p * p * (3 - 2 * p) 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 BT = BEATS[cfg.tempo or 2] or 18 for k = 1, #CFX do local ph = (t + CFP[k]) // 10 if ph % 4 ~= 0 then local c = CFC[k] local r, g, b = CONF[c + 1], CONF[c + 2], CONF[c + 3] local x, y = CFX[k], CFY[k] if ph % 4 == 1 then rect(x - 1, y, 3, 1, r, g, b) rect(x, y - 1, 1, 3, r, g, b) else px(x, y, r, g, b) end end end local A = 0.15 local al = A * sin(t * PI / BT) local p = (t % BT) / BT local hop = 2.4 * 4 * p * (1 - p) local sq = 0.12 * exp(-p * 14) KA, SY, SX, BASE = sin(al), 1 - sq, 1 + sq * 0.5, FY - hop local sw = 16 - floor(hop * 2 + 0.5) blend_rect(FX - sw // 2, FY, sw, 1, 20, 0, 40, 0.35) blend_rect(FX - sw // 2 + 2, FY + 1, sw - 4, 1, 20, 0, 40, 0.2) local top = floor(BASE - BH * SY) for y = top, floor(BASE) - 1 do local h = (BASE - y - 0.5) / SY if h >= 0 and h < BH then local r = floor(h) local dx = FX + h * SY * KA local a = FL0[r] if a then erow(y, dx + a * SX, dx + FL1[r] * SX, FLR, FLG, FLB) end a = PL0[r] if a then local x0, x1 = dx + a * SX, dx + PL1[r] * SX if h < 2.8 then erow(y, x0, x1, STR, STG, STB) else local s = x1 - 2.2 erow(y, x0, s, PER, PEG, PEB) erow(y, s, x1, SHR, SHG, SHB) end end end end local lagged = 0.6 * A * sin((t - 6) * PI / BT) local fh = 0.18 * sin((t - 4) * TAU / BT) local pl = lagged + 0.5 + fh local pr = lagged - 0.5 - fh local wu = t % LW - 120 local waving = false if wu >= 0 and wu < 70 then local e = smooth(wu / 8) * smooth((70 - wu) / 8) local wv = 0.45 * sin(wu * TAU / 10) if (t // LW) % 2 == 0 then pr = pr + (-2.6 + wv - pr) * e else pl = pl + (2.6 - wv - pl) * e end waving = e > 0.5 end local lx, ly = tf(ROOT_LX, ROOT_H) local rx, ry = tf(ROOT_RX, ROOT_H) flap(lx, ly, pl) flap(rx, ry, pr) local ex = spx(EYE_S) local blink = (t % 97) < 4 or (t % 211) < 3 for k = -1, 1, 2 do local x, y = tf(ex + k * 3.4, BH * EYE_S) local ix, iy = floor(x), floor(y) if blink then rect(ix - 1, iy + 1, 2, 1, 40, 26, 20) else rect(ix - 1, iy - 1, 2, 3, 40, 26, 20) px(ix - 1, iy - 1, 255, 255, 255) end end for k = -1, 1, 2 do local x, y = tf(spx(CHEEK_S) + k * 5.4, BH * CHEEK_S) blend_rect(floor(x) - 1, floor(y), 2, 1, 255, 120, 140, 0.6) end local mx, my = tf(spx(MOUTH_S), BH * MOUTH_S) local ix, iy = floor(mx + 0.5), floor(my) if waving then rect(ix - 1, iy, 3, 2, 110, 40, 30) px(ix, iy + 1, 240, 110, 120) else px(ix - 2, iy, 110, 40, 30) rect(ix - 1, iy + 1, 3, 1, 110, 40, 30) px(ix + 2, iy, 110, 40, 30) end end