1
0

feat: Add controls to update paramaters

This commit is contained in:
Rokas Puzonas 2021-06-01 21:46:26 +03:00
parent 7705c88936
commit 78213b2eeb
3 changed files with 60 additions and 30 deletions

View File

@ -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

View File

@ -1,3 +1,4 @@
function love.conf(t)
t.window.title = "Circle packing"
t.console = true
end

View File

@ -1,40 +1,44 @@
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
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
local brigthness = (r + g + b) / 3
if brigthness > 0.9 then table.insert(spots, {x, y}) end
end
end
end
local function dist(x1, y1, x2, y2)
return ((x1-x2)^2 + (y1-y2)^2)^0.5
return ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ 0.5
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)
@ -49,7 +53,7 @@ local function createCircle()
local w, h = love.graphics.getDimensions()
-- local x = love.math.random(0, w)
-- local y = love.math.random(0, h)
local index = love.math.random(1, #spots)
local x = spots[index][1]
local y = spots[index][2]
@ -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
for _=1, total do
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
end
if circle:edges() then
circle.growing = false
end
if isCircleOverlapping(circle) then circle.growing = false 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