info = { type = "image", title = "Love letter", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "An envelope opens and rains on a little heart", api = 33, } local tr = ({ sk = { ["Heart colour"] = "Farba srdca", ["Rain"] = "Dážď" }, cs = { ["Heart colour"] = "Barva srdce", ["Rain"] = "Déšť" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "heart", label = T("Heart colour"), type = "color", default = "#D6263E" }, { key = "rain", label = T("Rain"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, exp, min, max = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI = 3.14159265 local BGC = { 14, 12, 32 } local EX0, EX1, HY, EY1 = 8, 55, 34, 59 local AX, FL = 31.5, 17 local PAPER_L, PAPER, PAPER_D = { 238, 220, 178 }, { 224, 204, 160 }, { 202, 180, 136 } local SEAM, EDGE = { 150, 126, 88 }, { 252, 240, 206 } local OUTER = { 214, 194, 150 } local INSIDE = { 92, 38, 60 } local T0, P = 60, 300 local function c3(c) return c[1], c[2], c[3] end local function disc(cx, cy, r, c) local x0, y0 = floor(cx + 0.5), floor(cy + 0.5) local n = floor(r + 0.5) for dy = -n, n do local hw = floor(sqrt(max(0, r * r - dy * dy)) + 0.5) if hw > 0 or dy == 0 then rect(x0 - hw, y0 + dy, 2 * hw + 1, 1, c3(c)) end end end local BG local function build_bg() rect(0, 0, W, H, c3(BGC)) blend_rect(10, EY1 + 1, 44, 2, 2, 2, 8, 0.7) blend_rect(14, EY1 + 3, 36, 1, 2, 2, 8, 0.5) rect(EX0, HY, EX1 - EX0 + 1, EY1 - HY + 1, c3(PAPER)) local hw = (EX1 - EX0 + 1) / 2 for y = HY, EY1 do local u = (y - HY) / (EY1 - HY) local xs = floor(EX0 + (AX - EX0) * min(1, (y - HY) / 13) + 0.5) local xe = floor(EX1 + (AX - EX1) * min(1, (y - HY) / 13) + 0.5) if y <= HY + 13 then rect(xs, y, xe - xs + 1, 1, c3(INSIDE)) rect(EX0, y, xs - EX0, 1, c3(PAPER_L)) rect(xe + 1, y, EX1 - xe, 1, c3(PAPER_D)) px(xs, y, c3(SEAM)); px(xe, y, c3(SEAM)) else local t = (y - (HY + 13)) / (EY1 - HY - 13) local bl = floor(AX - (AX - EX0) * t + 0.5) local br = floor(AX + (EX1 - AX) * t + 0.5) rect(EX0, y, bl - EX0, 1, c3(PAPER_L)) rect(bl, y, br - bl + 1, 1, c3(PAPER)) rect(br + 1, y, EX1 - br, 1, c3(PAPER_D)) px(bl, y, c3(SEAM)); px(br, y, c3(SEAM)) end end rect(EX0, EY1, EX1 - EX0 + 1, 1, c3(PAPER_D)) rect(EX0, HY, 1, EY1 - HY + 1, c3(EDGE)) rect(EX1, HY, 1, EY1 - HY + 1, c3(SEAM)) BG = gfx.snapshot(0, 0, W, H) end local HEART local HR = { { 1, 4, 7, 10 }, { 0, 5, 6, 11 }, { 0, 11 }, { 0, 11 }, { 1, 10 }, { 2, 9 }, { 3, 8 }, { 4, 7 }, { 5, 6 } } local HC, HD, HL = { 214, 38, 62 }, { 150, 18, 40 }, { 255, 150, 160 } local function build_heart() local c = cfg.heart or { r = 214, g = 38, b = 62 } HC = { c.r, c.g, c.b } HD = { floor(c.r * 0.68), floor(c.g * 0.68), floor(c.b * 0.68) } HL = { c.r + (255 - c.r) * 6 // 10, c.g + (255 - c.g) * 6 // 10, c.b + (255 - c.b) * 6 // 10 } rect(0, 0, 12, 9, 255, 0, 255) for y = 1, 9 do local r = HR[y] if #r == 4 then rect(r[1], y - 1, r[2] - r[1] + 1, 1, c3(HC)); rect(r[3], y - 1, r[4] - r[3] + 1, 1, c3(HC)) else rect(r[1], y - 1, r[2] - r[1] + 1, 1, c3(HC)) end end for y = 5, 9 do local r = HR[y] rect(max(r[1], 6), y - 1, r[#r] - max(r[1], 6) + 1, 1, c3(HD)) end px(0, 2, c3(HD)); px(0, 3, c3(HD)); px(11, 2, c3(HD)); px(11, 3, c3(HD)) px(1, 1, c3(HD)); px(10, 1, c3(HD)) rect(1, 1, 3, 1, c3(HL)); rect(1, 2, 2, 1, c3(HL)) px(1, 1, 255, 236, 240) HEART = gfx.snapshot(0, 0, 12, 9) end local function smooth(u) u = max(0, min(1, u)); return u * u * (3 - 2 * u) end local function flap_angle(p) if p < 30 then return 0 end if p < 40 then return 6 * smooth((p - 30) / 10) end if p < 76 then local u = (p - 40) / 36; return 6 + 159 * (1 - (1 - u) ^ 3) end if p < 242 then return 165 + 5 * exp(-(p - 76) / 6) * sin((p - 76) * 0.55) end if p < 270 then local u = (p - 242) / 28; return 165 * (1 - u * u) end if p < 280 then local v = (p - 270) / 10; return 7 * sin(PI * v) * (1 - v) end return 0 end local function flap(a) local rad = a * PI / 180 local ya = 34 + FL * cos(rad) local yi = floor(ya + 0.5) if a < 90 then for y = HY, yi - 1 do local t = (y - HY) / max(1, yi - HY) local xs = floor(EX0 + (AX - EX0) * t + 0.5) local xe = floor(EX1 + (AX - EX1) * t + 0.5) local lit = 1 - 0.18 * t rect(xs, y, xe - xs + 1, 1, floor(OUTER[1] * lit), floor(OUTER[2] * lit), floor(OUTER[3] * lit)) px(xs, y, c3(EDGE)); px(xe, y, c3(SEAM)) end if yi > HY then px(floor(AX), yi - 1, c3(SEAM)); px(floor(AX) + 1, yi - 1, c3(SEAM)) end else for y = yi, HY - 1 do local t = (HY - y) / max(1, HY - yi) local xs = floor(EX0 + (AX - EX0) * t + 0.5) local xe = floor(EX1 + (AX - EX1) * t + 0.5) rect(xs, y, xe - xs + 1, 1, floor(44 + 52 * t), floor(58 + 62 * t), floor(112 + 70 * t)) px(xs, y, 150, 170, 220); px(xe, y, 30, 40, 84) end end end local function cloud(cx, cy, s) if s <= 0.05 then return end local function d(dx, dy, r, c) disc(cx + dx * s, cy + dy * s, max(1, r * s), c) end local MIDC, LITC, SHC = { 200, 214, 240 }, { 246, 249, 255 }, { 146, 164, 204 } d(-6, 1, 4.2, SHC); d(0, 3, 4.4, SHC); d(6, 1.5, 4.4, SHC); d(0, -2, 5.6, SHC) d(-6, 0.5, 4.2, MIDC); d(0, 2, 4.4, MIDC); d(6, 1, 4.4, MIDC); d(0, -2.5, 5.6, MIDC) rect(floor(cx - 8 * s), floor(cy + 1 * s), floor(16 * s), max(1, floor(3 * s)), c3(MIDC)) d(-1, -4, 3.4, LITC); d(-6, -1, 2.4, LITC); d(5, -0.5, 2.6, LITC) rect(floor(cx - 9 * s), floor(cy + 4 * s), floor(18 * s), 1, c3(SHC)) end local DROP_X = { 27, 29, 31, 33, 35, 36 } local function rain(t, n, amount) for i = 1, #DROP_X do if i <= n then local x = DROP_X[i] local hit = (x >= 31 and x <= 32) and 37 or 36 local len = hit - 30 local ph = (t * 1.15 + i * 2.7) % (len + 3) local y = 30 + floor(ph) if ph < len then blend_rect(x, y, 1, 2, 214, 238, 255, amount) if ph > len - 1.5 then blend(x - 1, hit - 1, 220, 240, 255, 0.7); blend(x + 1, hit - 1, 220, 240, 255, 0.7) end end end end for k = 0, 1 do local y = 37 + ((t * 0.11 + k * 3.1) % 6) local yi = floor(y) local f = y - yi local x = k == 0 and 30 or 33 blend(x, yi, 210, 235, 255, 0.9 * (1 - f)) blend(x, yi + 1, 210, 235, 255, 0.9 * f) end end function on_settings() HEART = nil end function render() if not BG then build_bg() end if not HEART then build_heart() end local t = frame * STEP_HZ // FPS local p = (t + T0) % P gfx.blit_rgb(BG, 0, 0, W, H) local a = flap_angle(p) if a > 90 then flap(a) end if a > 20 then gfx.blit_rgb(HEART, 26, 36, 12, 9, 255, 0, 255) local wet = (p > 96 and p < 222) and min(1, (p - 96) / 20, (222 - p) / 20) or 0 if wet > 0 then blend(27, 37, 255, 236, 240, 0.4 + 0.5 * sin(t * 0.2) ^ 2) end local s = smooth((p - 78) / 22) * (1 - smooth((p - 212) / 16)) local cy = 25 + 8 * (1 - smooth((p - 78) / 22)) + floor(0.7 * sin(t * 0.07) + 0.5) if s > 0 then cloud(31.5, cy, s) end if wet > 0 then rain(t, 2 + (cfg.rain or 3), 0.9 * wet) end end if a <= 90 then flap(a) end end