info = { type = "image", title = "Fox at the brook", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fox leaps into a brook after a fish", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Falling apple"] = "Padajúce jablko" }, cs = { ["Speed"] = "Rychlost", ["Falling apple"] = "Padající jablko" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "apple", label = T("Falling apple"), type = "bool", default = true }, } local rect, blend_rect, blend, px = gfx.rect, gfx.blend_rect, gfx.blend, gfx.px local floor, sin, sqrt, exp, pi = math.floor, math.sin, math.sqrt, math.exp, math.pi local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function R(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local function disc(cx, cy, r, c) for dy = -r, r do local h = floor(sqrt(r * r - dy * dy) + 0.5) rect(cx - h, cy + dy, 2 * h + 1, 1, c[1], c[2], c[3]) end end local O, D, WH = { 238, 118, 40 }, { 176, 74, 30 }, { 255, 244, 228 } local K, L, M = { 48, 28, 26 }, { 96, 46, 30 }, { 214, 96, 34 } local GRASS, GRASS_D = { 118, 204, 84 }, { 92, 180, 66 } local SAND = { 224, 196, 138 } local SPLASH, RING = { 236, 248, 255 }, { 200, 232, 255 } local APPLE, APPLE_L = { 232, 52, 48 }, { 255, 160, 140 } local WATER_Y0, WATER_Y1 = 36, 63 local FLOW = {} local function xl(y) return 24 - floor((y - WATER_Y0) * 0.12) end local function xr(y) return 40 + floor((y - WATER_Y0) * 0.9) end local BG, TREE local TX, TY, TW, TH = 42, 0, 22, 46 local function build_bg() for y = 0, 33 do local k = y / 33 rect(0, y, W, 1, floor(70 + 106 * k), floor(150 + 72 * k), floor(235 + 15 * k)) end for x = 0, W - 1 do local h = floor(29 + 2.5 * sin(x * 0.09) + 2 * sin(x * 0.23 + 1) + 0.5) rect(x, h, 1, 36 - h, 122, 196, 136) end R(0, 34, W, 30, GRASS) for i = 1, 70 do local h = hash32(i * 0x9E3779B1) local x, y = (h & 0x7FFF) % W, 35 + ((h >> 8) & 0x7FFF) % 28 if x < xl(y) - 1 or x > xr(y) + 2 then if i % 9 == 0 then px(x, y, 255, 255, 255) elseif i % 13 == 0 then px(x, y, 255, 220, 80) else rect(x, y, 1, 2, GRASS_D[1], GRASS_D[2], GRASS_D[3]) end end end for y = WATER_Y0, WATER_Y1 do local k = (y - WATER_Y0) / (WATER_Y1 - WATER_Y0) local a, b = xl(y), xr(y) rect(a - 1, y, 1, 1, SAND[1], SAND[2], SAND[3]) rect(b + 1, y, 1, 1, SAND[1], SAND[2], SAND[3]) rect(a, y, b - a + 1, 1, floor(84 - 34 * k), floor(166 - 40 * k), floor(240 - 10 * k)) end for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) FLOW[i] = { (h & 0x7FFF) % 100, ((h >> 8) & 0x7FFF) % 100, 0.12 + ((h >> 4) & 7) * 0.02 } end blend_rect(50, 45, 14, 2, 40, 110, 40, 0.35) BG = gfx.snapshot(0, 0, W, H) rect(TX, TY, TW, TH, 255, 0, 255) R(58, 22, 4, 23, { 118, 74, 42 }); R(61, 22, 1, 23, { 92, 56, 34 }); R(55, 43, 10, 2, { 118, 74, 42 }) disc(53, 12, 9, { 40, 134, 60 }); disc(58, 10, 8, { 40, 134, 60 }); disc(49, 15, 6, { 40, 134, 60 }) disc(53, 10, 7, { 70, 168, 70 }); disc(58, 8, 6, { 70, 168, 70 }); disc(48, 13, 4, { 70, 168, 70 }) disc(52, 8, 4, { 118, 206, 84 }); disc(58, 6, 3, { 118, 206, 84 }) TREE = gfx.snapshot(TX, TY, TW, TH) end local APPLES = { { 56, 14 }, { 50, 17 }, { 60, 18 }, { 53, 6 } } local function apple(x, y, a) if a >= 1 then R(x, y, 3, 3, APPLE); px(x, y, APPLE_L[1], APPLE_L[2], APPLE_L[3]) else blend_rect(x, y, 3, 3, APPLE[1], APPLE[2], APPLE[3], a) end end local LEGS = { stand = { { 7, 9, 2, 4 }, { 10, 9, 2, 4 }, { 15, 9, 2, 4 }, { 18, 9, 2, 4 } }, walkA = { { 6, 9, 2, 4 }, { 12, 9, 2, 4 }, { 14, 9, 2, 4 }, { 19, 9, 2, 4 } }, walkB = { { 9, 9, 2, 4 }, { 10, 9, 2, 3 }, { 17, 9, 2, 4 }, { 16, 9, 2, 3 } }, crouch = { { 6, 11, 3, 2 }, { 9, 11, 3, 2 }, { 15, 11, 3, 2 }, { 18, 11, 3, 2 } }, air = { { 2, 8, 6, 2 }, { 5, 10, 4, 2 }, { 17, 8, 6, 2 }, { 15, 10, 4, 2 } }, } local function fox(x, y, dir, pose, wag, fish) x, y = floor(x + 0.5), floor(y + 0.5) local function r(dx, dy, w, h, c) R(dir > 0 and x + dx or x + 24 - dx - w, y + dy, w, h, c) end local by = pose == "crouch" and 2 or 0 for _, l in ipairs(LEGS[pose]) do r(l[1], l[2], l[3], l[4], L) end if pose == "air" then r(0, 4, 6, 2, O); r(0, 4, 2, 2, WH) else r(1 + wag, 5 + by, 5, 3, O); r(wag, 6 + by, 2, 3, WH) end r(6, 3 + by, 11, 6, O); r(6, 8 + by, 11, 1, D); r(7, 4 + by, 3, 3, M) r(14, 3 + by, 3, 4, O); r(15, 5 + by, 3, 4, WH) r(16, 2 + by, 6, 5, O); r(21, 5 + by, 3, 2, WH); r(23, 4 + by, 1, 1, K); r(17, 6 + by, 3, 1, WH) r(17, by, 2, 3, O); r(20, by, 2, 3, O); r(18, 1 + by, 1, 2, D); r(21, 1 + by, 1, 2, D) r(20, 3 + by, 1, 1, K) if fish then r(24, 5, 4, 2, { 214, 226, 240 }); r(24, 6, 4, 1, { 255, 255, 255 }) r(28, 4, 1, 4, { 238, 150, 90 }); r(25, 5, 1, 1, K) end end local function drop(x0, y0, vx, vy, u, ymax) local y = y0 + vy * u + 35 * u * u if y < ymax then px(floor(x0 + vx * u + 0.5), floor(y + 0.5), SPLASH[1], SPLASH[2], SPLASH[3]) end end local function ring(cx, cy, rx, a) local ry = rx * 0.35 for i = 0, 35 do local ang = i * pi / 18 blend(floor(cx + rx * math.cos(ang) + 0.5), floor(cy + ry * sin(ang) + 0.5), RING[1], RING[2], RING[3], a) end end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local GROUND = 45 local function apple_y(u, y0) local h = GROUND - y0 local g = 70 local T = sqrt(2 * h / g) if u < T then return y0 + 0.5 * g * u * u end local v = g * T * 0.4 u = u - T local tb = 2 * v / g if u < tb then return GROUND - (v * u - 0.5 * g * u * u) end v = v * 0.4 u = u - tb tb = 2 * v / g if u < tb then return GROUND - (v * u - 0.5 * g * u * u) end return GROUND end local PERIOD = 16.5 local DIVE_X, DIVE_Y = 35, 51 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = (frame / FPS * (0.6 + 0.2 * (cfg.speed or 3))) % PERIOD local fr = frame / FPS for i = 1, 2 do local x = floor((i * 29 + fr * (2 + i)) % (W + 24) - 24) local y = 4 + i * 7 blend_rect(x + 3, y, 13, 2, 255, 255, 255, 0.85) blend_rect(x, y + 2, 20, 2, 255, 255, 255, 0.85) end gfx.blit_rgb(TREE, TX, TY, TW, TH, 255, 0, 255) local falls = cfg.apple ~= false for i = 1, #APPLES do local a = APPLES[i] if i > 1 or not falls then apple(a[1], a[2], 1) elseif t < 11.5 then apple(a[1], a[2], 1) elseif t > 15.8 then apple(a[1], a[2], (t - 15.8) / 0.7) end end if falls and t >= 11.5 and t < 15.8 then local ay = apple_y(t - 11.5, APPLES[1][2]) local x = APPLES[1][1] - (ay >= GROUND and 0 or 0) local roll = t > 12.6 and floor(math.min(3, (t - 12.6) * 4)) or 0 apple(x - roll, floor(ay + 0.5), t > 15 and (15.8 - t) / 0.8 or 1) end for i = 1, #FLOW do local f = FLOW[i] local y = WATER_Y0 + 2 + floor((f[2] + fr * f[3] * 30) % (WATER_Y1 - WATER_Y0 - 2)) local a, b = xl(y) + 2, xr(y) - 6 local x = a + floor(f[1] / 100 * (b - a)) blend_rect(x, y, 4, 1, 220, 244, 255, 0.55) end if t >= 0.4 and t < 5.0 then local k = ease((t - 0.4) / 3.0) local fx, fy = floor(50 - 14 * k + 0.5), 51 + floor(sin(t * 2) + 0.5) local al = t < 0.9 and (t - 0.4) / 0.5 or 0.9 blend_rect(fx, fy, 5, 2, 214, 226, 240, al) blend_rect(fx + 5, fy - 1 + floor(sin(t * 12) + 0.5), 1, 3, 238, 150, 90, al) end local us = t - 5.0 if us > 0 then for k = 0, 2 do local u = us - k * 0.45 if u > 0 and u < 1.8 then ring(DIVE_X, DIVE_Y + 2, 2 + 9 * u, 0.8 * (1 - u / 1.8)) end end if us < 0.3 then local h = floor(10 * (1 - us / 0.3) + 0.5) rect(DIVE_X - 1, DIVE_Y - h, 3, h + 1, SPLASH[1], SPLASH[2], SPLASH[3]) end if us < 1.2 then for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local hh = h & 0x7FFF drop(DIVE_X, DIVE_Y, hh % 19 - 9, -(20 + (hh >> 4) % 18), us, DIVE_Y + 1) end end end if t < 1.6 then fox(-24 + 24 * t / 1.6, 39, 1, floor(t * 6) % 2 == 0 and "walkA" or "walkB", 0) elseif t < 3.4 then fox(0, 39, 1, "stand", floor(sin(t * 5) + 0.5)) elseif t < 4.2 then fox(0, 39, 1, "crouch", 0) elseif t < 5.0 then local s = (t - 4.2) / 0.8 if s > 0.85 then gfx.clip(0, 0, W, DIVE_Y) end fox(12 * s, 39 + 7 * s - 48 * s * (1 - s), 1, "air", 0) gfx.clip() elseif t < 5.9 then elseif t < 8.4 then local s = ease((t - 5.9) / 2.5) local x = 30 - 22 * s local y = 46 - 7 * math.min(1, math.max(0, (24 - x) / 16)) if t < 6.6 then y = y + 6 * (1 - ease((t - 5.9) / 0.7)) end gfx.clip(0, 0, W, DIVE_Y + 1) fox(x, y, -1, "stand", 0, true) gfx.clip() elseif t < 10.2 then local u = t - 8.4 local dx = floor(2 * sin(u * 2 * pi * 9) * (1 - u / 1.8) + 0.5) fox(8 + dx, 39, -1, "stand", 0, true) for i = 0, 11 do local h = hash32(i * 0x85EBCA6B) & 0x7FFF local d = u - i * 0.12 if d > 0 and d < 0.9 then local sgn = h % 2 == 0 and 1 or -1 drop(8 + 12 + dx, 43, sgn * (12 + (h >> 3) % 18), -(8 + (h >> 6) % 14), d, 52) end end elseif t < 12.4 then local s = (t - 10.2) / 2.2 fox(8 - 34 * s, 39, -1, floor(t * 6) % 2 == 0 and "walkA" or "walkB", 0, true) end end