info = { type = "image", title = "Dancing cactus", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A potted cactus dances to the beat", api = 33, } local tr = ({ sk = { ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo", ["Flower"] = "Kvet", ["Disco lights"] = "Diskosvetlá" }, cs = { ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle", ["Flower"] = "Květ", ["Disco lights"] = "Disko světla" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tempo", label = T("Tempo"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "flower", label = T("Flower"), type = "color", default = "#FF6EAA" }, { key = "lights", label = T("Disco lights"), type = "bool", default = true }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, exp, abs = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local WALL, WAINS = { 68, 42, 120 }, { 88, 60, 150 } local FL1, FL2 = { 112, 76, 182 }, { 138, 100, 208 } local SHAD = { 62, 40, 106 } local SPK, SPKC = { 40, 26, 76 }, { 100, 74, 154 } local TERRA, TERRA_L, TERRA_D = { 214, 108, 64 }, { 244, 152, 102 }, { 158, 74, 48 } local CACT, CACT_L, CACT_D = { 96, 200, 100 }, { 158, 232, 144 }, { 56, 150, 92 } local SPINE = { 255, 244, 170 } local EYE, MOUTH = { 30, 36, 56 }, { 150, 40, 70 } local LEG = { 30, 20, 48 } local PINKL, CYANL = { 255, 110, 190 }, { 90, 220, 255 } local FLOWER, FLOWER_D = { 255, 110, 170 }, { 255, 238, 120 } local BPMS = { 84, 112, 140 } local TB = 60 / 112 local LIGHTS = true local CX, PIV, GROUND = 32, 50, 56 local function span(xa, xb, y, c) if xb <= xa then return end local r, g, b = c[1], c[2], c[3] local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local function spanh(xa, xb, y, h, c) if xb <= xa then return end local r, g, b = c[1], c[2], c[3] local ia, ib = floor(xa), floor(xb) if ia == ib then brect(ia, y, 1, h, r, g, b, xb - xa) return end local s0 = ia if xa > ia then brect(ia, y, 1, h, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, h, r, g, b) end if xb > ib then brect(ib, y, 1, h, r, g, b, xb - ib) end end local function ell(cx, cy, rx, ry, c) for y = floor(cy - ry), floor(cy + ry) do local dy = (y + 0.5 - cy) / ry local q = 1 - dy * dy if q > 0 then local hw = rx * sqrt(q) span(cx - hw, cx + hw, y, c) end end end local function glow(cx, cy, r, c, a) for y = floor(cy - r), floor(cy + r) do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = floor(sqrt(q) + 0.5) brect(cx - hw, y, 2 * hw, 1, c[1], c[2], c[3], a) end end end local BG = nil local function build_bg() rect(0, 0, W, 46, WALL[1], WALL[2], WALL[3]) rect(0, 44, W, 2, WAINS[1], WAINS[2], WAINS[3]) for k = 0, 2 do local y = 46 + k * 6 rect(0, y, W, 6, FL1[1], FL1[2], FL1[3]) for x = (k % 2) * 8, W - 1, 16 do rect(x, y, 8, 6, FL2[1], FL2[2], FL2[3]) end end for s = 0, 1 do local x = s == 0 and 0 or 55 rect(x, 26, 9, 20, SPK[1], SPK[2], SPK[3]) rect(x + 1, 27, 7, 1, SPKC[1], SPKC[2], SPKC[3]) end BG = gfx.snapshot(0, 0, W, H) end function on_settings() TB = 60 / (BPMS[(cfg.tempo or 1) + 1] or 112) LIGHTS = cfg.lights ~= false local c = cfg.flower or { r = 255, g = 110, b = 170 } FLOWER = { c.r, c.g, c.b } end local SPINES = { { -4, 14 }, { 3, 13 }, { 0, 16 }, { -5, 20 }, { 5, 19 }, { -5, 33 }, { 5, 31 }, { -3, 37 }, { 3, 36 }, { 5, 25 }, { -5, 28 } } local function arm(sx, sy, dir, a) local a2 = a + 1.0 local ex, ey = sx + 4 * dir * cos(a), sy - 4 * sin(a) local hx, hy = ex + 4 * dir * cos(a2), ey - 4 * sin(a2) for k = 1, 4 do local f = k / 4 rect(floor(sx + (ex - sx) * f + 0.5) - 1, floor(sy + (ey - sy) * f + 0.5) - 1, 2, 2, CACT[1], CACT[2], CACT[3]) end for k = 1, 3 do local f = k / 4 rect(floor(ex + (hx - ex) * f + 0.5) - 1, floor(ey + (hy - ey) * f + 0.5) - 1, 2, 2, CACT[1], CACT[2], CACT[3]) end rect(floor(hx + 0.5) - 1, floor(hy + 0.5) - 1, 3, 3, CACT_L[1], CACT_L[2], CACT_L[3]) end local function draw_cactus(bph) local sw = sin(PI * bph) local th = 0.13 * sw local bob = floor(1.4 * abs(cos(PI * bph)) + 0.5) ell(CX, GROUND + 1.5, 9 - bob * 1.2, 2, SHAD) local lf = sw > 0 and sw * 2.4 or 0 local rf = sw < 0 and -sw * 2.4 or 0 for s = 0, 1 do local lift = floor((s == 0 and lf or rf) + 0.5) local x = CX - 5 + s * 8 local top = PIV - bob rect(x, top, 2, GROUND - lift - top - 1, LEG[1], LEG[2], LEG[3]) rect(x - 1 + s, GROUND - lift - 1, 4, 2, LEG[1], LEG[2], LEG[3]) end local cy = 25 - bob local cxs = CX + (PIV - 25) * th local y = 11 while y <= 40 do local h = y < 18 and 1 or 3 if y + h > 41 then h = 41 - y end local ym = y + (h - 1) * 0.5 local hw = 7 if ym < 18 then local dy = (18 - (ym + 0.5)) / 7.5 hw = 7 * sqrt(1 - dy * dy) end local c = CX + (PIV - ym) * th local yy = y - bob spanh(c - hw, c + hw, yy, h, CACT) if hw > 3 then spanh(c - hw, c - hw + 2.2, yy, h, CACT_L) spanh(c + hw - 2.6, c + hw, yy, h, CACT_D) end y = y + h end for i = 1, #SPINES do local s = SPINES[i] px(floor(CX + (PIV - s[2]) * th + s[1] + 0.5), s[2] - bob, SPINE[1], SPINE[2], SPINE[3]) end local fc = CX + (PIV - 24) * th rect(floor(fc - 3.5 + 0.5), 24 - bob, 2, 2, EYE[1], EYE[2], EYE[3]) rect(floor(fc + 1.5 + 0.5), 24 - bob, 2, 2, EYE[1], EYE[2], EYE[3]) local mx = floor(fc - 1.5 + 0.5) if bph % 1 < 0.35 then rect(mx, 28 - bob, 4, 2, MOUTH[1], MOUTH[2], MOUTH[3]) else rect(mx, 28 - bob, 4, 1, EYE[1], EYE[2], EYE[3]) px(mx - 1, 27 - bob, EYE[1], EYE[2], EYE[3]); px(mx + 4, 27 - bob, EYE[1], EYE[2], EYE[3]) end do local c = CX + (PIV - 41) * th spanh(c - 10.2, c + 10.2, 40 - bob, 1, TERRA_L) spanh(c - 10.2, c + 10.2, 41 - bob, 2, TERRA) spanh(c + 7.5, c + 10.2, 40 - bob, 3, TERRA_D) local y = 43 while y <= 49 do local h = y == 49 and 1 or 2 local ym = y + (h - 1) * 0.5 local hw = 8.4 - (ym - 43) * 0.34 c = CX + (PIV - ym) * th spanh(c - hw, c + hw, y - bob, h, TERRA) spanh(c - hw, c - hw + 2.2, y - bob, h, TERRA_L) spanh(c + hw - 3, c + hw, y - bob, h, TERRA_D) y = y + h end end arm(cxs - 7, cy, -1, 0.25 + 0.95 * sin(PI * bph + 0.4)) arm(cxs + 7, cy, 1, 0.25 + 0.95 * sin(PI * bph + PI + 0.4)) local fx = floor(CX + (PIV - 11) * th + 2.6 * sin(PI * bph - 0.9) + 0.5) local fy = 8 - bob local fr, fg, fb = FLOWER[1], FLOWER[2], FLOWER[3] rect(fx - 1, fy - 3, 2, 2, fr, fg, fb); rect(fx - 1, fy + 1, 2, 2, fr, fg, fb) rect(fx - 3, fy - 1, 2, 2, fr, fg, fb); rect(fx + 1, fy - 1, 2, 2, fr, fg, fb) rect(fx - 1, fy - 1, 2, 2, FLOWER_D[1], FLOWER_D[2], FLOWER_D[3]) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local ts = frame / FPS local bph = ts / TB local ph = bph % 1 local pulse = exp(-4 * ph) if LIGHTS then local side = floor(bph) % 2 local a1 = 0.14 + 0.26 * (side == 0 and pulse or 0.0) local a2 = 0.14 + 0.26 * (side == 1 and pulse or 0.0) glow(17, 15, 11, PINKL, a1) glow(47, 15, 11, CYANL, a2) brect(8, 53, 18, 4, PINKL[1], PINKL[2], PINKL[3], a1 * 0.7) brect(38, 53, 18, 4, CYANL[1], CYANL[2], CYANL[3], a2 * 0.7) end local rr = 2.2 + 0.9 * pulse for s = 0, 1 do local x = s == 0 and 4.5 or 59.5 ell(x, 31.5, rr, rr, SPKC) ell(x, 40, rr + 1, rr + 1, SPKC) end draw_cactus(bph) end