info = { type = "image", title = "Slime run", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A slime hops across platforms collecting coins", api = 33, } local tr = ({ sk = { ["Palette"] = "Paleta", ["Mint"] = "Mätová", ["Berry"] = "Bobuľa", ["Ice"] = "Ľad", ["Score"] = "Skóre" }, cs = { ["Palette"] = "Paleta", ["Mint"] = "Mátová", ["Berry"] = "Bobule", ["Ice"] = "Led", ["Score"] = "Skóre" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pal", label = T("Palette"), type = "int", default = 0, opts = { T("Mint"), T("Game Boy"), T("Berry"), T("Ice") } }, { key = "hud", label = T("Score"), type = "bool", default = true }, } local rect, px = gfx.rect, gfx.px local floor, sqrt = math.floor, math.sqrt local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PALS = { { { 22, 60, 72 }, { 46, 130, 122 }, { 132, 206, 160 }, { 222, 246, 204 } }, { { 40, 72, 30 }, { 76, 122, 42 }, { 152, 184, 44 }, { 208, 226, 124 } }, { { 64, 26, 74 }, { 152, 62, 124 }, { 240, 142, 162 }, { 255, 228, 204 } }, { { 26, 42, 100 }, { 62, 104, 196 }, { 134, 184, 244 }, { 228, 242, 255 } }, } local C, PAL = PALS[1], {} local L, SX = 40, 20 local TOPS = { 40, 46, 52 } local HOP, SPIKE_HOP = 4, 10 local TW, TH = 32, 24 local TEX do local rows, row = {}, {} for y = 0, TH - 1 do for x = 0, TW - 1 do local v if y == 0 or y == 3 then v = 1 elseif y == 1 then v = 3 elseif y == 2 then v = (x % 4 == 1) and 2 or 3 else local b = (y - 4) // 5 if (y - 4) % 5 == 4 or (x + (b % 2) * 4) % 8 == 7 then v = 1 elseif umod(hash32(y * 0x9E3779B1 + x * 0x85EBCA6B), 11) == 0 then v = 3 else v = 2 end end row[x + 1] = v end rows[y + 1] = string.char(table.unpack(row, 1, TW)) end TEX = table.concat(rows) end local CK = {} local function chunk(j) local slot = j & 7 local c = CK[slot] if c and c.j == j then return c end local h = hash32(j * 0x9E3779B1) local len = 22 + umod(h, 11) local a, b = j * L + 4, j * L + len - 3 local spike = j > 1 and umod(h >> 12, 3) == 0 local n = spike and 1 or floor((b - a) / 11 + 0.5) if n < 1 then n = 1 end c = { j = j, x0 = j * L, len = len, top = TOPS[umod(h >> 8, 3) + 1], a = a, b = b, n = n, l = (b - a) / n, ks = n // 2, spike = spike } CK[slot] = c return c end local function jump_y(c, d, s) local y0, y1 = c.top, d.top local hj = 10 + (y0 > y1 and (y0 - y1) * 0.6 or 0) return y0 + (y1 - y0) * s - 4 * hj * s * (1 - s) end local function slime_at(X) local j = X // L local c = chunk(j) if X < c.a then local p = chunk(j - 1) local s = (X - p.b) / (c.a - p.b) return jump_y(p, c, s), s, c.a - p.b elseif X > c.b then local d = chunk(j + 1) local s = (X - c.b) / (d.a - c.b) return jump_y(c, d, s), s, d.a - c.b end local k = floor((X - c.a) / c.l) if k > c.n - 1 then k = c.n - 1 end local s = (X - c.a - k * c.l) / c.l local hh = (c.spike and k == c.ks) and SPIKE_HOP or HOP return c.top - 4 * hh * s * (1 - s), s, c.l end local COIN_S = { 0.25, 0.5, 0.75 } local function coins(c) if c.cx then return end local d = chunk(c.j + 1) c.cx, c.cy = {}, {} for k = 1, 3 do local s = COIN_S[k] c.cx[k] = c.b + s * (d.a - c.b) c.cy[k] = jump_y(c, d, s) - 5 end c.cx[4] = c.a + (c.ks + 0.5) * c.l c.cy[4] = c.top - ((c.spike and SPIKE_HOP) or HOP) - 5 end local function col(i) return C[i][1], C[i][2], C[i][3] end local SHAPE = { normal = { 9, 7 }, squash = { 11, 5 }, stretch = { 7, 9 } } local function draw_slime(x, by, shape) local w, h = shape[1], shape[2] local hw, top = w // 2, by - h + 1 rect(x - hw + 2, top, w - 4, 1, col(1)) rect(x - hw + 1, top + 1, w - 2, 1, col(1)) rect(x - hw, top + 2, w, h - 2, col(1)) rect(x - hw + 2, top + 1, w - 4, 1, col(2)) rect(x - hw + 1, top + 2, w - 2, h - 3, col(2)) px(x - hw + 2, top + 2, col(4)) local ey = top + (h > 5 and 3 or 2) rect(x, ey, 1, 2, col(4)); rect(x + 2, ey, 1, 2, col(4)) end local SPIN = { 5, 3, 1, 3 } local function draw_coin(x, y, ph) local w = SPIN[ph % 4 + 1] local l = x - w // 2 if w == 5 then rect(l + 1, y - 2, 3, 5, col(1)); rect(l, y - 1, 5, 3, col(1)) rect(l + 1, y - 1, 3, 3, col(3)); px(l + 1, y - 1, col(4)) elseif w == 3 then rect(l, y - 2, 3, 5, col(1)); rect(l + 1, y - 1, 1, 3, col(3)) else rect(l, y - 2, 1, 5, col(1)) end end local HILLS = { { 16, 14, 10 }, { 54, 20, 15 }, { 82, 10, 7 } } local HSTEP = { 0.45, 0.75, 0.92, 1.0 } local CLOUDS = { { 10, 8, 12 }, { 62, 14, 9 }, { 100, 5, 14 } } local HB = 52 local function draw_back(cam) rect(0, 0, W, H, col(4)) local co = (cam // 4) % 128 for k = 0, 1 do for i = 1, #CLOUDS do local c = CLOUDS[i] local x = c[1] - co + k * 128 if x > -c[3] and x < W then rect(x + 2, c[2], c[3] - 4, 1, col(3)) rect(x, c[2] + 1, c[3], 2, col(3)) end end end local ho = (cam // 2) % 96 for k = 0, 1 do for i = 1, #HILLS do local hl = HILLS[i] local x = hl[1] - ho + k * 96 if x + hl[2] >= 0 and x - hl[2] < W then local bh = (hl[3] + 3) // 4 for b = 1, 4 do local hw = floor(hl[2] * HSTEP[b] + 0.5) rect(x - hw, HB - hl[3] + (b - 1) * bh, 2 * hw + 1, bh, col(3)) end end end end rect(0, HB, W, H - HB, col(3)) end local SCORE_F = { font = "3x5", tight = false, cell = true } local HUD = true function on_settings() C = PALS[(cfg.pal or 0) + 1] or PALS[1] PAL = { 0, 0, 0 } for i = 1, 4 do PAL[#PAL + 1] = C[i][1]; PAL[#PAL + 1] = C[i][2]; PAL[#PAL + 1] = C[i][3] end HUD = cfg.hud ~= false end local STEP_HZ = 30 function render() local fr = frame * STEP_HZ // gfx.FPS local X = fr + L + 4 local cam = X - SX draw_back(cam) local j0, j1 = cam // L, (cam + W) // L for j = j0, j1 do local c = chunk(j) local x = c.x0 - cam gfx.clip(x, c.top, c.len, H - c.top) gfx.blit_indexed(TEX, PAL, x, c.top, TW, TH, TW, TH) gfx.clip() rect(x, c.top, 1, H - c.top, col(1)); rect(x + c.len - 1, c.top, 1, H - c.top, col(1)) if c.spike then local sx = floor(c.a + (c.ks + 0.5) * c.l + 0.5) - cam px(sx, c.top - 3, col(1)) rect(sx - 1, c.top - 2, 3, 2, col(1)) px(sx, c.top - 2, col(4)) end end for j = j0 - 1, j1 do local c = chunk(j) coins(c) for k = 1, 4 do local cx = c.cx[k] local x, y = floor(cx + 0.5) - cam, floor(c.cy[k] + 0.5) if X < cx then draw_coin(x, y, fr // 5 + k) elseif X - cx < 8 then local r = 2 + floor((X - cx) / 2) px(x - r, y, col(1)); px(x + r, y, col(1)); px(x, y - r, col(1)); px(x, y + r, col(1)) end end end local y, s, len = slime_at(X) local ft = s * len local shape = SHAPE.normal if ft < 2 or (len - ft) < 1 then shape = SHAPE.squash elseif s < 0.35 then shape = SHAPE.stretch end draw_slime(SX, floor(y + 0.5) - 1, shape) if HUD then local jc = (X - 4) // L local c = chunk(jc) coins(c) local got = 0 for k = 1, 4 do if c.cx[k] <= X then got = got + 1 end end local score = (4 * jc + got) % 1000 draw_coin(4, 4, 0) gfx.text(9, 2, string.format("%03d", score), C[1][1], C[1][2], C[1][3], SCORE_F) end end