info = { type = "image", title = "Mochi", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "A soft mochi hops, squashes and sends hearts", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Sky"] = "Obloha", ["Hop speed"] = "Rýchlosť skákania", ["Hearts"] = "Srdiečka" }, cs = { ["Sky"] = "Obloha", ["Hop speed"] = "Rychlost skákání", ["Hearts"] = "Srdíčka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky"), type = "color", default = "#A9DBF5" }, { key = "hop", label = T("Hop speed"), type = "int", min = 1, max = 3, default = 2 }, { key = "hearts", label = T("Hearts"), type = "bool", default = true }, } local floor, sin, abs = math.floor, math.sin, math.abs local HOP = { 54, 36, 26 } local OUTC = { 107, 102, 136 } local WHITE = { 255, 255, 255 } local SHADEC = { 226, 222, 240 } local CHEEKC = { 255, 179, 199 } local EYEC = { 43, 37, 64 } local HEARTC = { 255, 92, 134 } local PINK_BAND = { 248, 204, 228 } local MINT, MINT_D = { 191, 235, 201 }, { 150, 214, 178 } local PAL = { 0, 0, 0, OUTC[1], OUTC[2], OUTC[3], WHITE[1], WHITE[2], WHITE[3], SHADEC[1], SHADEC[2], SHADEC[3], CHEEKC[1], CHEEKC[2], CHEEKC[3], EYEC[1], EYEC[2], EYEC[3], HEARTC[1], HEARTC[2], HEARTC[3] } local BODY_OPEN, BODY_BLINK, HEART, BG local function pack(w, h, get) local rows = {} for y = 0, h - 1 do local row = {} for x = 0, w - 1 do row[x + 1] = get(x, y) end rows[y + 1] = string.char(table.unpack(row, 1, w)) end local cells = table.concat(rows) return { cells = cells, mask = (cells:gsub("[\1-\255]", "\255")), w = w, h = h } end local function shape(w, h, inside, paint) local ins = {} for y = 0, h - 1 do local row = {} for x = 0, w - 1 do row[x] = inside(x, y) end ins[y] = row end local function at(x, y) return x >= 0 and x < w and y >= 0 and y < h and ins[y][x] end return pack(w, h, function(x, y) if not ins[y][x] then return 0 end if at(x - 1, y) and at(x + 1, y) and at(x, y - 1) and at(x, y + 1) then return paint(x, y) end return 1 end) end local function put(s, fx, fy, w, h) w, h = w or s.w, h or s.h local ix, iy = floor(fx), floor(fy) local ax, ay = fx - ix, fy - iy if ax > 0.05 then gfx.blend_mask(s.mask, OUTC[1], OUTC[2], OUTC[3], ix + 1, iy, w, h, s.w, s.h, ax) end if ay > 0.05 then gfx.blend_mask(s.mask, OUTC[1], OUTC[2], OUTC[3], ix, iy + 1, w, h, s.w, s.h, ay) end gfx.blit_indexed(s.cells, PAL, ix, iy, w, h, s.w, s.h, true) return ix, iy end local function body_in(x, y) local dx, dy = (x + 0.5 - 12) / 11.5, (y + 0.5 - 11) / 10.5 return dx * dx + dy * dy <= 1 end local function face(blink) return function(x, y) if y >= 13 and y <= 14 and ((x >= 4 and x <= 5) or (x >= 18 and x <= 19)) then return 4 end if (x >= 7 and x <= 8) or (x >= 15 and x <= 16) then if blink then if y == 10 then return 5 end elseif y >= 9 and y <= 10 then return 5 end end if (y == 13 and (x == 10 or x == 13)) or (y == 14 and (x == 11 or x == 12)) then return 5 end if x + y >= 27 then return 3 end return 2 end end local HEART_ROWS = { ".##.##.", "#######", "#######", ".#####.", "..###..", "...#..." } local function setup() local s = cfg.sky or { r = 169, g = 219, b = 245 } gfx.rect(0, 0, 64, 64, s.r, s.g, s.b) gfx.rect(0, 30, 64, 20, PINK_BAND[1], PINK_BAND[2], PINK_BAND[3]) gfx.rect(0, 50, 64, 14, MINT[1], MINT[2], MINT[3]) gfx.rect(0, 50, 64, 1, MINT_D[1], MINT_D[2], MINT_D[3]) gfx.rect(5, 9, 14, 3, 255, 255, 255) gfx.rect(8, 6, 7, 3, 255, 255, 255) gfx.rect(42, 14, 16, 3, 255, 255, 255) gfx.rect(46, 11, 8, 3, 255, 255, 255) BG = gfx.snapshot(0, 0, 64, 64) end function on_settings() BG = nil end function render() FR = frame * STEP_HZ // gfx.FPS if not BG then setup() end if not BODY_OPEN then BODY_OPEN = shape(24, 22, body_in, face(false)) return end if not BODY_BLINK then BODY_BLINK = shape(24, 22, body_in, face(true)) HEART = pack(7, 6, function(x, y) return HEART_ROWS[y + 1]:sub(x + 1, x + 1) == "#" and 6 or 0 end) return end local t = FR local P = HOP[cfg.hop or 2] or 36 local u = (t % P) / P local d = u < 0.5 and u or 1 - u local c = d < 0.12 and (1 - d / 0.12) or 0 local air = (1 - c) * abs(1 - 2 * u) * 0.35 local jump = 4 * u * (1 - u) * 18 local w = floor(24 * (1 - air * 0.5) + 6 * c + 0.5) local h = floor(22 * (1 + air) - 8 * c + 0.5) local fx = 32 - w / 2 local fy = 52 - h - jump gfx.blit_rgb(BG, 0, 0, 64, 64) local sw = floor(26 - jump * 0.5) gfx.blend_rect(floor(32 - sw / 2), 52, sw, 1, 40, 110, 80, 0.35) local blink = t % 110 < 3 put(blink and BODY_BLINK or BODY_OPEN, fx, fy, w, h) if cfg.hearts ~= false then local age = t % 90 if age < 60 then put(HEART, 36 + 2 * sin(age * 0.15), 22 - age * 0.3) end end end