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) function love.conf(t)
t.window.highdpi = false t.window.highdpi = true
t.window.title = "ants" t.window.title = "ants"
end end

View File

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

View File

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

View File

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

View File

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