Spawn ants with mouse, FPS counter

main
mat ess 2022-09-11 12:08:09 -04:00
parent 31b3dec460
commit 535a45dbcc
5 changed files with 92 additions and 57 deletions

View File

@ -1,4 +1,4 @@
function love.conf(t)
t.window.highdpi = false
t.window.highdpi = true
t.window.title = "ants"
end

View File

@ -33,13 +33,12 @@ local function initColors(rules)
local colorbrewer = require("colorbrewer")
-- set a specific random seed to avoid picking the same palette every time
math.randomseed(os.time())
local scheme = colorbrewer.random()
local variant = scheme[#rules]
local scheme = colorbrewer.random(#rules)
COLORS = {}
for i = 1, #rules do
for i, color in pairs(scheme) do
COLORS[i] = {
clockwise = rules:sub(i, i) == "r",
rgba = variant[i]
rgba = color
}
end
end
@ -56,12 +55,31 @@ local function initScreen()
end)
end
local function initAnt()
ANT = {
x = (WIDTH / 2) - ((WIDTH / 2) % UNITS),
y = (HEIGHT / 2) - ((HEIGHT / 2) % UNITS),
direction = "u",
}
local function initFont()
FONT = love.graphics.newFont(24)
love.graphics.setFont(FONT)
FPS_WIDTH = FONT:getWidth("120 FPS")
FPS_HEIGHT = FONT:getHeight()
end
local function spawnAnt(x, y)
table.insert(ANTS, {
x = x,
y = y,
direction = "u"
})
end
local function spawnAntCentered()
spawnAnt(
(WIDTH / 2) - ((WIDTH / 2) % UNITS),
(HEIGHT / 2) - ((HEIGHT / 2) % UNITS)
)
end
local function initAnts()
ANTS = {}
spawnAntCentered()
end
local function initGrid()
@ -82,77 +100,91 @@ end
function love.load(args)
local rules = args[1] or "RL"
UNITS = args[2] or 8
UNITS = args[2] or 4
STEPS_PER_FRAME = args[3] or 5
initColors(rules)
initScreen()
initAnt()
initFont()
initAnts()
initGrid()
end
local function color(x, y)
return GRID[x][y]
local function antColor(ant)
return GRID[ant.x][ant.y]
end
local function current()
return color(ANT.x, ANT.y)
local function setColor(ant, color)
GRID[ant.x][ant.y] = color
end
local function setCurrent(index)
GRID[ANT.x][ANT.y] = index
end
local function updateCurrent()
local val = current()
local function updateColor(ant)
local val = antColor(ant)
if val == #COLORS then
val = 1
else
val = val + 1
end
setCurrent(val)
setColor(ant, val)
end
local function nextColor()
local c = COLORS[current()]
updateCurrent()
local function nextColor(ant)
local c = COLORS[antColor(ant)]
updateColor(ant)
return c
end
function love.mousepressed(x, y, _, _)
spawnAnt(x, y)
end
function love.update()
require("lurker").update()
for _ = 1, STEPS_PER_FRAME do
local c = nextColor()
ANT.direction = rotate(ANT.direction, c.clockwise)
for i, ant in pairs(ANTS) do
local c = nextColor(ant)
ant.direction = rotate(ant.direction, c.clockwise)
CANVAS:renderTo(function()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(c.rgba)
love.graphics.rectangle("fill", ANT.x, ANT.y, UNITS, UNITS)
love.graphics.rectangle("fill", ant.x, ant.y, UNITS, UNITS)
end)
if ANT.direction == "u" then
ANT.y = ANT.y - UNITS
elseif ANT.direction == "r" then
ANT.x = ANT.x + UNITS
elseif ANT.direction == "d" then
ANT.y = ANT.y + UNITS
if ant.direction == "u" then
ant.y = ant.y - UNITS
elseif ant.direction == "r" then
ant.x = ant.x + UNITS
elseif ant.direction == "d" then
ant.y = ant.y + UNITS
else
ANT.x = ANT.x - UNITS
ant.x = ant.x - UNITS
end
if ANT.x >= WIDTH then
ANT.x = 0
elseif ANT.x < 0 then
ANT.x = (WIDTH - 1) - ((WIDTH - 1) % UNITS)
if ant.x >= WIDTH then
ant.x = 0
elseif ant.x < 0 then
ant.x = (WIDTH - 1) - ((WIDTH - 1) % UNITS)
end
if ANT.y >= HEIGHT then
ANT.y = 0
elseif ANT.y < 0 then
ANT.y = (HEIGHT - 1) - ((HEIGHT - 1) % UNITS)
if ant.y >= HEIGHT then
ant.y = 0
elseif ant.y < 0 then
ant.y = (HEIGHT - 1) - ((HEIGHT - 1) % UNITS)
end
ANTS[i] = ant
end
end
end
local function paintFPS()
love.graphics.setBlendMode("alpha")
local fps = tostring(love.timer.getFPS()) .. " FPS"
love.graphics.setColor(COLORS[1].rgba)
love.graphics.rectangle("fill", WIDTH - FPS_WIDTH - 10, HEIGHT - FPS_HEIGHT - 20, FPS_WIDTH + 10, FPS_HEIGHT + 10)
love.graphics.setColor(COLORS[#COLORS].rgba)
love.graphics.print(fps, WIDTH - FPS_WIDTH - 5, HEIGHT - FPS_HEIGHT - 15)
end
function love.draw()
love.graphics.setBlendMode("alpha", "premultiplied")
love.graphics.setColor(COLORS[1].rgba)
love.graphics.draw(CANVAS, 0, 0)
paintFPS()
end

View File

@ -517,10 +517,11 @@ for _, scheme in pairs(colorbrewer) do
end
end
function colorbrewer.random()
local index = math.floor(math.random() * #names)
local scheme = names[index]
return colorbrewer[scheme]
function colorbrewer.random(length)
local index = math.floor(math.random() * #names) + 1
local name = names[index]
local scheme = colorbrewer[name]
return scheme[length]
end
return colorbrewer

View File

@ -20,7 +20,7 @@
pkgs.mkShell {
LUA_PATH = "vendor/?.lua;;";
name = "love";
packages = [ love ];
packages = [ love pkgs.lua ];
shellHook = ''
mkdir -p vendor
ln -snf ${lume}/lume.lua vendor/lume.lua

View File

@ -17,3 +17,5 @@ function printt(t, prefix, maxdepth)
end
end
end
color = require("colorbrewer")