feat: Add controls to update paramaters
This commit is contained in:
parent
7705c88936
commit
78213b2eeb
@ -10,3 +10,9 @@ So yeah, this is my implementation of that, but with Lua and a couple extra feat
|
||||
To run this project you need to install [Love2D](https://love2d.org) yourself.
|
||||
There is a great tutorial on how to do all of this [here](https://love2d.org/wiki/Getting_Started).
|
||||
|
||||
## Controls
|
||||
|
||||
* Space - Remove all circles
|
||||
* Q/E - Decrease/increase speed
|
||||
* A/D - Decrease/increase density
|
||||
|
||||
|
1
conf.lua
1
conf.lua
@ -1,3 +1,4 @@
|
||||
function love.conf(t)
|
||||
t.window.title = "Circle packing"
|
||||
t.console = true
|
||||
end
|
||||
|
71
main.lua
71
main.lua
@ -1,18 +1,26 @@
|
||||
love.graphics.setNewFont(45)
|
||||
love.keyboard.setKeyRepeat(true)
|
||||
|
||||
local Circle = require("Circle")
|
||||
|
||||
local circles = {}
|
||||
local spots = {}
|
||||
|
||||
local notification_message = "Notification"
|
||||
local notification_transparency = 0
|
||||
local total = 3
|
||||
local speed = 5
|
||||
|
||||
function love.load()
|
||||
local stencil_data = love.image.newImageData("stencil.png")
|
||||
local w, h = stencil_data:getDimensions()
|
||||
love.window.setMode(w, h)
|
||||
|
||||
for x = 0, w - 1 do
|
||||
for y = 0, h - 1 do
|
||||
local r, g, b = stencil_data:getPixel(x, y)
|
||||
local brigthness = (r + g + b) / 3
|
||||
if brigthness > 0.9 then
|
||||
table.insert(spots, { x, y })
|
||||
end
|
||||
if brigthness > 0.9 then table.insert(spots, {x, y}) end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -24,17 +32,13 @@ end
|
||||
local function isInCircle(x, y)
|
||||
for _, circle in ipairs(circles) do
|
||||
local d = dist(circle.x, circle.y, x, y)
|
||||
if d < circle.r then
|
||||
return true
|
||||
end
|
||||
if d < circle.r then return true end
|
||||
end
|
||||
end
|
||||
|
||||
local function areCirclesOverlapping(circle1, circle2)
|
||||
local d = dist(circle1.x, circle1.y, circle2.x, circle2.y)
|
||||
if d - 2 < circle1.r + circle2.r then
|
||||
return true
|
||||
end
|
||||
if d - 2 < circle1.r + circle2.r then return true end
|
||||
end
|
||||
|
||||
local function isCircleOverlapping(circle)
|
||||
@ -59,33 +63,52 @@ local function createCircle()
|
||||
return Circle:new(x, y)
|
||||
end
|
||||
|
||||
local function showNotification(msg)
|
||||
notification_transparency = 1
|
||||
notification_message = msg
|
||||
end
|
||||
|
||||
function love.keypressed(key)
|
||||
if key == "space" then
|
||||
showNotification("Reset")
|
||||
circles = {}
|
||||
elseif key == "e" then
|
||||
showNotification("Increase speed")
|
||||
speed = speed * 1.2
|
||||
elseif key == "q" then
|
||||
showNotification("Decrease speed")
|
||||
speed = speed * 0.8
|
||||
elseif key == "d" then
|
||||
showNotification("Increase density")
|
||||
total = total * 1.2
|
||||
elseif key == "a" then
|
||||
showNotification("Decrease density")
|
||||
total = total * 0.8
|
||||
end
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
local total = 3
|
||||
notification_transparency = notification_transparency * dt * 59
|
||||
for _ = 1, total do
|
||||
local c = createCircle()
|
||||
|
||||
if c then
|
||||
table.insert(circles, c)
|
||||
end
|
||||
if c then table.insert(circles, c) end
|
||||
end
|
||||
|
||||
for _, circle in ipairs(circles) do
|
||||
if circle.growing then
|
||||
if isCircleOverlapping(circle) then
|
||||
circle.growing = false
|
||||
if isCircleOverlapping(circle) then circle.growing = false end
|
||||
if circle:edges() then circle.growing = false end
|
||||
end
|
||||
if circle:edges() then
|
||||
circle.growing = false
|
||||
end
|
||||
end
|
||||
circle:grow(5*dt)
|
||||
circle:grow(speed * dt)
|
||||
end
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
for _, circle in ipairs(circles) do
|
||||
circle:draw()
|
||||
end
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
for _, circle in ipairs(circles) do circle:draw() end
|
||||
|
||||
love.graphics.setColor(0.8, 0.1, 0.1, notification_transparency)
|
||||
love.graphics.print(notification_message, 10, 10)
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user