info = { type = "image", title = "Chomper", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A big-mouthed monster chomps at a fly", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Monster colour"] = "Farba príšery", ["Speed"] = "Rýchlosť" }, cs = { ["Monster colour"] = "Barva příšery", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Monster colour"), type = "color", default = "#F08A2A" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, ceil, sin, cos, sqrt = math.floor, math.ceil, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.2831853 local SKY, SUN, SUN_R = { 98, 176, 236 }, { 255, 234, 128 }, { 150, 206, 242 } local HILL_F, HILL_N = { 126, 198, 210 }, { 80, 160, 110 } local GRASS, GRASS_HI = { 84, 174, 62 }, { 132, 208, 88 } local SOIL, SOIL_D = { 140, 88, 52 }, { 104, 62, 38 } local TUFT, PEB, PEB_D = { 46, 126, 46 }, { 184, 136, 96 }, { 96, 58, 36 } local BUSH, CLOUD = { 58, 138, 72 }, { 242, 248, 255 } local PETAL_W, PETAL_Y = { 250, 250, 250 }, { 255, 220, 60 } local OUT, TOOTH = { 44, 22, 40 }, { 248, 246, 234 } local MOUTH, TONGUE = { 96, 18, 40 }, { 228, 92, 122 } local EYE_W, PUPIL = { 250, 250, 244 }, { 24, 18, 30 } local FLY, WING = { 30, 30, 40 }, { 214, 234, 255 } local KEY = { 255, 0, 255 } local C, CD, CL, BELLY = {}, {}, {}, {} local function monster_colours(c) C = { c.r, c.g, c.b } CD = { c.r * 5 // 8, c.g * 5 // 8, c.b * 5 // 8 } CL = { c.r + (255 - c.r) * 2 // 5, c.g + (255 - c.g) * 2 // 5, c.b + (255 - c.b) * 2 // 5 } BELLY = { c.r + (255 - c.r) * 3 // 5, c.g + (240 - c.g) * 3 // 5, c.b + (200 - c.b) * 3 // 5 } end monster_colours({ r = 240, g = 138, b = 42 }) local BG, UP, LO = nil, nil, nil local CYC = { 24, 16, 12, 10, 8 } local cyc, v = 12, 1 function on_settings() monster_colours(cfg.body or { r = 240, g = 138, b = 42 }) cyc = CYC[cfg.speed or 3] or 12 v = 12 / cyc BG = nil end local function c3(c) return c[1], c[2], c[3] end local BODY = { { 0, 10, 22 }, { 1, 7, 25 }, { 2, 5, 27 }, { 3, 3, 28 }, { 4, 2, 29 }, { 5, 1, 30 }, { 6, 1, 31 }, { 7, 0, 31 }, { 8, 0, 32 }, { 9, 0, 32 }, { 10, 0, 32 }, { 11, 0, 32 }, { 12, 0, 32 }, { 13, 0, 32 }, { 14, 0, 32 }, { 15, 0, 32 }, { 16, 0, 32 }, { 17, 0, 31 }, { 18, 0, 31 }, { 19, 1, 30 }, { 20, 1, 29 }, { 21, 2, 28 }, { 22, 4, 26 }, { 23, 6, 24 }, } local HINGE = 12 local TEETH_UP = { 13, 17, 21, 25, 29 } local TEETH_LO = { 15, 19, 23, 27 } local function part(ox, oy, r0, r1) for i = 1, #BODY do local r = BODY[i] if r[1] >= r0 and r[1] <= r1 then rect(ox + r[2] - 1, oy + r[1] - 1, r[3] - r[2] + 3, 3, c3(OUT)) end end for i = 1, #BODY do local r = BODY[i] if r[1] >= r0 and r[1] <= r1 then rect(ox + r[2], oy + r[1], r[3] - r[2] + 1, 1, c3(C)) end end end local function eyeball(cx, cy, r) for y = floor(cy - r - 1), ceil(cy + r + 1) do local dy = y + 0.5 - cy for pass = 1, 2 do local rr = pass == 1 and r + 0.8 or r if dy * dy < rr * rr then local hw = sqrt(rr * rr - dy * dy) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c3(pass == 1 and OUT or EYE_W)) end end end end end local function build() rect(0, 0, 40, 20, c3(KEY)) local ox, oy = 2, 3 local SP = { 12, -2, 7, -1, 3, 1, 0, 4 } for i = 1, #SP, 2 do local x, y = ox + SP[i], oy + SP[i + 1] rect(x - 1, y, 3, 2, c3(OUT)); px(x, y - 1, c3(OUT)) end part(ox, oy, 0, 12) rect(ox, oy + 13, HINGE, 1, c3(C)) for i = 1, #SP, 2 do local x, y = ox + SP[i], oy + SP[i + 1]; rect(x, y, 1, 2, c3(CD)) end rect(ox + 8, oy + 1, 12, 1, c3(CL)); rect(ox + 5, oy + 2, 6, 1, c3(CL)) local SPOT = { 4, 6, 8, 4, 9, 9, 13, 7, 3, 10 } for i = 1, #SPOT, 2 do rect(ox + SPOT[i], oy + SPOT[i + 1], 2, 1, c3(CD)) end eyeball(ox + 21, oy + 4, 3) eyeball(ox + 28, oy + 6.5, 2.1) for i = 1, #TEETH_UP do local x = ox + TEETH_UP[i] rect(x, oy + 13, 3, 1, c3(TOOTH)); px(x + 1, oy + 14, c3(TOOTH)) end UP = gfx.snapshot(0, 0, 37, 18) rect(0, 0, 40, 20, c3(KEY)) oy = -10 part(ox, oy, 13, 23) rect(ox, oy + 12, HINGE, 1, c3(C)) for y = 16, 21 do local inset = (y == 16 or y == 21) and 3 or 1 rect(ox + 7 + inset, oy + y, 18 - 2 * inset, 1, c3(BELLY)) end rect(ox + 5, oy + 22, 21, 1, c3(CD)); rect(ox + 7, oy + 23, 17, 1, c3(CD)) for i = 1, #TEETH_LO do local x = ox + TEETH_LO[i] rect(x, oy + 12, 3, 1, c3(TOOTH)); px(x + 1, oy + 11, c3(TOOTH)) end LO = gfx.snapshot(0, 0, 37, 16) rect(0, 0, W, 50, c3(SKY)) for pass = 1, 2 do local r = pass == 1 and 6.5 or 4.5 for y = 1, 17 do local dy = y + 0.5 - 9 if dy * dy < r * r then local hw = sqrt(r * r - dy * dy) local x0, x1 = ceil(9 - hw - 0.5), floor(9 + hw - 0.5) rect(x0, y, x1 - x0 + 1, 1, c3(pass == 1 and SUN_R or SUN)) end end end for x = 0, W - 1 do local f = floor(36 + 4 * sin(x * 0.09 + 1) + 2 * sin(x * 0.23) + 0.5) rect(x, f, 1, 50 - f, c3(HILL_F)) local n = floor(43 + 3 * sin(x * 0.13 + 2) + 1.5 * sin(x * 0.31) + 0.5) rect(x, n, 1, 50 - n, c3(HILL_N)) end rect(0, 50, W, 2, c3(GRASS)); rect(0, 50, W, 1, c3(GRASS_HI)) rect(0, 52, W, 12, c3(SOIL)); rect(0, 52, W, 1, c3(SOIL_D)) BG = gfx.snapshot(0, 0, W, H) end local function hrun(x, y, w, c) local xi = floor(x); local f = x - xi local r, g, b = c[1], c[2], c[3] if f < 0.04 then rect(xi, y, w, 1, r, g, b) return end if w > 1 then rect(xi + 1, y, w - 1, 1, r, g, b) end blend(xi, y, r, g, b, 1 - f) blend(xi + w, y, r, g, b, f) end local ITEMS = {} do local function add(k, x, y) ITEMS[#ITEMS + 1] = { k, x, y } end for i = 0, 5 do add(1, i * 12 + umod(hash32(i * 0x9E3779B1), 7), 49) end for i = 0, 7 do local h = hash32(i * 0x85EBCA6B + 7) add(2, i * 9 + umod(h, 5), 54 + umod(h >> 8, 9)) end add(3, 20, 47); add(3, 55, 47); add(4, 10, 45); add(4, 46, 46) end local CLOUDS = { { 7, 18, 10 }, { 15, 12, 60 } } local X, Y = 12, 22 local LEGS = { { 7, 0.5, true }, { 21, 0.0, true }, { 7, 0.0, false }, { 21, 0.5, false } } local function leg(l, ph0, by) local p = (ph0 + l[2]) % 1 local dx, lift if p < 0.6 then dx, lift = 3 - 10 * p, 0 else local q = (p - 0.6) / 0.4; dx, lift = -3 + 6 * q, floor(2.5 * sin(3.14159 * q) + 0.5) end local hx = X + l[1] local fx = floor(hx + dx + 0.5) local top = by + 21 local fy = 48 - lift local lx = floor(hx + dx * 0.5 + 0.5) if l[3] then rect(lx, top, 4, fy - top, c3(CD)); rect(fx - 1, fy, 6, 2, c3(CD)) else rect(lx - 1, top, 6, fy - top + 1, c3(OUT)); rect(fx - 2, fy - 1, 8, 4, c3(OUT)) rect(lx, top, 4, fy - top, c3(C)); rect(fx - 1, fy, 6, 2, c3(C)) px(fx + 4, fy + 1, c3(TOOTH)) end end function render() FR = frame * STEP_HZ // gfx.FPS if not BG then build() end gfx.blit_rgb(BG, 0, 0, W, H) local S = (FR % (cyc * 12)) * v local cs = (FR % 4800) * 0.06 for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[3] - cs) % (W + 32) - 16 hrun(x + 4, c[1], L - 8, CLOUD) hrun(x, c[1] + 1, L, CLOUD) hrun(x + 2, c[1] + 2, L - 4, CLOUD) end for i = 1, #ITEMS do local it = ITEMS[i] local k = it[1] local x = (it[2] - S * (k == 4 and 0.5 or 1)) % 72 - 4 local y = it[3] if k == 1 then hrun(x + 1, y - 1, 1, TUFT); hrun(x, y, 3, TUFT) elseif k == 2 then hrun(x, y, 2, (i % 3 == 0) and PEB_D or PEB) elseif k == 3 then hrun(x + 1, y + 1, 1, TUFT); hrun(x, y, 3, (i % 2 == 0) and PETAL_Y or PETAL_W) else hrun(x + 1, y, 4, BUSH); hrun(x, y + 1, 6, BUSH); hrun(x, y + 2, 6, BUSH) end end local t = (FR + 95) % 105 local o, squash = 0, 0 if t < 12 then o = floor(7 * sin(1.5708 * t / 12) + 0.5) elseif t < 22 then o = 7 elseif t == 22 then o = 4 elseif t == 23 then o = 1 elseif t < 26 then squash = 1 end local ph0 = (FR % cyc) / cyc local bob = (cos(ph0 * 2 * TAU) > 0) and -1 or 0 local by = Y + bob + squash leg(LEGS[1], ph0, by); leg(LEGS[2], ph0, by) if o > 2 then local gy = by + 14 - o rect(X, gy, HINGE, o - 2, c3(C)) rect(X - 1, gy, 1, o - 2, c3(OUT)) rect(X + HINGE, gy, 21, o - 2, c3(MOUTH)) rect(X + 14, by + 10, 13, 2, c3(TONGUE)) end local kr, kg, kb = c3(KEY) gfx.blit_rgb(LO, X - 2, by + 10, 37, 16, kr, kg, kb) gfx.blit_rgb(UP, X - 2, by - 3 - o, 37, 18, kr, kg, kb) leg(LEGS[3], ph0, by); leg(LEGS[4], ph0, by) local fr = FR % 3600 local fx = 52 + 2.5 * sin(fr * 0.37) + 1.2 * sin(fr * 0.91) local fy = 28 + 2 * sin(fr * 0.29 + 1) + sin(fr * 1.3) if t >= 16 and t < 40 then fx, fy = fx + (t - 16) * 1.8, fy - (t - 16) * 1.3 elseif t >= 40 and t < 70 then local q = (t - 40) / 30; q = q * q * (3 - 2 * q) fx, fy = 70 + (fx - 70) * q, -6 + (fy + 6) * q end local ix, iy = floor(fx + 0.5), floor(fy + 0.5) if ix > -3 and ix < W + 2 and iy > -3 then rect(ix, iy, 2, 1, c3(FLY)) if FR % 2 == 0 then px(ix, iy - 1, c3(WING)); px(ix + 1, iy - 1, c3(WING)) else px(ix - 1, iy - 1, c3(WING)); px(ix + 2, iy - 1, c3(WING)) end end local ey = by - o local dx = ix > X + 30 and 1 or (ix < X + 18 and -1 or 0) local dy = iy < ey + 2 and -1 or (iy > ey + 8 and 1 or 0) if (FR + 30) % 120 < 4 then rect(X + 18, ey + 1, 7, 6, c3(C)); rect(X + 18, ey + 4, 7, 1, c3(OUT)) rect(X + 26, ey + 5, 4, 4, c3(C)); rect(X + 26, ey + 6, 4, 1, c3(OUT)) else rect(X + 20 + dx, ey + 3 + dy, 2, 2, c3(PUPIL)) rect(X + 27 + (dx > 0 and 1 or 0), ey + 6 + (dy > 0 and 1 or 0), 1, 2, c3(PUPIL)) end if t >= 24 and t < 29 then local mx, my = X + 34, by + 13 rect(mx + 1, my, 3, 1, c3(TOOTH)) px(mx, my - 3, c3(TOOTH)); px(mx + 1, my - 4, c3(TOOTH)) px(mx, my + 3, c3(TOOTH)); px(mx + 1, my + 4, c3(TOOTH)) end if t >= 25 and t < 34 then local gx, gy = X + 13 + floor((t - 25) * 2.2), by + 13 local a = 1 - (t - 25) / 9 px(gx, gy, 255, 255, 255) blend(gx - 1, gy, 255, 255, 220, a); blend(gx + 1, gy, 255, 255, 220, a) blend(gx, gy - 1, 255, 255, 220, a); blend(gx, gy + 1, 255, 255, 220, a) end end