info = { type = "image", title = "Yarn cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A small white cat catches a rolling yarn ball", api = 33, } local tr = ({ sk = { ["Cat"] = "Mačka", ["Yarn"] = "Klbko", ["Background"] = "Pozadie" }, cs = { ["Cat"] = "Kočka", ["Yarn"] = "Klubko", ["Background"] = "Pozadí" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cat", label = T("Cat"), type = "color", default = "#F4F4F8" }, { key = "yarn", label = T("Yarn"), type = "color", default = "#F2604A" }, { key = "bg", label = T("Background"), type = "color", default = "#26405A" }, } local rect, px, blend_rect, blit = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, max = math.floor, math.sin, math.cos, math.max local FPS = gfx.FPS local SPRITES = { sit = { ".......#.....#..", ".......##...##..", ".......#######..", ".......##o##o#..", ".......#######..", "........#####...", ".......#####....", ".....#######....", "....########....", "...#########....", "..######o###....", "..######o###....", "..####...###....", }, sitb = { ".......#.....#..", ".......##...##..", ".......#######..", ".......#######..", ".......#######..", "........#####...", ".......#####....", ".....#######....", "....########....", "...#########....", "..######o###....", "..######o###....", "..####...###....", }, swat = { ".......#.....#..", ".......##...##..", ".......#######..", ".......##o##o#..", ".......#######..", "........#####...", ".......#####....", ".....#######....", "....########....", "...#############", "..######o#####..", "..######o###....", "..####..........", }, front = { "....#......#....", "....##....##....", "....########....", "....#o####o#....", "....########....", ".....######.....", ".....######.....", "....########....", "....########....", "...##########...", "...##########...", "...##########...", "...####..####...", }, frontb = { "....#......#....", "....##....##....", "....########....", "....########....", "....########....", ".....######.....", ".....######.....", "....########....", "....########....", "...##########...", "...##########...", "...##########...", "...####..####...", }, crouch = { "................", "................", "................", "................", "................", "........#.....#.", "........##...##.", "..####..#######.", ".#########o##o#.", ".##############.", ".#############..", "..#######.......", "..##.....###....", }, crouch2 = { "................", "................", "................", "................", "................", "........#.....#.", "........##...##.", "...####.#######.", ".#########o##o#.", ".##############.", ".#############..", "..#######.......", "..##.....###....", }, air = { "................", "................", ".........#.....#", ".........##...##", ".........#######", ".........##o##o#", "..##############", ".##############.", "##.#######.#####", "###.............", "................", "................", "................", }, hold = { "................", "................", "................", "........#.....#.", "........##...##.", "........#######.", "........##o##o#.", ".....##########.", "....##########..", "...##########...", "..###########...", "..###########...", "..####...#####..", }, } local MAP = { ["."] = "\0", ["#"] = "\1", o = "\2" } local GRID = {} for name, rows in pairs(SPRITES) do local flipped = {} for i = 1, #rows do flipped[i] = rows[i]:reverse() end GRID[name] = { (table.concat(rows):gsub(".", MAP)), (table.concat(flipped):gsub(".", MAP)) } end local GY = 44 local P_XL, Q_XL = 8, 18 local AXP = 41 local BX_REST = 34 local TC, OFS = 330, 100 local PAL, BGC, YARN, YARN_D, YARN_L = {}, { 38, 64, 90 }, { 242, 96, 74 }, {}, {} function on_settings() local c, y, b = cfg.cat or { r = 244, g = 244, b = 248 }, cfg.yarn or { r = 242, g = 96, b = 74 }, cfg.bg or { r = 38, g = 64, b = 90 } PAL = { 0, 0, 0, c.r, c.g, c.b, b.r, b.g, b.b } BGC = { b.r, b.g, b.b } YARN = { y.r, y.g, y.b } YARN_D = { y.r * 6 // 10, y.g * 6 // 10, y.b * 6 // 10 } YARN_L = { y.r + (255 - y.r) * 4 // 10, y.g + (255 - y.g) * 4 // 10, y.b + (255 - y.b) * 4 // 10 } end local FLIP = false local function mp(p) return FLIP and AXP - p or p end local function soft_row(xl, xr, y, a, c) if FLIP then xl, xr = AXP + 1 - xr, AXP + 1 - xl end local il, ir = floor(xl), floor(xr) if ir <= il then return end if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) end blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end local BLACK = { 0, 0, 0 } local function cat(name, xl, dy) local i = FLIP and 2 or 1 local x = FLIP and AXP - xl - 15 or xl blit(GRID[name][i], PAL, x, GY - 13 + dy, 16, 13, 16, 13, true) soft_row(xl + 2, xl + 13, GY, dy < 0 and 0.12 or 0.28, BLACK) end local WAG = { { { 1, 11 }, { 0, 10 }, { 0, 9 }, { 0, 8 }, { 0, 7 } }, { { 1, 11 }, { 0, 10 }, { -1, 9 }, { -1, 8 }, { -1, 7 } }, { { 1, 11 }, { 0, 10 }, { 0, 9 }, { 1, 8 }, { 1, 7 } }, } local WAG_SEQ = { 1, 2, 1, 3 } local FLICK = { { { 13, 12 }, { 14, 11 }, { 14, 10 } }, { { 13, 12 }, { 14, 12 }, { 15, 11 } } } local TAIL_UP = { { 0, 9 }, { -1, 8 }, { -1, 7 }, { -2, 6 } } local TAIL_LOW = { { 1, 11 }, { 0, 10 }, { 0, 9 }, { 0, 8 } } local function tail(pts, xl) for i = 1, #pts do px(mp(xl + pts[i][1]), GY - 13 + pts[i][2], 244, 244, 248) end end local HW = { 1.8, 2.9, 3.35, 3.5, 3.35, 2.9, 1.8 } local function ball(cx) for k = 1, 7 do soft_row(cx - HW[k], cx + HW[k], GY - 8 + k, 1, YARN) end local th = cx / 3.5 for i = 0, 1 do local a = th + i * 1.25 local ca, sa = cos(a), sin(a) for s = -2.6, 2.7, 0.9 do px(mp(floor(cx + ca * s)), floor(GY - 3.5 + sa * s), YARN_D[1], YARN_D[2], YARN_D[3]) end end px(mp(floor(cx - 1)), GY - 6, YARN_L[1], YARN_L[2], YARN_L[3]) soft_row(cx - 3, cx + 3, GY, 0.28, BLACK) end local function strand(cx, dir) for i = 0, 4 do local x = floor(cx - dir * (4 + i)) px(mp(x), GY - 2 + floor(sin(i * 1.3 + cx * 0.4) * 0.9 + 0.5), YARN[1], YARN[2], YARN[3]) end end function render() local g = frame + OFS local t = g % TC FLIP = (g // TC) % 2 == 1 gfx.clear(BGC[1], BGC[2], BGC[3]) local bx, dir if t >= 36 and t < 106 then local s = (t - 36) / 70 bx = BX_REST + 38 * (1 - s) * (1 - s) dir = s < 0.8 and -1 or 0 elseif t >= 106 and t < 184 then bx = BX_REST if t >= 146 then bx = bx + 0.6 * sin((t - 146) * 0.6) * max(0, 1 - (t - 146) / 30) end elseif t >= 184 and t < 232 then bx, dir = BX_REST + 0.02375 * (t - 184) ^ 2, 1 end if bx and bx < 67.5 then ball(bx) if dir and dir ~= 0 then strand(bx, dir) end end if t < 24 then cat((t >= 8 and t < 11) and "frontb" or "front", P_XL, 0) if (t // 15) % 2 == 0 then tail(FLICK[1], P_XL) else tail(FLICK[2], P_XL) end elseif t < 110 then cat((t >= 70 and t < 73) and "sitb" or "sit", P_XL, 0) tail(WAG[WAG_SEQ[(t // 8) % 4 + 1]], P_XL) elseif t < 128 then cat((t // 3) % 2 == 0 and "crouch" or "crouch2", P_XL, 0) tail(TAIL_UP, P_XL) elseif t < 146 then local s = (t - 128) / 18 cat("air", P_XL + floor((Q_XL - P_XL) * s + 0.5), -floor(9 * 4 * s * (1 - s) + 0.5)) elseif t < 176 then cat("hold", Q_XL, 0) tail(TAIL_LOW, Q_XL) elseif t < 190 then cat("swat", Q_XL, 0) tail(TAIL_LOW, Q_XL) elseif t < 206 then cat("sit", Q_XL, 0) tail(WAG[WAG_SEQ[(t // 8) % 4 + 1]], Q_XL) else cat((t >= 250 and t < 253) and "frontb" or "front", Q_XL, 0) tail((t // 15) % 2 == 0 and FLICK[1] or FLICK[2], Q_XL) end end