info = { type = "image", title = "Kite hill", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A child flies a kite on a windy hill", api = 33, } local tr = ({ sk = { ["Kite colour"] = "Farba šarkana", ["Wind"] = "Vietor", ["Breeze"] = "Vánok", ["Windy"] = "Veterno", ["Gusty"] = "Nárazový" }, cs = { ["Kite colour"] = "Barva draka", ["Wind"] = "Vítr", ["Breeze"] = "Vánek", ["Windy"] = "Větrno", ["Gusty"] = "Nárazový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "kite", label = T("Kite colour"), type = "color", default = "#E8402C" }, { key = "wind", label = T("Wind"), type = "int", default = "windy", opts = { keys = { "breeze", "windy", "gusty" }, T("Breeze"), T("Windy"), T("Gusty") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, ceil, sin, cos, min, max = math.floor, math.ceil, math.sin, math.cos, math.min, math.max local W, H = gfx.W, gfx.H local function near_top(x) return floor(53 + 2.5 * sin(x * 0.065 + 1.2) + 0.5) end local function far_top(x) return floor(46 + 3 * sin(x * 0.06 + 2.0) + 1.5 * sin(x * 0.17) + 0.5) end local CHILD_X = 9 local GY = near_top(CHILD_X + 1) local HAND_X, HAND_Y = CHILD_X + 4, GY - 9 local CREAM = { 255, 244, 224 } local HAIR = { 232, 190, 84 } local STR = 1.0 local CA, CB = { 232, 64, 44 }, CREAM function on_settings() local c = cfg.kite or { r = 232, g = 64, b = 44 } CA = { c.r, c.g, c.b } STR = ({ breeze = 0.6, windy = 1.0, gusty = 1.5 })[cfg.wind] or 1.0 end local BG local function build_bg() for y = 0, 52 do local t = y / 52 rect(0, y, W, 1, floor(88 + 94 * t + 0.5), floor(170 + 54 * t + 0.5), floor(244 + 6 * t + 0.5)) end for x = 0, W - 1 do local t = far_top(x) rect(x, t, 1, H - t, 126, 192, 118) px(x, t, 150, 210, 134) end for x = 0, W - 1 do local t = near_top(x) rect(x, t, 1, H - t, 84, 172, 62) px(x, t, 112, 196, 80) end blend_rect(0, 58, W, 6, 40, 110, 40, 0.35) for i = 0, 17 do local x = (i * 29) % 60 + 2 local y = 56 + (i * 7) % 6 blend_rect(x, y, 5 + i % 3, 1, 56, 140, 50, 0.7) end local x0, y0 = CHILD_X, GY - 13 rect(x0, y0, 3, 1, HAIR[1], HAIR[2], HAIR[3]) px(x0, y0 + 1, HAIR[1], HAIR[2], HAIR[3]) rect(x0 + 1, y0 + 1, 2, 2, 244, 204, 164) px(x0, y0 + 2, HAIR[1], HAIR[2], HAIR[3]) rect(x0, y0 + 3, 3, 5, 52, 196, 200) rect(x0 + 3, y0 + 4, 1, 1, 52, 196, 200) px(x0 + 4, y0 + 4, 244, 204, 164) rect(x0, y0 + 8, 3, 3, 40, 70, 132) px(x0 + 1, y0 + 10, 120, 192, 100) rect(x0, y0 + 11, 1, 1, 50, 44, 60) rect(x0 + 2, y0 + 11, 1, 1, 50, 44, 60) BG = gfx.snapshot(0, 0, W, H) end local function gust(f) return 0.5 + 0.35 * sin(f * 0.0105) + 0.15 * sin(f * 0.043 + 1.3) end local function kite_at(f) local g = gust(f) - 0.5 local th = 0.62 + g * 0.3 * STR + 0.03 * sin(f * 0.071) local len = 44 + 3 * g * STR + 1.2 * sin(f * 0.057 + 0.5) return HAND_X + len * cos(th), HAND_Y - len * sin(th), 0.18 * sin(f * 0.045 + 0.3) * STR + 0.3 * g end local VX = { 0, 5.2, 0, -5.2 } local VY = { -7, -2, 8, -2 } local function span(xa, xb, y, r, g, b) if xb <= xa then return end local i0, i1 = floor(xa), floor(xb) if i0 == i1 then if xb - xa > 0.04 then blend(i0, y, r, g, b, xb - xa) end else local c0 = 1 - (xa - i0) if c0 > 0.04 then blend(i0, y, r, g, b, c0) end if i1 - i0 > 1 then rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b) end local c1 = xb - i1 if c1 > 0.04 then blend(i1, y, r, g, b, c1) end end end local PX, PY = { 0, 0, 0, 0 }, { 0, 0, 0, 0 } local function draw_kite(kx, ky, al) local c, s = cos(al), sin(al) local lo, hi = 99, -99 for i = 1, 4 do local x, y = VX[i], VY[i] PX[i], PY[i] = kx + x * c - y * s, ky + x * s + y * c if PY[i] < lo then lo = PY[i] end if PY[i] > hi then hi = PY[i] end end local tx, ty, bx, by = PX[1], PY[1], PX[3], PY[3] for y = floor(lo), floor(hi) do local yc = y + 0.5 local xl, xr = 999, -999 for i = 1, 4 do local j = i % 4 + 1 local py1, py2 = PY[i], PY[j] if (py1 <= yc and py2 > yc) or (py2 <= yc and py1 > yc) then local x = PX[i] + (yc - py1) * (PX[j] - PX[i]) / (py2 - py1) if x < xl then xl = x end if x > xr then xr = x end end end if xr > xl then local xs = tx + (yc - ty) * (bx - tx) / (by - ty) if xs < xl then xs = xl elseif xs > xr then xs = xr end span(xl, xs, y, CA[1], CA[2], CA[3]) span(xs, xr, y, CB[1], CB[2], CB[3]) span(xs - 0.45, xs + 0.45, y, 120, 70, 60) end end return bx, by end local function bulb(x, y, r, g, b, k) k = k or 1.4 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local w = (1 - fx) * (1 - fy) if w > 0.05 then blend(ix, iy, r, g, b, min(1, w * k)) end w = fx * (1 - fy) if w > 0.05 then blend(ix + 1, iy, r, g, b, min(1, w * k)) end w = (1 - fx) * fy if w > 0.05 then blend(ix, iy + 1, r, g, b, min(1, w * k)) end w = fx * fy if w > 0.05 then blend(ix + 1, iy + 1, r, g, b, min(1, w * k)) end end local CLOUD = { { 0, 4, 8 }, { 1, 1, 14 }, { 2, 0, 16 }, { 3, 2, 11 } } local CLOUDS = { { 4, 0.031, 6, 0.9 }, { 26, 0.022, 30, 1.0 }, { 34, 0.045, 52, 0.8 } } local function draw_clouds(f) for i = 1, #CLOUDS do local c = CLOUDS[i] local x = (c[3] + c[2] * f) % (W + 24) - 20 for r = 1, 4 do local row = CLOUD[r] if r < 4 then span(x + row[2], x + row[2] + row[3], c[1] + row[1], 252, 252, 255) else span(x + row[2], x + row[2] + row[3], c[1] + row[1], 214, 230, 250) end end end end local function draw_grass(f, g) local amp = (0.6 + 1.6 * g) * STR for x = 1, W - 2, 4 do local y = near_top(x) local s = amp * sin(f * 0.13 - x * 0.45) px(x, y - 1, 70, 160, 54) local fx = s * 0.8 local ix = floor(fx) local fr = fx - ix blend(x + ix, y - 2, 150, 214, 96, 1 - fr) blend(x + ix + 1, y - 2, 150, 214, 96, fr) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local g = gust(f) draw_clouds(f) local kx, ky, al = kite_at(f) local ex, ey = kx, ky + 2.5 local mx, my = (HAND_X + ex) / 2, (HAND_Y + ey) / 2 local sag = (7 - 3 * STR) + 2 * sin(f * 0.037) local cx, cy = mx - 2, my + sag * 2 for i = 1, 40 do local u = i / 41 local a, b, c = (1 - u) * (1 - u), 2 * u * (1 - u), u * u bulb(a * HAND_X + b * cx + c * ex, a * HAND_Y + b * cy + c * ey, 250, 250, 255, 0.85) end local bx0, by0 local lx, ly = kx - 8 * sin(al), ky + 8 * cos(al) for i = 1, 6 do local kx2, ky2 = kite_at(f - 5 * i) local u = i / 6 local x = lx + (kx2 - kx) * 0.8 - 1.0 * i * (0.7 + 0.5 * g) + 1.5 * u * STR * sin(f * 0.17 - i * 0.9) local y = ly + 2.7 * i + (ky2 - ky) * 0.8 + 0.7 * u * STR * sin(f * 0.13 - i * 1.1 + 1) local ax, ay = bx0 or lx, by0 or ly for s = 1, 3 do local t = s / 4 px(floor(ax + (x - ax) * t + 0.5), floor(ay + (y - ay) * t + 0.5), 250, 250, 255) end local col = i % 2 == 1 and CA or CB bulb(x - 0.9, y, col[1], col[2], col[3]) bulb(x + 0.9, y, col[1], col[2], col[3]) bx0, by0 = x, y end draw_kite(kx, ky, al) draw_grass(f, g) px(HAND_X - 1 + (f // 9) % 2, GY - 12, HAIR[1], HAIR[2], HAIR[3]) px(HAND_X + (f // 7) % 2, GY - 12, HAIR[1], HAIR[2], HAIR[3]) end