info = { type = "image", title = "Ladybug climb", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Ladybug climbs a grass blade and flies off", api = 44, } local tr = ({ sk = { ["Ladybug"] = "Lienka", ["Red"] = "Červená", ["Orange"] = "Oranžová", ["Yellow"] = "Žltá", ["Climb speed"] = "Rýchlosť lezenia", ["Breeze"] = "Vánok" }, cs = { ["Ladybug"] = "Beruška", ["Red"] = "Červená", ["Orange"] = "Oranžová", ["Yellow"] = "Žlutá", ["Climb speed"] = "Rychlost lezení", ["Breeze"] = "Vánek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bug", label = T("Ladybug"), type = "int", default = "red", opts = { keys = { "red", "orange", "yellow" }, T("Red"), T("Orange"), T("Yellow") } }, { key = "speed", label = T("Climb speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "breeze", label = T("Breeze"), type = "int", min = 0, max = 3, default = 1 }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local floor, ceil, sin, cos, sqrt, exp = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.exp local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local YB, L, X0, NAT = 66, 54, 22, 9 local DT = 9 local C = DT / (2 * L * L * L) local ST = L - 6 local Q0 = 2 * ST / (3 * L - ST) local PAL = { red = { { 214, 36, 30 }, { 255, 150, 130 }, { 150, 20, 22 } }, orange = { { 238, 112, 26 }, { 255, 196, 120 }, { 170, 70, 18 } }, yellow = { { 244, 198, 40 }, { 255, 236, 150 }, { 196, 140, 24 } }, } local COL = { {}, { 24, 16, 22 }, { 236, 236, 226 }, {}, { 214, 222, 232 }, {} } local function cat(...) local o = {} for _, l in ipairs({ ... }) do for i = 1, #l do o[#o + 1] = l[i] end end return o end local HEAD = { { 2, 0, 3, 1, 2 }, { 1, 1, 5, 1, 2 }, { 1, 1, 1, 1, 3 }, { 5, 1, 1, 1, 3 } } local ANT = { { 1, -1, 1, 1, 2 }, { 5, -1, 1, 1, 2 } } local BODY = { { 0, 2, 7, 4, 1 }, { 1, 6, 5, 1, 1 }, { 2, 7, 3, 1, 1 }, { 3, 2, 1, 5, 2 }, { 1, 2, 1, 1, 4 }, { 5, 5, 1, 1, 6 }, { 4, 6, 1, 1, 6 } } local SPOTS = { { { 1, 3, 2, 2, 2 }, { 4, 3, 2, 2, 2 } }, { { 1, 3, 1, 1, 2 }, { 2, 5, 1, 1, 2 }, { 5, 3, 1, 1, 2 }, { 4, 5, 1, 1, 2 } } } local LEGS = { { { -1, 2, 1, 1, 2 }, { 7, 3, 1, 1, 2 }, { -1, 5, 1, 1, 2 }, { 7, 6, 1, 1, 2 } }, { { -1, 3, 1, 1, 2 }, { 7, 2, 1, 1, 2 }, { -1, 6, 1, 1, 2 }, { 7, 5, 1, 1, 2 } } } local CLOSED = {} for sset = 1, 2 do CLOSED[sset] = {} for lg = 1, 2 do CLOSED[sset][lg] = cat(BODY, SPOTS[sset], LEGS[lg], HEAD, ANT) end end local ABD = { { 2, 2, 3, 5, 2 }, { 2, 7, 3, 1, 5 } } local OPEN1 = cat({ { -1, 2, 2, 5, 1 }, { 6, 2, 2, 5, 1 } }, ABD, LEGS[1], HEAD, ANT) local OPEN2 = cat({ { -6, 2, 5, 2, 5 }, { -5, 4, 4, 1, 5 }, { 8, 2, 5, 2, 5 }, { 8, 4, 4, 1, 5 }, { -2, 0, 2, 5, 1 }, { 7, 0, 2, 5, 1 } }, ABD, HEAD, ANT) local FLY1 = cat({ { -5, -4, 4, 6, 5 }, { 8, -4, 4, 6, 5 }, { -2, 1, 2, 4, 1 }, { 7, 1, 2, 4, 1 } }, ABD, HEAD) local FLY2 = cat({ { -7, 2, 6, 2, 5 }, { 8, 2, 6, 2, 5 }, { -2, 1, 2, 4, 1 }, { 7, 1, 2, 4, 1 } }, ABD, HEAD) local function draw_pose(pose, x, y, a) for i = 1, #pose do local p = pose[i] local c = COL[p[5]] if a >= 1 then rect(x + p[1], y + p[2], p[3], p[4], c[1], c[2], c[3]) else blend_rect(x + p[1], y + p[2], p[3], p[4], c[1], c[2], c[3], a) end end end local function bug(pose, bx, by) local ix, iy = floor(bx + 0.5), floor(by) local f = by - iy if f < 0.08 then draw_pose(pose, ix, iy, 1) elseif f > 0.92 then draw_pose(pose, ix, iy + 1, 1) else draw_pose(pose, ix, iy, 1 - f); draw_pose(pose, ix, iy + 1, f) end end local BG = nil local function lerp(a, b, t) return a + (b - a) * t end local STAGE = 0 local BLADES = { { 6, 0.12, 9, 0.22, 120, 176, 56 }, { 44, -0.10, 8, 0.2, 168, 212, 90 }, { 52, 0.08, 10, 0.24, 70, 140, 44 }, { 12, -0.06, 7, 0.18, 188, 220, 100 } } local function soft_blade(b) for y = 0, H - 1 do local cx = b[1] + b[2] * y blend_rect(floor(cx - b[3] / 2), y, b[3], 1, b[5], b[6], b[7], b[4]) blend_rect(floor(cx - b[3] / 4), y, b[3] // 2, 1, b[5], b[6], b[7], b[4]) end end local function build_bg(stage) if stage > 1 then gfx.blit_rgb(BG, 0, 0, W, H) end if stage == 1 then for y = 0, H - 1 do local t = y / (H - 1) local r, g, b if t < 0.45 then local k = t / 0.45 r, g, b = lerp(214, 134, k), lerp(230, 188, k), lerp(130, 72, k) else local k = (t - 0.45) / 0.55 r, g, b = lerp(134, 40, k), lerp(188, 98, k), lerp(72, 38, k) end rect(0, y, W, 1, floor(r), floor(g), floor(b)) end soft_blade(BLADES[1]) elseif stage == 2 then for i = 2, 4 do soft_blade(BLADES[i]) end elseif stage == 3 then for i = 1, 16 do local h = hash32(i * 0x9E3779B1) local x, y, r = umod(h, W), umod(h >> 8, 56), 2 + umod(h >> 16, 4) local lite = y < 30 for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) if lite then blend_rect(x - hw, y + dy, 2 * hw, 1, 255, 250, 190, 0.2) else blend_rect(x - hw, y + dy, 2 * hw, 1, 150, 210, 80, 0.12) end end end else for y = 0, H - 1 do local lx = 3 + 0.05 * (63 - y) for k = 0, 2 do blend_rect(-6, y, floor(lx + 3 - k * 2) + 6, 1, 20, 66, 24, 0.4) blend_rect(floor(W - 2 - 0.04 * (63 - y) + k * 2), y, 12, 1, 22, 70, 26, 0.4) end end end BG = gfx.snapshot(0, 0, W, H) end local D, CYC = 270, 470 local BUG = PAL.red function on_settings() D = 420 - 50 * (cfg.speed or 3) CYC = D + 200 BUG = PAL[cfg.bug or "red"] or PAL.red COL[1], COL[4], COL[6] = BUG[1], BUG[2], BUG[3] end local function blade(s, q, sway, t) if s and s < 0 then s = 0 end for y = 63, YB - L, -1 do local h = YB - y - 0.5 local k = h / L local d if s then if h <= s then d = C * h * h * (3 * s - h) else d = C * s * s * (3 * h - s) end else d = q * C * h * h * (3 * L - h) end local cx = X0 + NAT * k * k + d + sway * k * k * sin(t * 1.9 + k * 1.5) local hw = 0.7 + 5.7 * (1 - k ^ 1.2) local xl, xr = cx - hw, cx + hw local il, ir = ceil(xl), floor(xr) local n = ir - il local r, g, b = floor(34 + 70 * k), floor(98 + 78 * k), floor(34 + 18 * k) local dr, dg, db = floor(r * 0.6), floor(g * 0.6), floor(b * 0.6) if n >= 2 then rect(il, y, n, 1, r, g, b) rect(il, y, 1, 1, dr, dg, db) rect(il + floor(n * 0.6), y, 1, 1, r + 36, g + 36, b + 22) end rect(ir - 1, y, 1, 1, dr, dg, db) local a = il - xl if a > 0.05 then blend(il - 1, y, dr, dg, db, a * 0.7) end a = xr - ir if a > 0.05 then blend(ir, y, dr, dg, db, a * 0.7) end end end local function blade_x(h, s, q, sway, t) local k = h / L local d if s then s = s > 0 and s or 0 if h <= s then d = C * h * h * (3 * s - h) else d = C * s * s * (3 * h - s) end else d = q * C * h * h * (3 * L - h) end return X0 + NAT * k * k + d + sway * k * k * sin(t * 1.9 + k * 1.5) end local function pollen(t) for i = 1, 5 do local h = hash32(i * 0x85EBCA6B) local x = (umod(h, 80) + t * (1.2 + umod(h >> 8, 8) / 10)) % (W + 16) - 8 local y = (umod(h >> 12, 70) - t * (0.7 + umod(h >> 16, 6) / 10)) % (H + 12) - 6 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 255, 252, 214, 0.7 * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 255, 252, 214, 0.7 * fx * (1 - fy)) blend(ix, iy + 1, 255, 252, 214, 0.7 * (1 - fx) * fy) blend(ix + 1, iy + 1, 255, 252, 214, 0.7 * fx * fy) end end function render() if STAGE < 4 then STAGE = STAGE + 1; build_bg(STAGE) if STAGE < 4 then gfx.ready(false) end end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local u, n = frame % CYC, frame // CYC local sset = n % 2 + 1 local amp = (cfg.breeze or 1) * 0.5 local a = u - D if u < D then local w = u / D + 0.04 * sin(TAU * 3 * u / D) local s = -6 + (ST + 6) * w blade(s, 0, amp, t) local bx, by = blade_x(s, s, 0, amp, t) - 3.5, YB - s - 4 bug(CLOSED[sset][floor(s / 1.7) % 2 + 1], bx, by) elseif a < 60 then blade(ST, 0, amp, t) local bx, by = blade_x(ST, ST, 0, amp, t) - 3.5, YB - ST - 4 local pose = CLOSED[sset][1] if a >= 44 then pose = (frame // 2) % 2 == 0 and FLY1 or FLY2 elseif a >= 18 then pose = OPEN2 elseif a >= 10 then pose = OPEN1 end bug(pose, bx, by) else local tr_ = (a - 60) / FPS local q = Q0 * exp(-tr_ / 0.9) * (cos(TAU * tr_ / 1.25) + sin(TAU * tr_ / 1.25) * 1.25 / (0.9 * TAU)) blade(nil, q, amp, t) local tau = (a - 60) / 90 if tau < 1 then local xs, ys = blade_x(ST, ST, 0, 0, 0) - 3.5, YB - ST - 4 local bx = xs + 62 * tau ^ 1.5 local by = ys - 30 * tau ^ 1.2 + 3 * sin(tau * 25) * tau if bx < W + 8 and by > -12 then bug((frame // 2) % 2 == 0 and FLY1 or FLY2, bx, by) end end end pollen(t) end