mirror of
https://codeberg.org/fairyglade/ly.git
synced 2026-08-12 00:29:13 +02:00
67 lines
2.1 KiB
Lua
67 lines
2.1 KiB
Lua
-- [[
|
|
-- This is an example of using LuaJIT to create a custom animation in Ly, in this case a
|
|
-- bouncing square that changes colors.
|
|
--
|
|
-- You are given the following `ly` table:
|
|
-- {
|
|
-- height: number -- The height of the terminal
|
|
-- width: number -- The width of the terminal
|
|
-- frame_delay: number -- The amount of frames to delay an update to the animation
|
|
-- putCell(byte, fg, bg, x, y) -- Put the cell a buffer. This buffer will be drawn
|
|
-- on screen after draw() is called. It will not clear
|
|
-- itself automatically, see `ly.clear()`.
|
|
-- All arguments to this function are numbers, and
|
|
-- must be in the unsigned 32-bit integer range: 0 to 2^32-1.
|
|
-- clear() -- Clear the buffer.
|
|
-- }
|
|
--
|
|
-- A function named `draw()` must be declared in the script. This is ran every frame.
|
|
--
|
|
-- ]]
|
|
|
|
local SQUARE_WIDTH = 10
|
|
local SQUARE_HEIGHT = 5
|
|
|
|
local SQUARE_COUNT = 25
|
|
|
|
local FPS = 120
|
|
|
|
local squares = {}
|
|
|
|
for i = 0, SQUARE_COUNT-1 do
|
|
local vx = 1
|
|
local vy = 1
|
|
if math.random(1, 2) == 2 then vx = -vx end
|
|
if math.random(1, 2) == 2 then vy = -vy end
|
|
squares[#squares+1] = {
|
|
x = math.random(1, ly.width - SQUARE_WIDTH),
|
|
y = math.random(1, ly.height - SQUARE_HEIGHT),
|
|
vx = vx,
|
|
vy = vy,
|
|
color = math.random(0xFFFFFF)
|
|
}
|
|
end
|
|
|
|
local timer = os.clock()
|
|
|
|
function draw()
|
|
-- Rather than progressing the animation by frame, do it based on
|
|
-- seconds, like from os.clock().
|
|
if timer + (1/FPS) < os.clock() then
|
|
ly.clear()
|
|
for i, v in ipairs(squares) do
|
|
v.x = v.x + v.vx
|
|
v.y = v.y + v.vy
|
|
for x = v.x, v.x + SQUARE_WIDTH do
|
|
for y = v.y, v.y + SQUARE_HEIGHT do
|
|
ly.putCell(string.byte(' '), 0, v.color, x, y)
|
|
end
|
|
end
|
|
if v.x <= 0 then v.vx = 1; v.color = math.random(0xFFFFFF) end
|
|
if v.x + SQUARE_WIDTH >= ly.width-1 then v.vx = -1; v.color = math.random(0xFFFFFF) end
|
|
if v.y <= 0 then v.vy = 1; v.color = math.random(0xFFFFFF) end
|
|
if v.y + SQUARE_HEIGHT >= ly.height-1 then v.vy = -1; v.color = math.random(0xFFFFFF) end
|
|
end
|
|
timer = os.clock()
|
|
end
|
|
end
|