info = { type = "image", title = "Fetch dog", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A dog chases a bouncing ball across the lawn", api = 33, } local tr = ({ sk = { ["Dog"] = "Pes", ["White"] = "Biely", ["Brown"] = "Hnedý", ["Black"] = "Čierny", ["Toy"] = "Hračka", ["Ball"] = "Lopta", ["Frisbee"] = "Frisbee" }, cs = { ["Dog"] = "Pes", ["White"] = "Bílý", ["Brown"] = "Hnědý", ["Black"] = "Černý", ["Toy"] = "Hračka", ["Ball"] = "Míč", ["Frisbee"] = "Frisbee" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "dog", label = T("Dog"), type = "int", default = "white", opts = { keys = { "white", "brown", "black" }, T("White"), T("Brown"), T("Black") } }, { key = "toy", label = T("Toy"), type = "int", default = "ball", opts = { keys = { "ball", "frisbee" }, T("Ball"), T("Frisbee") } }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor, sin, cos, sqrt, max = math.floor, math.sin, math.cos, math.sqrt, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GY = 57 local DOGS = { white = { m = { 240, 240, 246 }, s = { 170, 178, 196 }, e = { 120, 126, 146 }, l = { 255, 255, 255 }, eye = { 28, 28, 38 }, nose = { 28, 28, 38 } }, brown = { m = { 184, 122, 68 }, s = { 128, 80, 42 }, e = { 86, 50, 28 }, l = { 214, 156, 98 }, eye = { 24, 18, 14 }, nose = { 24, 18, 14 } }, black = { m = { 60, 62, 76 }, s = { 36, 38, 48 }, e = { 24, 24, 32 }, l = { 108, 112, 134 }, eye = { 240, 240, 250 }, nose = { 120, 120, 138 } }, } local D, FRIS = DOGS.white, false local BALL, BALL_L, BALL_D = { 224, 48, 54 }, { 255, 160, 160 }, { 150, 26, 38 } local DISC, DISC_D = { 255, 130, 40 }, { 200, 76, 20 } local TONGUE = { 228, 98, 118 } local BG local function cloud(x, y, w) rect(x + 2, y, w - 4, 1, 255, 255, 255) rect(x, y + 1, w, 2, 255, 255, 255) rect(x + 1, y + 3, w - 2, 1, 214, 232, 252) end local function build_bg() for y = 0, 44 do local t = y / 44 rect(0, y, W, 1, floor(58 + 110 * t + 0.5), floor(138 + 76 * t + 0.5), floor(230 + 18 * t + 0.5)) end cloud(6, 7, 16); cloud(36, 14, 20); cloud(50, 4, 12) for x = 0, W - 1 do local top = floor(37 - 4 * sin(x * 0.09 + 1) - 2.5 * sin(x * 0.23) + 0.5) rect(x, top, 1, 46 - top, 128, 184, 168) end for x = 0, W - 1 do local top = floor(41 + 1.5 * sin(x * 0.7) + sin(x * 1.9 + 2) + 0.5) rect(x, top, 1, 48 - top, 44, 116, 62) if umod(hash32(x * 0x9E3779B1), 5) == 0 then rect(x, top + 1, 1, 1, 76, 150, 80) end end local y, k = 47, 0 while y < H do local h = 2 + k // 2 if k % 2 == 0 then rect(0, y, W, h, 104, 190, 76) else rect(0, y, W, h, 90, 174, 66) end y, k = y + h, k + 1 end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) local c = ({ { 255, 255, 255 }, { 255, 224, 90 }, { 250, 160, 190 } })[i % 3 + 1] rect(umod(h, W), 49 + umod(h >> 8, 14), 1, 1, c[1], c[2], c[3]) end BG = gfx.snapshot(0, 0, W, H) end local XS = 46 local VD, TD, TS, TA = 2, 16, 60, 12 local ARCS_BALL = { { 26, 26 }, { 17, 11.5 }, { 11, 5 } } local ARCS_DISC = { { 40, 14 }, { 14, 3 } } local ARCS = ARCS_BALL local TC = 54 local T1 = TC - TD // 2 local PER = 170 local function mouth(t) if t < T1 then return XS - VD * TD / 2 - VD * (T1 - t) end local u = t - T1 if u < TD then return XS - VD * TD / 2 + VD * u - VD * u * u / (2 * TD) end u = u - TD if u < TS then return XS end u = u - TS if u < TA then return XS + VD * u * u / (2 * TA) end return XS + VD * TA / 2 + VD * (u - TA) end local VB = (mouth(TC) - 2 + 3) / TC local function height(t) local acc = 0 for i = 1, #ARCS do local T, h = ARCS[i][1], ARCS[i][2] if t < acc + T then local u = (t - acc) / T return 4 * h * u * (1 - u) end acc = acc + T end return 0 end function on_settings() D = DOGS[cfg.dog] or DOGS.white FRIS = cfg.toy == "frisbee" ARCS = FRIS and ARCS_DISC or ARCS_BALL end local FL = false local function R(x, y, w, h, c) if FL then x = W - x - w end rect(x, y, w, h, c[1], c[2], c[3]) end local function shadow(x, y, w, a) if FL then x = W - x - w end blend_rect(x, y, w, 1, 10, 40, 10, a) end local function leg(ox, bob, hx, fx, fy, c) local y0 = -5 + bob local n = fy - y0 + 1 for i = 0, n - 1 do R(ox + hx + floor((fx - hx) * i / max(1, n - 1) + 0.5), GY + y0 + i, 2, 1, c) end R(ox + fx, GY + fy, 3, 1, c) end local HIPS = { 3, 1, -5, -7 } local PH = { 0.4, 0, 2.9, 2.5 } local function toy(x, y, t) if FRIS then R(x - 1, y + 1, 7, 1, DISC_D) R(x, y, 5, 1, DISC) if (t // 3) % 2 == 0 then R(x + 1, y, 1, 1, { 255, 220, 170 }) else R(x + 3, y, 1, 1, { 255, 220, 170 }) end else R(x + 1, y, 2, 1, BALL); R(x, y + 1, 4, 2, BALL); R(x + 1, y + 3, 2, 1, BALL_D) R(x + 1, y + 1, 1, 1, BALL_L) end end local function dog(ox, k, dh, held, t) local M, S, E, L = D.m, D.s, D.e, D.l local bob = (k and (k == 1 or k == 2)) and -1 or 0 local fx, fy = {}, {} for j = 1, 4 do if k then local ph = k * 1.0472 + PH[j] fx[j] = HIPS[j] + floor(-4 * cos(ph) + 0.5) + 1 fy[j] = -1 - floor(3 * max(0, sin(ph)) + 0.5) else fx[j], fy[j] = HIPS[j], -1 end end shadow(ox - 8, GY, 20, 0.25); shadow(ox - 6, GY + 1, 16, 0.2) leg(ox, bob, HIPS[1], fx[1], fy[1], S) leg(ox, bob, HIPS[3], fx[3], fy[3], S) local wag = k and 0.6 or 2.2 local ph = t * (k and 0.3 or 0.6) for i = 1, 4 do local yy = -11 - i // 2 + bob + floor(wag * (i / 2) * sin(ph - i * 0.8) + 0.5) R(ox - 7 - i, GY + yy, 1, 2, M) end R(ox - 7, GY - 11 + bob, 12, 6, M) R(ox - 7, GY - 7 + bob, 12, 2, S) R(ox - 6, GY - 11 + bob, 8, 1, L) R(ox + 3, GY - 11 + bob, 2, 3, L) leg(ox, bob, HIPS[2], fx[2], fy[2], M) leg(ox, bob, HIPS[4], fx[4], fy[4], M) local hy = GY - 14 + dh + bob R(ox + 4, hy, 6, 5, M) R(ox + 10, hy + 2, 3, 3, M) R(ox + 10, hy + 4, 3, 1, S) R(ox + 12, hy + 2, 1, 1, D.nose) R(ox + 8, hy + 1, 1, 1, D.eye) R(ox + 4, hy, 2, 4, E) if k and not held then R(ox + 11, hy + 5, 1, 1, TONGUE) end if held then toy(ox + 10, GY - 12 + dh + bob, t) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local cyc, t = frame // PER, frame % PER FL = cyc % 2 == 1 local m = mouth(t) local ox = floor(m - 12 + 0.5) if t < TC then local h = height(t) local bx = floor(-3 + VB * t + 0.5) shadow(bx, GY + 2, FRIS and 6 or 4, 0.3 * (1 - h / 30)) toy(bx, GY - (FRIS and 1 or 3) - floor(h + 0.5), t) end if ox > -26 and ox < 80 then local u = t - T1 local stand = u >= TD - 3 and u < TD + TS + 1 local k = (not stand) and floor(ox / 3.2) % 6 or nil local dh = (t >= TC - 3 and t < TC + 3) and 8 or 0 dog(ox, k, dh, t >= TC, t) end end