info = { type = "image", title = "Fox in a box", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fox peeks from a box and leaps in and out", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Jump out"] = "Vyskočí", ["Box"] = "Krabica", ["Kraft"] = "Kartón", ["White"] = "Biela", ["Blue"] = "Modrá" }, cs = { ["Speed"] = "Rychlost", ["Jump out"] = "Vyskočí", ["Box"] = "Krabice", ["Kraft"] = "Karton", ["White"] = "Bílá", ["Blue"] = "Modrá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "jump", label = T("Jump out"), type = "bool", default = true }, { key = "box", label = T("Box"), type = "int", default = "kraft", opts = { keys = { "kraft", "white", "blue" }, T("Kraft"), T("White"), T("Blue") } }, } local rect, blend_rect, blit_rgb = gfx.rect, gfx.blend_rect, gfx.blit_rgb local floor, sin, exp, pi = math.floor, math.sin, math.exp, math.pi local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function R(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local BOXES = { kraft = { face = { 230, 176, 126 }, side = { 196, 138, 98 }, flap = { 246, 208, 160 }, inner = { 148, 96, 66 }, edge = { 206, 150, 104 }, tape = { 242, 226, 192 } }, white = { face = { 238, 238, 244 }, side = { 188, 190, 214 }, flap = { 252, 252, 255 }, inner = { 132, 136, 172 }, edge = { 206, 208, 228 }, tape = { 150, 170, 236 } }, blue = { face = { 110, 156, 232 }, side = { 70, 108, 190 }, flap = { 150, 190, 250 }, inner = { 46, 74, 150 }, edge = { 88, 132, 214 }, tape = { 236, 240, 255 } }, } local WALL, BASEB = { 168, 214, 228 }, { 238, 238, 230 } local FLOOR, PLANK, SHADOW = { 214, 164, 112 }, { 184, 132, 90 }, { 150, 104, 74 } local O, D, WH = { 238, 118, 40 }, { 176, 74, 30 }, { 255, 244, 228 } local K, L, M = { 48, 28, 26 }, { 96, 46, 30 }, { 214, 96, 34 } local FX0, FX1, FY, FB = 9, 36, 35, 53 local BG local function build_bg() R(0, 0, W, 38, WALL) R(44, 5, 15, 17, { 255, 255, 255 }) R(46, 7, 11, 13, { 204, 236, 250 }) R(51, 7, 1, 13, { 255, 255, 255 }); R(46, 13, 11, 1, { 255, 255, 255 }) R(43, 22, 17, 2, { 226, 226, 220 }) R(0, 38, W, 3, BASEB) R(0, 41, W, 23, FLOOR) for k, y in ipairs({ 46, 52, 58 }) do R(0, y, W, 1, PLANK) for x = (k * 17) % 23, W - 1, 23 do R(x, y - 5, 1, 5, PLANK) end end R(0, 63, W, 1, PLANK) R(7, FB, FX1 - FX0 + 8, 3, SHADOW) BG = gfx.snapshot(0, 0, W, H) end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function head(hx, hy, look, blink) R(hx + 1, hy + 1, 3, 3, O); R(hx + 9, hy + 1, 3, 3, O) rect(hx + 2, hy, 1, 1, O[1], O[2], O[3]); rect(hx + 10, hy, 1, 1, O[1], O[2], O[3]) R(hx + 2, hy + 2, 1, 2, D); R(hx + 10, hy + 2, 1, 2, D) R(hx + 1, hy + 3, 11, 1, O) R(hx, hy + 4, 13, 4, O) R(hx + 1, hy + 8, 11, 6, O) R(hx, hy + 6, 3, 2, WH); R(hx + 10, hy + 6, 3, 2, WH) R(hx + 4, hy + 6, 5, 3, WH) R(hx + 6, hy + 6, 1, 1, K) if blink then R(hx + 2 + look, hy + 5, 2, 1, D); R(hx + 9 + look, hy + 5, 2, 1, D) else R(hx + 3 + look, hy + 4, 1, 2, K); R(hx + 9 + look, hy + 4, 1, 2, K) end R(hx + 5, hy + 3, 3, 1, M) end local function tail(ox, sw) local x = 29 + ox R(x, 33, 5, 1, O); R(x + 1, 32, 5, 1, O); R(x + 2, 31, 5, 1, O) x = x + sw R(x + 3, 30, 5, 1, O); R(x + 3, 29, 5, 1, O) R(x + 4, 28, 4, 1, WH); R(x + 4, 27, 4, 1, WH); R(x + 5, 26, 2, 1, WH) end local function side_fox(x, y, dir, air, wag) x, y = floor(x + 0.5), floor(y + 0.5) local function r(dx, dy, w, h, c) R(dir > 0 and x + dx or x + 24 - dx - w, y + dy, w, h, c) end if air then r(2, 8, 6, 2, L); r(5, 10, 4, 2, L); r(17, 8, 6, 2, L); r(15, 10, 4, 2, L) r(0, 4, 6, 2, O); r(0, 4, 2, 2, WH) else r(7, 9, 2, 4, L); r(10, 9, 2, 4, L); r(15, 9, 2, 4, L); r(18, 9, 2, 4, L) r(1 + wag, 5, 5, 3, O); r(wag, 6, 2, 3, WH) end r(6, 3, 11, 6, O); r(6, 8, 11, 1, D); r(7, 4, 3, 3, M) r(14, 3, 3, 4, O); r(15, 5, 3, 4, WH) r(16, 2, 6, 5, O); r(21, 5, 3, 2, WH); r(23, 4, 1, 1, K); r(17, 6, 3, 1, WH) r(17, 0, 2, 3, O); r(20, 0, 2, 3, O); r(18, 1, 1, 2, D); r(21, 1, 1, 2, D) r(20, 3, 1, 1, K) if not air then blend_rect(x + 3, y + 13, 18, 1, 120, 80, 50, 0.4) end end local function peek(u) if u < 0 or u > 5.5 then return 0, 0, false end local p = u < 1 and ease(u) or (u < 4.5 and 1 or 1 - ease(u - 4.5)) local look = (u >= 3 and u < 3.8) and 1 or ((u >= 3.8 and u < 4.6) and -1 or 0) local blink = (u > 2.55 and u < 2.7) or (u > 4.7 and u < 4.85) or (u > 3.4 and u < 3.5) return p, look, blink end local function wobble(u, a) if u < 0 or u > 1.6 then return 0 end return floor(a * sin(u * 6 * pi) * exp(-u * 2.2) + 0.5) end local T_OUT, T_BACK, T_END = 9.5, 12.0, 12.9 local PERIOD = 16 function render() if not BG then build_bg() end blit_rgb(BG, 0, 0, W, H) local B = BOXES[cfg.box] or BOXES.kraft local jump = cfg.jump ~= false local t = (frame / FPS * (0.6 + 0.2 * (cfg.speed or 3))) % PERIOD local out = jump and t >= T_OUT and t < T_END local ox = wobble(t - 8, 2) + (jump and wobble(t - T_END, 1) or 0) local p, look, blink if t < 6.5 then p, look, blink = peek(t - 1) elseif not jump then p, look, blink = peek(t - T_OUT) else p, look, blink = 0, 0, false if t >= 13.2 and t < 14.8 then p = 0.55 * sin((t - 13.2) / 1.6 * pi) end end R(FX0 + 2 + ox, 30, FX1 - FX0 - 3, 5, B.inner) R(FX0 + 2 + ox, 29, FX1 - FX0 - 3, 1, B.edge) if not out then if p > 0 then head(15 + ox + look * 2, floor(36 - 12 * p + 0.5), look, blink) end tail(ox, floor(1.5 * sin(t * 2.5) + 0.5)) end local fx, fy, fdir, fair, early if out then local s = (t - T_OUT) / 0.9 if s < 1 then fx, fy = 14 + 24 * s, 27 + 16 * s - 64 * s * (1 - s) fdir, fair, early = 1, true, s < 0.2 elseif t < T_BACK then fx, fy, fdir, fair = 38, 43, 1, false end if t >= T_BACK - 0.3 and t < T_BACK then fdir = -1 end if t >= T_BACK then s = (t - T_BACK) / 0.9 fx, fy = 38 - 26 * s, 43 - 7 * s - 62 * s * (1 - s) fdir, fair, early = -1, true, s > 0.7 end if t < T_BACK and t >= T_OUT + 0.9 then fair = false end end if fx and early then side_fox(fx + ox, fy, fdir, fair, 0) end R(FX0 + ox, FY, FX1 - FX0 + 1, FB - FY, B.face) R(FX0 + ox, FY, FX1 - FX0 + 1, 1, B.flap) R(FX0 + ox + 12, FY + 1, 4, 6, B.tape) R(FX0 + ox, 45, FX1 - FX0 + 1, 1, B.edge) R(FX0 + ox, FB - 1, FX1 - FX0 + 1, 1, B.side) for c = 0, 3 do R(FX1 + 1 + c + ox, FY - c, 1, FB - FY + 1 - c * 0, B.side) end for i = 0, 5 do R(FX0 - 1 - floor(i * 0.7) + ox, 34 - i, 4, 1, B.flap) R(FX1 - 2 + floor(i * 0.7) + ox, 34 - i, 4, 1, B.flap) end R(FX0 - 4 + ox, 28, 2, 1, B.edge); R(FX1 + 3 + ox, 28, 2, 1, B.edge) if not out and p > 0.95 then local hx = 15 + ox + look * 2 R(hx + 2, 33, 3, 3, O); R(hx + 9, 33, 3, 3, O) R(hx + 2, 35, 3, 1, WH); R(hx + 9, 35, 3, 1, WH) end if fx and not early then side_fox(fx, fy, fdir, fair, floor(sin(t * 6) + 0.5)) end end