info = { type = "image", title = "Toast pop", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A toast pops out of the toaster and walks away", api = 44, } local tr = ({ sk = { ["Toast"] = "Hrianka", ["Light"] = "Svetlá", ["Golden"] = "Zlatá", ["Burnt"] = "Pripálená", ["Pause"] = "Pauza", ["Short"] = "Krátka", ["Normal"] = "Normálna", ["Long"] = "Dlhá" }, cs = { ["Toast"] = "Topinka", ["Light"] = "Světlá", ["Golden"] = "Zlatá", ["Burnt"] = "Připálená", ["Pause"] = "Pauza", ["Short"] = "Krátká", ["Normal"] = "Normální", ["Long"] = "Dlouhá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "toast", label = T("Toast"), type = "int", default = "golden", opts = { keys = { "light", "golden", "burnt" }, T("Light"), T("Golden"), T("Burnt") } }, { key = "pause", label = T("Pause"), type = "int", default = "normal", opts = { keys = { "short", "normal", "long" }, T("Short"), T("Normal"), T("Long") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local GROUND = 46 local SLOT_X = 12 local TOASTS = { light = { crust = { 196, 142, 84 }, ring = { 232, 204, 142 }, inner = { 244, 226, 176 }, face = { 60, 36, 28 } }, golden = { crust = { 168, 104, 44 }, ring = { 214, 160, 76 }, inner = { 236, 184, 98 }, face = { 56, 32, 24 } }, burnt = { crust = { 58, 36, 24 }, ring = { 84, 52, 30 }, inner = { 130, 84, 46 }, face = { 238, 218, 182 } }, } local C, BURNT = TOASTS.golden, false local KNOB = { 28, 28, 36 } local BLUE, BLUE_D, SHOE, ARM = { 36, 98, 160 }, { 26, 70, 122 }, { 40, 30, 38 }, { 78, 54, 40 } local TT, FT, SQ, LK, WK = 36, 30, 10, 44, 34 local RISE = 25 local G = 8 * RISE / (FT * FT) local VY = G * FT / 2 local PERIOD = TT + FT + SQ + LK + WK + 70 local LAND_X = SLOT_X + FT local P_PEEK = { arms = "down", legs = "stand", peek = true } local P_AIR = { arms = "up", legs = "air", mouth = "o" } local P_SQUAT = { arms = "down", legs = "squat", drop = 2, mouth = "flat" } local P_STAND = { arms = "down", legs = "stand", mouth = "smile" } local look = { arms = "down", legs = "stand", mouth = "smile" } local function state(t) if t < 0 then return end if t < TT then return SLOT_X, GROUND + 3, P_PEEK end local u = t - TT if u < FT then return SLOT_X + u, floor(GROUND - VY * u + G * u * u / 2 + 0.5), P_AIR end u = u - FT if u < SQ then return LAND_X, GROUND, u < 6 and P_SQUAT or P_STAND end u = u - SQ if u < LK then look.dx = (u >= 10 and u < 24) and -1 or ((u >= 24 and u < 38) and 1 or 0) look.blink = (u >= 5 and u < 7) or (u >= 40 and u < 42) return LAND_X, GROUND, look end u = u - LK if u < WK then local k = (u // 4) % 4 return LAND_X + u, GROUND, { arms = (k == 0 or k == 3) and "swingL" or "swingR", legs = k == 0 and "walkA" or (k == 2 and "walkB" or "mid"), mouth = "smile" } end end local BG, FRONT, PART local TX0, TY0, TW = 2, 36, 22 local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local function wall() for y = 0, 43 do local t = y / 43 rect(0, y, W, 1, lerp(246, 230, t), lerp(230, 208, t), lerp(194, 166, t)) end rect(0, 30, W, 14, 244, 232, 204) for x = 0, W - 1, 8 do rect(x, 30, 1, 14, 222, 206, 170) end for y = 30, 42, 6 do rect(0, y, W, 1, 222, 206, 170) end rect(0, 43, W, 1, 196, 160, 110) rect(37, 4, 25, 22, 160, 108, 60) for y = 6, 23 do rect(39, y, 21, 1, lerp(160, 206, (y - 6) / 17), lerp(208, 234, (y - 6) / 17), lerp(246, 252, (y - 6) / 17)) end rect(49, 6, 1, 18, 160, 108, 60); rect(39, 14, 21, 1, 160, 108, 60) rect(40, 8, 5, 2, 255, 255, 255); rect(52, 17, 6, 2, 255, 255, 255) rect(35, 26, 29, 2, 178, 124, 72) rect(0, 16, 27, 2, 170, 116, 66) rect(4, 9, 6, 7, 214, 70, 60); rect(4, 9, 6, 2, 150, 40, 40); rect(5, 12, 4, 2, 246, 226, 190) rect(15, 11, 6, 5, 60, 110, 170); rect(21, 12, 1, 3, 60, 110, 170); rect(16, 11, 4, 1, 246, 240, 226) end local function counter() local top = { { 236, 190, 124 }, { 224, 172, 106 }, { 212, 158, 94 }, { 192, 136, 78 } } for i = 1, 4 do rect(0, 43 + i, W, 1, top[i][1], top[i][2], top[i][3]) end for y = 48, H - 1 do local t = (y - 48) / 15 rect(0, y, W, 1, lerp(120, 158, math.min(1, t * 3)), lerp(76, 102, math.min(1, t * 3)), lerp(42, 56, math.min(1, t * 3))) end for i = 0, 1 do local x = 2 + i * 31 rect(x, 50, 28, 6, 176, 118, 66); rect(x + 1, 51, 26, 4, 164, 106, 58) rect(x + 11, 52, 6, 1, 236, 204, 130) rect(x, 58, 28, 6, 176, 118, 66); rect(x + 1, 59, 26, 5, 164, 106, 58) rect(x + 12, 60, 4, 2, 236, 204, 130) end rect(55, 38, 7, 6, 190, 94, 52); rect(55, 38, 7, 1, 214, 120, 70) for i = 0, 6 do local h = hash32(i * 0x9E3779B1) rect(54 + umod(h, 9), 32 + umod(h >> 6, 6), 2, 3, 70 + umod(h >> 10, 40), 150 + umod(h >> 14, 40), 70) end end local function toaster() rect(TX0 + 1, 33, TW - 2, 3, 196, 208, 222) rect(TX0 + 4, 34, 15, 1, 28, 26, 34) rect(TX0 + 2, 33, TW - 4, 1, 224, 232, 242) for y = TY0, 44 do local t = (y - TY0) / 8 local r, g, b if t < 0.5 then r, g, b = lerp(214, 150, t / 0.5), lerp(224, 164, t / 0.5), lerp(236, 186, t / 0.5) else r, g, b = lerp(150, 98, (t - 0.5) / 0.5), lerp(164, 112, (t - 0.5) / 0.5), lerp(186, 138, (t - 0.5) / 0.5) end rect(TX0, y, TW, 1, r, g, b) rect(TX0, y, 1, 1, 90, 100, 124); rect(TX0 + TW - 1, y, 1, 1, 90, 100, 124) end rect(TX0 + 3, TY0 + 1, 2, 7, 250, 252, 255) rect(TX0 + 15, TY0 + 1, 1, 7, 232, 238, 248) rect(TX0 + 14, TY0 + 5, 7, 3, 214, 168, 120) rect(TX0 + 8, TY0 + 4, 4, 1, 90, 100, 124) rect(TX0 + 17, TY0 + 1, 1, 8, 70, 78, 98) rect(TX0, 45, TW, 2, 44, 48, 60) rect(TX0 + 1, 47, TW - 2, 1, 150, 100, 56) end local STAGES = { function() wall() end, function() counter(); toaster(); BG = snapshot(0, 0, W, H); FRONT = snapshot(TX0, 35, TW, 12) end, } local STAGE = 1 local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE < #STAGES then PART = snapshot(0, 0, W, H) end STAGE = STAGE + 1 end function on_settings() C = TOASTS[cfg.toast] or TOASTS.golden BURNT = cfg.toast == "burnt" PERIOD = TT + FT + SQ + LK + WK + ({ short = 30, normal = 70, long = 140 })[cfg.pause or "normal"] end local function R(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local function toast(cx, f, o) local d = o.drop or 0 local top = f - 16 + d if not o.peek then local ax = (o.arms == "swingL") and 8 or 7 local bx = (o.arms == "swingR") and 8 or 7 if o.arms == "up" then for i = 0, 4 do px(cx - 7 - i // 2, f - 9 - i, ARM[1], ARM[2], ARM[3]) px(cx + 7 + i // 2, f - 9 - i, ARM[1], ARM[2], ARM[3]) end else R(cx - ax, f - 9 + d, 1, 5, ARM); R(cx + bx, f - 9 + d, 1, 5, ARM) end local lg = o.legs if lg == "squat" then R(cx - 5, f, 3, 1, SHOE); R(cx + 3, f, 3, 1, SHOE) elseif lg == "air" then R(cx - 4, f - 2, 2, 2, BLUE_D); R(cx + 3, f - 2, 2, 2, BLUE_D) R(cx - 6, f, 3, 1, SHOE); R(cx + 4, f, 3, 1, SHOE) elseif lg == "walkA" then R(cx - 3, f - 2, 2, 1, BLUE_D); R(cx - 4, f - 1, 3, 1, SHOE) R(cx + 2, f - 2, 2, 2, BLUE_D); R(cx + 2, f, 3, 1, SHOE) elseif lg == "walkB" then R(cx + 2, f - 2, 2, 1, BLUE_D); R(cx + 2, f - 1, 3, 1, SHOE) R(cx - 3, f - 2, 2, 2, BLUE_D); R(cx - 4, f, 3, 1, SHOE) elseif lg == "mid" then R(cx - 2, f - 2, 2, 2, BLUE_D); R(cx + 1, f - 2, 2, 2, BLUE_D) R(cx - 3, f, 3, 1, SHOE); R(cx + 1, f, 3, 1, SHOE) else R(cx - 3, f - 2, 2, 2, BLUE_D); R(cx + 2, f - 2, 2, 2, BLUE_D) R(cx - 4, f, 3, 1, SHOE); R(cx + 2, f, 3, 1, SHOE) end R(cx - 4, f - 5 + d, 9, 3, BLUE); R(cx - 4, f - 5 + d, 9, 1, BLUE_D) px(cx - 2, f - 4 + d, 240, 200, 60); px(cx + 2, f - 4 + d, 240, 200, 60) end local ct, rg, inn = C.crust, C.ring, C.inner R(cx - 4, top, 9, 1, ct); R(cx - 6, top + 1, 13, 2, ct); R(cx - 5, top + 3, 11, 8, ct) R(cx - 5, top + 1, 11, 2, rg); R(cx - 4, top + 3, 9, 7, rg) R(cx - 4, top + 2, 9, 1, inn); R(cx - 3, top + 3, 7, 6, inn) if BURNT then R(cx - 3, top + 5, 2, 1, ct); px(cx + 2, top + 3, ct[1], ct[2], ct[3]) end if o.peek then return end local fc, dx = C.face, o.dx or 0 local ey = top + 4 if o.blink then R(cx - 3 + dx, ey + 1, 1, 1, fc); R(cx + 2 + dx, ey + 1, 1, 1, fc) else R(cx - 3 + dx, ey, 1, 2, fc); R(cx + 2 + dx, ey, 1, 2, fc) end local m = o.mouth if m == "o" then R(cx - 1 + dx, top + 6, 2, 2, fc) elseif m == "smile" then R(cx - 1 + dx, top + 8, 2, 1, fc); px(cx - 2 + dx, top + 7, fc[1], fc[2], fc[3]); px(cx + 1 + dx, top + 7, fc[1], fc[2], fc[3]) else R(cx - 1 + dx, top + 7, 3, 1, fc) end end local function smoke(t) for i = 1, 9 do local age = i * 4 local cx, f = state(t - age) if cx and t - age >= TT and cx < 72 then local a = 0.34 * (1 - age / 40) local x = cx + 2 * sin(age * 0.35 + i) local ix = floor(x) local y = f - 17 - age * 0.2 blend(ix, floor(y), 110, 102, 98, a * (1 - (x - ix))) blend(ix + 1, floor(y), 110, 102, 98, a * (x - ix)) end end end function render() if STAGE <= #STAGES then build() if STAGE <= #STAGES then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local t = frame % PERIOD local down = t < TT if down then local k = t / TT blend_rect(TX0 + 5, TY0 + 5, 2, 1, 255, 140, 40, 0.3 + 0.6 * k * (0.75 + 0.25 * sin(t * 0.8))) blend_rect(TX0 + 5, 34, 15, 1, 255, 100, 30, 0.2 + 0.5 * k) end local cx, f, pose = state(t) if cx then toast(cx, f, pose) if cx < 31 and f >= 36 then blit_rgb(FRONT, TX0, 35, TW, 12) end end R(TX0 + 16, down and TY0 + 5 or TY0 + 1, 3, 2, KNOB) if BURNT then smoke(t) end end