info = { type = "image", title = "Hatchling", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Egg wobbles, cracks and a baby dragon hops out", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Egg colour"] = "Farba vajíčok", ["Dragon colour"] = "Farba draka", ["Nest"] = "Hniezdo" }, cs = { ["Egg colour"] = "Barva vajíček", ["Dragon colour"] = "Barva draka", ["Nest"] = "Hnízdo" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "egg", label = T("Egg colour"), type = "color", default = "#8ED8C6" }, { key = "baby", label = T("Dragon colour"), type = "color", default = "#F49A3C" }, { key = "nest", label = T("Nest"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs = math.floor, math.sin, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local FLOOR = 47 local OX, OY = 32, 54 local EH = 26 local CR = 10 local HW = {} for r = 0, EH - 1 do local v = (r + 0.5) / EH * 2 - 1 HW[r] = floor(10.4 * sqrt(1 - v * v) * (1 + 0.17 * v) + 0.5) end local function ck(dx) return ({ 0, 1, 0, -1 })[((dx + 40) // 2) % 4 + 1] end local SPOTS = { { -5, 4, 2, 2 }, { 2, 7, 3, 2 }, { -7, 13, 2, 2 }, { 4, 15, 2, 3 }, { -2, 19, 3, 2 }, { 6, 21, 2, 2 }, { -6, 22, 2, 1 }, { 0, 2, 2, 1 } } local function mix(c, d, k) return floor(c[1] + (d[1] - c[1]) * k + 0.5), floor(c[2] + (d[2] - c[2]) * k + 0.5), floor(c[3] + (d[3] - c[3]) * k + 0.5) end local BLACK, WHITE = { 0, 0, 0 }, { 255, 255, 255 } local E, ES, EL, EO, ESP, EIN = {}, {}, {}, {}, {}, {} local function egg_colours(c) local b = { c.r, c.g, c.b } E = b ES = { mix(b, BLACK, 0.22) } EL = { mix(b, WHITE, 0.50) } EO = { mix(b, { 30, 24, 40 }, 0.68) } ESP = { mix(b, { 70, 50, 60 }, 0.42) } EIN = { mix(b, { 40, 20, 30 }, 0.62) } end local function egg_row(r, xc, y, a) local hw = HW[r] local x0, w = xc - hw, 2 * hw local sx = x0 + floor(w * 0.66) if a >= 1 then rect(x0, y, w, 1, E[1], E[2], E[3]) rect(sx, y, x0 + w - sx, 1, ES[1], ES[2], ES[3]) if r >= 3 and r <= 11 then rect(x0 + 3, y, 2, 1, EL[1], EL[2], EL[3]) end px(x0, y, EO[1], EO[2], EO[3]); px(x0 + w - 1, y, EO[1], EO[2], EO[3]) else blend_rect(x0, y, w, 1, E[1], E[2], E[3], a) blend_rect(sx, y, x0 + w - sx, 1, ES[1], ES[2], ES[3], a) end end local function egg_part(r0, r1, xc, ybase, sk, piv, a) for r = r0, r1 do egg_row(r, xc + floor(sk * (piv - r) + 0.5), ybase + r, a) end end local function egg_top_bottom(xc, ybase, sk) rect(xc + floor(sk * EH + 0.5) - HW[0] + 1, ybase - 1, 2 * HW[0] - 2, 1, EO[1], EO[2], EO[3]) rect(xc - HW[EH - 1] + 1, ybase + EH, 2 * HW[EH - 1] - 2, 1, EO[1], EO[2], EO[3]) end local function spots(xc, ybase, sk, piv, rmin, rmax, a) for k = 1, #SPOTS do local s = SPOTS[k] local r = s[2] if r >= rmin and r <= rmax then local x = xc + s[1] + floor(sk * (piv - r) + 0.5) if a >= 1 then rect(x, ybase + r, s[3], s[4], ESP[1], ESP[2], ESP[3]) else blend_rect(x, ybase + r, s[3], s[4], ESP[1], ESP[2], ESP[3], a) end end end end local BABY = { "........oooo....", ".......oggggo...", "......oggggggo..", "....o.ogggwkggo.", "...odoggggwkgggo", "....doggggggggpo", "...ddogggglllllo", "....dogggooooo..", "...ddoggggo.....", "....doglllgo....", "...ddgllllggo...", ".oo.dglllllgo...", "oggodglllllgo...", ".ogggggllggggo..", "..oogggggggggo..", "....oggo.oggo...", "....ooo..ooo....", } local DOT = 46 local function build_sprite(rows, mirror) local h, w = #rows, #rows[1] local G = {} for y = 1, h do local r = { rows[y]:byte(1, w) } if mirror then local m = {} for x = 1, w do m[x] = r[w + 1 - x] end r = m end G[y] = r end local function at(x, y) local r = G[y] return r and r[x] or DOT end local core, top, bot = {}, {}, {} local function push(list, x, y, n, c) local m = #list list[m + 1], list[m + 2], list[m + 3], list[m + 4] = x, y, n, c end for y = 1, h do local x = 1 while x <= w do local c = at(x, y) if c == DOT then x = x + 1 else local tc = at(x, y - 1) == DOT local n = 1 while x + n <= w and at(x + n, y) == c and (at(x + n, y - 1) == DOT) == tc do n = n + 1 end push(tc and top or core, x - 1, y - 1, n, c) x = x + n end end x = 1 while x <= w do local c = at(x, y) if c ~= DOT and at(x, y + 1) == DOT then local n = 1 while x + n <= w and at(x + n, y) == c and at(x + n, y + 1) == DOT do n = n + 1 end push(bot, x - 1, y, n, c) x = x + n else x = x + 1 end end end return { core = core, top = top, bot = bot } end local function paint(s, pal, mw) local out = {} for _, key in ipairs({ "core", "top", "bot" }) do local src, dst = s[key], {} for k = 1, #src, 4 do local c = pal[src[k + 3]] local m = #dst local x = mw and (mw - src[k] - src[k + 2]) or src[k] dst[m + 1], dst[m + 2], dst[m + 3] = x, src[k + 1], src[k + 2] dst[m + 4], dst[m + 5], dst[m + 6] = c[1], c[2], c[3] end out[key] = dst end return out end local function draw_sprite(s, x, y, a) local iy = floor(y) local f = y - iy local c = s.core if a >= 1 then for k = 1, #c, 6 do rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5]) end else for k = 1, #c, 6 do blend_rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5], a) end end c = s.top local at = a * (1 - f) for k = 1, #c, 6 do blend_rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5], at) end if f > 0.03 then c = s.bot local ab = a * f for k = 1, #c, 6 do blend_rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5], ab) end end end local BLINK = {} for i = 1, #BABY do local s = BABY[i] if i == 4 then s = s:gsub("w", "g"):gsub("k", "g") elseif i == 5 then s = s:gsub("w", "o"):gsub("k", "o") end BLINK[i] = s end local SHAPES = { build_sprite(BABY, false), build_sprite(BLINK, false) } local SW = #BABY[1] local BR, BL, BBR, BBL = nil, nil, nil, nil local function baby_sprites(c) local b = { c.r, c.g, c.b } local pal = { [("o"):byte()] = { mix(b, { 40, 20, 30 }, 0.72) }, [("g"):byte()] = b, [("d"):byte()] = { mix(b, { 120, 30, 30 }, 0.35) }, [("l"):byte()] = { mix(b, WHITE, 0.55) }, [("w"):byte()] = { 255, 255, 255 }, [("k"):byte()] = { 30, 24, 36 }, [("p"):byte()] = { mix(b, { 255, 90, 120 }, 0.55) }, } BR, BL = paint(SHAPES[1], pal), paint(SHAPES[1], pal, SW) BBR, BBL = paint(SHAPES[2], pal), paint(SHAPES[2], pal, SW) end local BG, RIM, NEST = nil, nil, true local RX, RY, RW, RH = 10, 50, 44, 11 local function straw(x, y, k) local h = hash32(k * 0x9E3779B1) local c = umod(h, 3) if c == 0 then return 214, 168, 92 elseif c == 1 then return 168, 118, 64 end return 236, 200, 120 end local function build_bg() for y = 0, FLOOR - 1 do local t = y / (FLOOR - 1) rect(0, y, W, 1, floor(250 - 26 * t), floor(222 - 38 * t), floor(196 - 36 * t)) end rect(0, FLOOR, W, H - FLOOR, 200, 150, 120) rect(0, FLOOR, W, 1, 222, 176, 146) for y = FLOOR + 6, H - 1, 6 do rect(0, y, W, 1, 188, 138, 110) end if NEST then for y = 44, 51 do local v = (y - 51) / 7 local hw = floor(21 * sqrt(1 - v * v) + 0.5) if hw > 0 then rect(OX - hw, y, 2 * hw, 1, 150, 104, 58) end for k = 0, 2 * hw - 1, 3 do px(OX - hw + k + umod(hash32(y * 97 + k), 2), y, straw(0, 0, y * 131 + k)) end end rect(OX - 15, 49, 30, 3, 96, 64, 40) end BG = gfx.snapshot(0, 0, W, H) if NEST then rect(RX, RY, RW, RH, 255, 0, 255) for y = RY, RY + RH - 1 do local v = (y - 50) / 10 local hw = floor(22 * sqrt(1 - v * v) + 0.5) local cut = y <= 52 and floor(15 * sqrt(1 - ((y - 50) / 3.2) ^ 2) + 0.5) or 0 if hw > 0 then if cut > 0 then rect(OX - hw, y, hw - cut, 1, 176, 124, 66) rect(OX + cut, y, hw - cut, 1, 176, 124, 66) else rect(OX - hw, y, 2 * hw, 1, 176, 124, 66) end for k = 0, 2 * hw - 1, 2 do local xx = OX - hw + k + umod(hash32(y * 31 + k * 7), 2) if xx < OX - cut or xx >= OX + cut then px(xx, y, straw(0, 0, y * 57 + k)) end end end end rect(OX - 20, RY + RH - 1, 40, 1, 120, 80, 46) RIM = gfx.snapshot(RX, RY, RW, RH) end end local CYCLE = 480 local OFF = 170 local POP, RISE0, RISE1 = 160, 166, 196 local HOP0, HOPN, HOPT = 300, 3, 18 local FADE0, FADE1 = 380, 418 local DROP0 = 420 local function wobble(u) local function burst(u0, d, amp) if u < u0 or u >= u0 + d then return 0 end local e = sin(PI * (u - u0) / d) return amp * e * e * sin(2 * PI * (u - u0) / 11) end return burst(45, 45, 0.10) + burst(110, 50, 0.19) + burst(HOP0, 26, 0.07) end local function crack_n(u) if u < 75 then return -1 end if u < 92 then return floor((u - 75) / 17 * 4) end if u < 110 then return 4 end return math.min(HW[CR], 4 + floor((u - 110) / 45 * (HW[CR] - 4))) end local CAP_FLOOR = 56 local function cap_state(u) local x, y = OX, OY - EH + 1 + CR / 2 local vx, vy, tilt = 0.7, -3.4, 0 local g = 0.34 local n = math.min(u - POP, 70) local landed = 0 for _ = 1, n do vy = vy + g x, y = x + vx, y + vy if landed == 0 then tilt = tilt + 0.028 end if y >= CAP_FLOOR then y = CAP_FLOOR landed = landed + 1 vy = -vy * 0.38 vx = vx * 0.5 if landed >= 2 then vy, vx, g = 0, 0, 0 end end end return x, y, tilt end local HOPS = { { 24, 30, 9, 41, 11 }, { 9, 41, -6, 41, 6 }, { -6, 41, -22, 41, 6 } } local function hop_pos(u) local k = (u - HOP0) // HOPT + 1 if k > HOPN then return nil end local s = ((u - HOP0) % HOPT) / HOPT local h = HOPS[k] local x = h[1] + (h[3] - h[1]) * s local y = h[2] + (h[4] - h[2]) * s - h[5] * 4 * s * (1 - s) return x, y end local function drop_y(u) local t = u - DROP0 if t < 0 then return nil end local g = 0.30 local T1 = 19 if t < T1 then return -0.5 * g * (T1 - t) * (T1 - t) end t = t - T1 local T2 = 12 if t < T2 then return -0.5 * g * t * (T2 - t) end t = t - T2 local T3 = 6 if t < T3 then return -0.5 * g * t * (T3 - t) end return 0 end function on_settings() egg_colours(cfg.egg or { r = 142, g = 216, b = 198 }) baby_sprites(cfg.baby or { r = 244, g = 154, b = 60 }) NEST = cfg.nest ~= false BG, RIM = nil, nil end local function shadow(k, a) if NEST or k <= 0.1 then return end local hw = floor(11 * k + 0.5) blend_rect(OX - hw, OY, 2 * hw, 1, 90, 50, 40, 0.26 * k * a) blend_rect(OX - hw + 2, OY + 1, 2 * hw - 4, 1, 90, 50, 40, 0.22 * k * a) end local function whole_egg(u, dy) local ang = wobble(u) local yb = OY - EH + 1 + floor(dy + 0.5) egg_part(0, EH - 1, OX, yb, ang, EH - 1, 1) egg_top_bottom(OX, yb, ang) spots(OX, yb, ang, EH - 1, 0, EH - 1, 1) local n = crack_n(u) if n >= 0 then for dx = -n, n - 1 do local r = CR + ck(dx) px(OX + dx + floor(ang * (EH - 1 - r) + 0.5), yb + r, EO[1], EO[2], EO[3]) end if n >= 6 then px(OX - 3 + floor(ang * (EH - CR) + 0.5), yb + CR - 2, EO[1], EO[2], EO[3]) px(OX + 4 + floor(ang * (EH - 3 - CR) + 0.5), yb + CR + 2, EO[1], EO[2], EO[3]) end end end local function lower_shell(ang, a) local yb = OY - EH + 1 local xc = OX + floor(ang * (EH - 1 - CR) + 0.5) blend_rect(xc - HW[CR] + 2, yb + CR - 1, 2 * HW[CR] - 4, 2, EIN[1], EIN[2], EIN[3], a) end local function lower_front(ang, a) local yb = OY - EH + 1 local piv = EH - 1 egg_part(CR + 2, EH - 1, OX, yb, ang, piv, a) local xc = OX + floor(ang * (piv - CR) + 0.5) local hw = HW[CR] for dx = -hw, hw - 1, 2 do local r = CR + ck(dx) blend_rect(xc + dx, yb + r, 2, CR + 2 - r, E[1], E[2], E[3], a) end if a >= 1 then px(xc - hw, yb + CR + 1, EO[1], EO[2], EO[3]); px(xc + hw - 1, yb + CR + 1, EO[1], EO[2], EO[3]) rect(xc - HW[EH - 1] + 1, yb + EH, 2 * HW[EH - 1] - 2, 1, EO[1], EO[2], EO[3]) end spots(OX, yb, ang, piv, CR + 2, EH - 1, a) end local function cap(u, a) local x, y, tilt = cap_state(u) local xc = floor(x + 0.5) local mid = CR // 2 local yb = floor(y + 0.5) - mid for r = 0, CR + 1 do local xr = xc + floor(tilt * (mid - r) + 0.5) if r < CR - 1 then egg_row(r, xr, yb + r, a) else local hw = HW[r] for dx = -hw, hw - 1, 2 do if r < CR + ck(dx) then blend_rect(xr + dx, yb + r, 2, 1, E[1], E[2], E[3], a) end end end end spots(xc, yb, tilt, mid, 0, CR - 2, a) end local function baby_in_shell(u) local left = (u >= 232 and u < 268) or u >= 292 local blink = (u >= 214 and u < 218) or (u >= 252 and u < 256) or (u >= 282 and u < 286) local s if left then s = blink and BBL or BL else s = blink and BBR or BR end if u < RISE1 then local k = (u - RISE0) / (RISE1 - RISE0) if k < 0 then return end k = 1 - (1 - k) * (1 - k) draw_sprite(s, 24, 41 - 11 * k, 1) else draw_sprite(s, 24, 30, 1) end end function render() FR = frame * STEP_HZ // gfx.FPS if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local u = (FR + OFF) % CYCLE if u < POP then shadow(1, 1) whole_egg(u, 0) elseif u < FADE1 then local a = 1 if u >= FADE0 then a = 1 - (u - FADE0) / (FADE1 - FADE0) end local ang = (u >= HOP0) and wobble(u) or 0 shadow(1, a) lower_shell(ang, a) if u < HOP0 then baby_in_shell(u) end lower_front(ang, a) elseif u >= DROP0 then local dy = drop_y(u) shadow(1 + dy / 60, 1) whole_egg(0, dy) end if RIM then gfx.blit_rgb(RIM, RX, RY, RW, RH, 255, 0, 255) end if u >= POP and u < FADE1 then cap(u, (u >= FADE0) and 1 - (u - FADE0) / (FADE1 - FADE0) or 1) end if u >= HOP0 then local x, y = hop_pos(u) if x then draw_sprite(BL, floor(x + 0.5), y, 1) end end end