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.
|
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).
|
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)
|
function love.conf(t)
|
||||||
t.window.title = "Circle packing"
|
t.window.title = "Circle packing"
|
||||||
|
t.console = true
|
||||||
end
|
end
|
||||||
|
83
main.lua
83
main.lua
@ -1,40 +1,44 @@
|
|||||||
|
love.graphics.setNewFont(45)
|
||||||
|
love.keyboard.setKeyRepeat(true)
|
||||||
|
|
||||||
local Circle = require("Circle")
|
local Circle = require("Circle")
|
||||||
|
|
||||||
local circles = {}
|
local circles = {}
|
||||||
local spots = {}
|
local spots = {}
|
||||||
|
|
||||||
|
local notification_message = "Notification"
|
||||||
|
local notification_transparency = 0
|
||||||
|
local total = 3
|
||||||
|
local speed = 5
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
local stencil_data = love.image.newImageData("stencil.png")
|
local stencil_data = love.image.newImageData("stencil.png")
|
||||||
local w, h = stencil_data:getDimensions()
|
local w, h = stencil_data:getDimensions()
|
||||||
|
love.window.setMode(w, h)
|
||||||
|
|
||||||
for x=0, w-1 do
|
for x = 0, w - 1 do
|
||||||
for y=0, h-1 do
|
for y = 0, h - 1 do
|
||||||
local r, g, b = stencil_data:getPixel(x, y)
|
local r, g, b = stencil_data:getPixel(x, y)
|
||||||
local brigthness = (r + g + b)/3
|
local brigthness = (r + g + b) / 3
|
||||||
if brigthness > 0.9 then
|
if brigthness > 0.9 then table.insert(spots, {x, y}) end
|
||||||
table.insert(spots, { x, y })
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function dist(x1, y1, x2, y2)
|
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
|
end
|
||||||
|
|
||||||
local function isInCircle(x, y)
|
local function isInCircle(x, y)
|
||||||
for _, circle in ipairs(circles) do
|
for _, circle in ipairs(circles) do
|
||||||
local d = dist(circle.x, circle.y, x, y)
|
local d = dist(circle.x, circle.y, x, y)
|
||||||
if d < circle.r then
|
if d < circle.r then return true end
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function areCirclesOverlapping(circle1, circle2)
|
local function areCirclesOverlapping(circle1, circle2)
|
||||||
local d = dist(circle1.x, circle1.y, circle2.x, circle2.y)
|
local d = dist(circle1.x, circle1.y, circle2.x, circle2.y)
|
||||||
if d - 2 < circle1.r + circle2.r then
|
if d - 2 < circle1.r + circle2.r then return true end
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isCircleOverlapping(circle)
|
local function isCircleOverlapping(circle)
|
||||||
@ -49,7 +53,7 @@ local function createCircle()
|
|||||||
local w, h = love.graphics.getDimensions()
|
local w, h = love.graphics.getDimensions()
|
||||||
-- local x = love.math.random(0, w)
|
-- local x = love.math.random(0, w)
|
||||||
-- local y = love.math.random(0, h)
|
-- local y = love.math.random(0, h)
|
||||||
|
|
||||||
local index = love.math.random(1, #spots)
|
local index = love.math.random(1, #spots)
|
||||||
local x = spots[index][1]
|
local x = spots[index][1]
|
||||||
local y = spots[index][2]
|
local y = spots[index][2]
|
||||||
@ -59,33 +63,52 @@ local function createCircle()
|
|||||||
return Circle:new(x, y)
|
return Circle:new(x, y)
|
||||||
end
|
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)
|
function love.update(dt)
|
||||||
local total = 3
|
notification_transparency = notification_transparency * dt * 59
|
||||||
for _=1, total do
|
for _ = 1, total do
|
||||||
local c = createCircle()
|
local c = createCircle()
|
||||||
|
|
||||||
if c then
|
if c then table.insert(circles, c) end
|
||||||
table.insert(circles, c)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, circle in ipairs(circles) do
|
for _, circle in ipairs(circles) do
|
||||||
if circle.growing then
|
if circle.growing then
|
||||||
if isCircleOverlapping(circle) then
|
if isCircleOverlapping(circle) then circle.growing = false end
|
||||||
circle.growing = false
|
if circle:edges() then circle.growing = false end
|
||||||
end
|
|
||||||
if circle:edges() then
|
|
||||||
circle.growing = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
circle:grow(5*dt)
|
circle:grow(speed * dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
for _, circle in ipairs(circles) do
|
love.graphics.setColor(1, 1, 1)
|
||||||
circle:draw()
|
for _, circle in ipairs(circles) do circle:draw() end
|
||||||
end
|
|
||||||
|
love.graphics.setColor(0.8, 0.1, 0.1, notification_transparency)
|
||||||
|
love.graphics.print(notification_message, 10, 10)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user