info = { type = "image", title = "Hallway pup", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A pup runs out of a door down a hallway", api = 33, } local tr = ({ sk = { ["Coat"] = "Srsť", ["Speed"] = "Rýchlosť" }, cs = { ["Coat"] = "Srst", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "coat", label = T("Coat"), type = "color", default = "#D6A064" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect, blit_rgb = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local W, H = gfx.W, gfx.H local VPX = 32 local RUN = { 150, 110, 80 } local DOOR_AT, DOOR_STEP = 30, 8 local GO = DOOR_AT + 4 * DOOR_STEP local BG = nil local function sh(c, d) local k = 0.55 + 0.45 * d return floor(c[1] * k), floor(c[2] * k), floor(c[3] * k) end local PAINT, WAIN, RAIL = { 92, 150, 158 }, { 56, 98, 112 }, { 186, 206, 200 } local CEIL, PLANK, PLANK_L = { 172, 162, 198 }, { 160, 112, 74 }, { 112, 76, 50 } local RUNNER, RUNNER_E = { 178, 66, 86 }, { 232, 190, 120 } local function wall_col(x, d, top, bot) local yw = bot - floor((bot - top) * 0.38) rect(x, top, 1, yw - top, sh(PAINT, d)) rect(x, yw, 1, 1, sh(RAIL, d)) rect(x, yw + 1, 1, bot - yw, sh(WAIN, d)) end local function build_bg() for y = 0, 11 do local xl = floor(y / 0.6) rect(xl, y, W - 2 * xl, 1, sh(CEIL, y / 12)) end for y = 41, H - 1 do local d = 1 - (y - 41) / 22 local xl = floor(20 * (H - 1 - y) / 22 + 0.5) rect(xl, y, W - 2 * xl, 1, sh(PLANK, d)) local r = floor(5 + (y - 41) * 0.42) rect(VPX - r, y, 2 * r, 1, sh(RUNNER, d)) rect(VPX - r, y, 1, 1, sh(RUNNER_E, d)); rect(VPX + r - 1, y, 1, 1, sh(RUNNER_E, d)) end for k = 0, 6 do local y = floor(41 + 22 / (1 + 0.75 * k) + 0.5) if y > 41 and y < H then local xl = floor(20 * (H - 1 - y) / 22 + 0.5) local d = 1 - (y - 41) / 22 rect(xl, y, W - 2 * xl, 1, sh(PLANK_L, d)) rect(VPX - floor(5 + (y - 41) * 0.42), y, 2 * floor(5 + (y - 41) * 0.42), 1, sh({ 140, 50, 70 }, d)) end end for x = 0, 19 do local d = (x + 0.5) / 20 local top, bot = floor(0.6 * x + 0.5), H - 1 - floor(1.1 * x + 0.5) wall_col(x, d, top, bot) wall_col(W - 1 - x, d, top, bot) end rect(20, 12, 24, 30, sh(PAINT, 1)) rect(20, 33, 24, 9, sh(WAIN, 1)); rect(20, 32, 24, 1, sh(RAIL, 1)) rect(26, 19, 12, 22, 212, 186, 128) rect(26, 19, 12, 1, 238, 214, 160) for _, d in ipairs({ 0.18, 0.5, 0.78 }) do local y, w = floor(12 * d + 0.5), floor(11 - 8 * d + 0.5) rect(VPX - w // 2, y, w, 1 + (d < 0.4 and 1 or 0), 255, 240, 190) blend_rect(VPX - w // 2 - 1, y + 1, w + 2, 1, 255, 240, 190, 0.18) local x = floor(d * 20) local top, bot = floor(0.6 * x + 0.5), H - 1 - floor(1.1 * x + 0.5) local yy = top + floor((bot - top) * 0.3) local lw = max(1, floor(3 - 2 * d)) rect(x, yy, lw, 3, 255, 232, 150); rect(W - x - lw, yy, lw, 3, 255, 232, 150) blend_rect(x, yy - 1, lw + 3, 5, 255, 220, 140, 0.18); blend_rect(W - x - lw - 3, yy - 1, lw + 3, 5, 255, 220, 140, 0.18) end BG = gfx.snapshot(0, 0, W, H) end local LEAF_W = { 10, 7, 4, 2, 0 } local LEAF_E = { 0, 1, 2, 3, 0 } local function draw_door(k) if k >= 1 then rect(27, 20, 10, 8, 255, 244, 196) rect(27, 28, 10, 7, 255, 226, 152) rect(27, 35, 10, 6, 240, 192, 112) for y = 41, H - 1 do local hw = 5 + floor((y - 41) * 0.8) blend_rect(VPX - hw, y, 2 * hw, 1, 255, 236, 170, 0.07 + 0.02 * k) end end if k == 4 then rect(27, 21, 1, 19, 90, 56, 36) return end local w, e = LEAF_W[k + 1], LEAF_E[k + 1] for c = 0, w - 1 do local off = floor(e * c / w + 0.5) rect(27 + c, 20 + off, 1, 21 - 2 * off, c == w - 1 and 96 or 126, c == w - 1 and 60 or 80, c == w - 1 and 38 or 50) end if k == 0 then rect(29, 23, 6, 6, 148, 98, 62); rect(29, 31, 6, 7, 148, 98, 62) px(35, 31, 255, 224, 110) end end local function pup(cx, by, s, pose, coat) local cr, cg, cb = coat.r, coat.g, coat.b local dr, dg, db = cr * 7 // 10, cg * 7 // 10, cb * 7 // 10 local er, eg, eb = cr // 2, cg // 2, cb // 2 local lift = (pose == 1) and floor(s * 0.6 + 0.4) or 0 by = by - lift local function part(gx, gy, gw, gh, r, g, b) local x0 = cx + floor((gx - 6) * s + 0.5) local x1 = cx + floor((gx + gw - 6) * s + 0.5) local y0 = by + floor((gy - 14) * s + 0.5) local y1 = by + floor((gy + gh - 14) * s + 0.5) if x1 <= x0 then x1 = x0 + 1 end if y1 <= y0 then y1 = y0 + 1 end rect(x0, y0, x1 - x0, y1 - y0, r, g, b) end blend_rect(cx - floor(5.5 * s + 0.5), by + lift - floor(0.4 * s), floor(11 * s + 0.5), max(1, floor(0.9 * s + 0.5)), 0, 0, 0, 0.28) part(1, 12, 2, 2, dr, dg, db); part(9, 12, 2, 2, dr, dg, db) part(2, 7, 8, 5, cr, cg, cb); part(4, 7, 4, 4, 244, 226, 196) if pose == 0 then part(3, 11, 2, 3, cr, cg, cb); part(3, 13, 2, 1, 244, 226, 196); part(7, 10, 2, 2, cr, cg, cb) else part(7, 11, 2, 3, cr, cg, cb); part(7, 13, 2, 1, 244, 226, 196); part(3, 10, 2, 2, cr, cg, cb) end part(0, 1, 2, 5, er, eg, eb); part(10, 1, 2, 5, er, eg, eb) part(2, 0, 8, 7, cr, cg, cb) part(4, 3, 4, 4, 244, 226, 196) part(5, 3, 2, 1, 30, 24, 28) part(3, 2, 1, 1, 30, 24, 28); part(8, 2, 1, 1, 30, 24, 28) if pose == 0 then part(5, 6, 2, 2, 240, 120, 140) end end function render() if not BG then build_bg() end local coat = cfg.coat or { r = 214, g = 160, b = 100 } local run = RUN[cfg.speed or 2] local cyc = GO + run + 70 local t = frame % cyc blit_rgb(BG, 0, 0, W, H) local k if t < DOOR_AT then k = 0 elseif t < GO then k = 1 + (t - DOOR_AT) // DOOR_STEP elseif t < GO + run + 8 then k = 4 else k = max(0, 4 - (t - GO - run - 8) // 8) end draw_door(k) if t >= GO and t < GO + run then local tau = 0.8 * (t - GO) / run local u = 1 / (1 - tau) local wob = 0.6 * sin(t * 0.09) local cx = floor(VPX + (1 + wob) * u + 0.5) local by = floor(26 + 15 * u + 0.5) pup(cx, by, u * 0.5, (frame // 4) % 2, coat) end end