info = { type = "image", title = "Cube gang", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Cubes blink, hop and merge into one big cube", api = 33, } local ltr = ({ sk = { ["Colour"] = "Farba", ["Ink"] = "Čierna", ["Blue"] = "Modrá", ["Green"] = "Zelená", ["Plum"] = "Slivková", ["Cubes"] = "Kocky" }, cs = { ["Colour"] = "Barva", ["Ink"] = "Černá", ["Blue"] = "Modrá", ["Green"] = "Zelená", ["Plum"] = "Švestková", ["Cubes"] = "Kostky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tone", label = LT("Colour"), type = "int", default = "ink", opts = { keys = { "ink", "blue", "green", "plum" }, LT("Ink"), LT("Blue"), LT("Green"), LT("Plum") } }, { key = "n", label = LT("Cubes"), type = "int", default = "n16", opts = { keys = { "n9", "n16" }, "9", "16" } }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor, abs = math.floor, math.abs local hash32, umod = math.hash32, math.umod local W = gfx.W local TONES = { ink = { { 238, 234, 224 }, { 28, 30, 44 }, { 232, 92, 56 } }, blue = { { 222, 234, 252 }, { 30, 56, 140 }, { 255, 170, 40 } }, green = { { 232, 240, 222 }, { 28, 84, 66 }, { 240, 84, 110 } }, plum = { { 244, 230, 236 }, { 70, 36, 92 }, { 40, 170, 160 } }, } local BG, CUBE, ACC = TONES.ink[1], TONES.ink[2], TONES.ink[3] local LAYOUT = { n16 = { 16, 12, 4, 15, 3, { 2, 4, 3, 7, 4 }, 2, { 2, 1, 0, 0, 1, 2 } }, n9 = { 9, 16, 3, 20, 4, { 3, 6, 3, 10, 5 }, 3, { 3, 1, 0, 0, 1, 3 } }, } local N, S, G, EY, RN, HS = 16, 12, 4, LAYOUT.n16[6], 2, LAYOUT.n16[8] local HX, HY, CX, CY, RANK = {}, {}, {}, {}, {} function on_settings() local t = TONES[cfg.tone] or TONES.ink BG, CUBE, ACC = t[1], t[2], t[3] local L = LAYOUT[cfg.n] or LAYOUT.n16 local pitch, off N, S, G, pitch, off, EY, RN, HS = L[1], L[2], L[3], L[4], L[5], L[6], L[7], L[8] for i = 1, N do local c, r = (i - 1) % G, (i - 1) // G HX[i], HY[i] = off + c * pitch, off + r * pitch CX[i], CY[i] = 8 + c * S, 8 + r * S RANK[i] = floor(3 * (G - 1) - (abs(c - (G - 1) / 2) + abs(r - (G - 1) / 2)) * 3) end end local function sr(x, y, w, h, c) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, b = c[1], c[2], c[3] if fx < 0.05 and fy < 0.05 then rect(ix, iy, w, h, r, g, b) return end if w > 1 and h > 1 then rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) end if w > 1 then blend_rect(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy) blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) end if h > 1 then blend_rect(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx) blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) end blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end local function body(x, y, s, n, c) for k = 0, n do sr(x + n - k, y + k, s - 2 * (n - k), s - 2 * k, c) end end local function sign(v) return v > 0.4 and 1 or (v < -0.4 and -1 or 0) end local function ease(s) return s * s * (3 - 2 * s) end local function clamp01(v) return v < 0 and 0 or (v > 1 and 1 or v) end local BLINK_B = { 14, 8, 3, 0, 0, 3, 8, 14 } local GAZE = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 } } local function eyes(x, y, dx, dy, k, e, hs) local ew, eh = e[1], e[2] local h = hs[k] or eh if h <= 0 then return end local ey = y + e[5] + dy + (eh - h) // 2 sr(x + e[3] + dx, ey, ew, h, BG) sr(x + e[4] + dx, ey, ew, h, BG) end local BIG_EYE = { 7, 20, 11, 30, 12 } local CYCLE = 720 local JUMPS = { 60, 150, 230 } local function lift_at(f, t0, len, hgt) local s = (f - t0) / len if s < 0 or s > 1 then return 0 end return 4 * hgt * s * (1 - s) end function render() local f = frame % CYCLE local cyc = frame // CYCLE rect(0, 0, W, W, BG[1], BG[2], BG[3]) if f >= 504 and f < 600 then local wig = (f >= 580 and f < 598) and ((f // 3) % 2 == 0 and -2 or 2) or 0 body(8 + wig, 8, 48, 2, CUBE) local gx = f < 510 and 0 or (f < 530 and 5 or (f < 550 and -5 or 0)) eyes(8 + wig, 8, gx, 0, f - 559, BIG_EYE, BLINK_B) return end local jumper = {} for k = 1, #JUMPS do jumper[k] = umod(hash32((cyc * 7 + k) * 0x9E3779B1), N) + 1 end local target = umod(hash32((cyc * 7 + 5) * 0x85EBCA6B), N) + 1 local stare = f >= 320 and f < 430 local run = f >= 470 and f < 640 for i = 1, N do local x, y, lift, acc, cut = HX[i], HY[i], 0, false, RN local h = hash32(i * 0xC2B2AE35) local per = 45 + umod(h, 66) local gz = GAZE[umod(hash32(((frame + umod(h >> 8, 200)) // per) * 0x9E3779B1 + i), #GAZE) + 1] local gx, gy = gz[1] * 2, gz[2] * 2 local k = (frame + umod(h >> 4, 160)) % (80 + umod(h >> 16, 90)) + 1 if stare then k = (f >= 400 and f < 406) and f - 399 or 0 if i == target then gx, gy = 0, 0 acc = f >= 345 and f < 395 lift = lift_at(f, 350, 16, 6) + lift_at(f, 378, 16, 6) else gx, gy = sign(HX[target] - x) * 2, sign(HY[target] - y) * 2 end else for j = 1, #JUMPS do if jumper[j] == i then lift = lift_at(f, JUMPS[j], 18, 8) end end end if run then local a if f < 604 then a = clamp01((f - 470 - RANK[i]) / 14) else a = clamp01(1 - (f - 604 - RANK[i]) / 14) end local tx, ty = CX, CY if f >= 604 then tx, ty = HX, HY end x = HX[i] + (CX[i] - HX[i]) * ease(a) y = HY[i] + (CY[i] - HY[i]) * ease(a) - 20 * a * (1 - a) gx, gy = sign(tx[i] - x) * 2, sign(ty[i] - y) * 2 if a > 0 and a < 1 then k = 0 end cut = floor(RN * (1 - ease(a)) + 0.5) elseif lift > 0.5 then blend_rect(x + 2, y + S, S - 4, 1, CUBE[1], CUBE[2], CUBE[3], 0.25) end body(x, y - lift, S, cut, acc and ACC or CUBE) eyes(x, y - lift, gx, gy, k, EY, HS) end end