KEYS:
R: Return to menu
Enter: Toggles lines
Code: Select all
-- LÖVE Code Doodle #9
-- by HugoBDesigner
function love.load()
seed = ""
state = "start"
map = {}
mapsize = 16
levels = 5
spread = 1
basicColors = {{255, 255, 0}, {255, 0, 0}}
levels_colors = {}
economicmode = true
font = love.graphics.newFont()
buttons = {}
local txt = "Map size: "
table.insert(buttons, newButton(100+font:getWidth(txt), 300+font:getHeight()/2+20, 10, 10, "<", "mapsize", "-", 8))
table.insert(buttons, newButton(100+font:getWidth(txt)+10+5+font:getWidth(tostring(mapsize))+5, 300+font:getHeight()/2+20, 10, 10, ">", "mapsize", "+", 64))
txt = "Levels: "
table.insert(buttons, newButton(100+font:getWidth(txt), 300+font:getHeight()/2+20+30, 10, 10, "<", "levels", "-", 2))
table.insert(buttons, newButton(100+font:getWidth(txt)+10+5+font:getWidth(tostring(levels))+5, 300+font:getHeight()/2+20+30, 10, 10, ">", "levels", "+", 8))
txt = "Spreading level: "
table.insert(buttons, newButton(100+font:getWidth(txt), 300+font:getHeight()/2+20+60, 10, 10, "<", "spread", "-", 1))
table.insert(buttons, newButton(100+font:getWidth(txt)+10+5+font:getWidth(tostring(levels))+5, 300+font:getHeight()/2+20+60, 10, 10, ">", "spread", "+", 5))
end
function love.update(dt)
end
function love.draw()
love.graphics.setColor(255, 255, 255, 255)
if state == "game" then
for x = 1, mapsize do
for y = 1, mapsize do
local recsize = 8
local px = 400-mapsize/2*recsize+(x-1)*recsize
local py = 300-mapsize/2*recsize+(y-1)*recsize
love.graphics.setColor(levels_colors[map[x][y]])
love.graphics.rectangle("fill", px, py, recsize, recsize)
if not economicmode then
local r, g, b = unpack(levels_colors[map[x][y]])
love.graphics.setColor(r*.5, g*.5, b*.5)
love.graphics.rectangle("line", px, py, recsize, recsize)
end
end
end
else
love.graphics.print("Type in your seed:", 100, 300-font:getHeight()/2 - 20 - font:getHeight())
love.graphics.setColor(155, 155, 155, 255)
love.graphics.rectangle("fill", 100, 300-font:getHeight()/2-5, 800-100*2, font:getHeight()+5*2)
love.graphics.setColor(0, 0, 0, 255)
love.graphics.print(seed, 100+5, 300-font:getHeight()/2)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print("Map size:", 100, 300+font:getHeight()/2+20+5-font:getHeight()/2)
love.graphics.print("Levels:", 100, 300+font:getHeight()/2+20+30+5-font:getHeight()/2)
love.graphics.print("Spreading level:", 100, 300+font:getHeight()/2+20+60+5-font:getHeight()/2)
love.graphics.print(tostring(mapsize), 100+font:getWidth("Map size: ")+10+5, 300+font:getHeight()/2+20+5-font:getHeight()/2)
love.graphics.print(tostring(levels), 100+font:getWidth("Levels: ")+10+5, 300+font:getHeight()/2+20+30+5-font:getHeight()/2)
love.graphics.print(tostring(spread), 100+font:getWidth("Spreading level: ")+10+5, 300+font:getHeight()/2+20+60+5-font:getHeight()/2)
for i, v in ipairs(buttons) do
love.graphics.setColor(155, 155, 155, 255)
love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
love.graphics.setColor(55, 55, 55, 255)
love.graphics.rectangle("line", v.x, v.y, v.width, v.height)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(v.text, v.x+v.width/2-font:getWidth(v.text)/2, v.y+v.height/2-font:getHeight()/2)
end
end
end
function love.keypressed(key, unicode)
if state == "start" then
if key == "backspace" then
if string.len(seed) > 1 then seed = string.sub(seed, 1, string.len(seed)-1)
else seed = "" end
elseif key == "enter" or key == "return" or key == "kpenter" then
if string.len(seed) >= 1 then
state = "game"
renderSeed(seed)
end
elseif string.len(seed) < 15 then --let's not let giant texts, shall we?
able = "abcdefghijklmnopqrstuvwxyz0123456789 "
for i = 1, string.len(able) do
if key == string.sub(able, i, i) then
seed = seed .. key
break
end
end
end
else
if key == "enter" or key == "return" or key == "kpenter" then
economicmode = not economicmode
elseif key == "r" then
state = "start"
end
end
if key == "escape" then
love.event.quit()
end
end
function love.mousepressed(x, y, button)
for i, v in ipairs(buttons) do
if inside(x, y, 0, 0, v.x, v.y, v.width, v.height) then
buttonPressed(v)
break
end
end
buttons[2].x = 100+font:getWidth("Map size: ")+10+5+font:getWidth(tostring(mapsize))+5
buttons[4].x = 100+font:getWidth("Levels: ")+10+5+font:getWidth(tostring(levels))+5
buttons[6].x = 100+font:getWidth("Spreading level: ")+10+5+font:getWidth(tostring(spread))+5
end
function renderSeed(seed)
local a = ""
for i = 1, string.len(seed) do
a = a .. tostring(string.byte(string.sub(seed, i, i)))
end
math.randomseed(tonumber(a))
levels_colors = {basicColors[1]}
for i = 2, levels-1 do
table.insert(levels_colors, fade(i-1, levels-1, basicColors[1], basicColors[2]))
end
table.insert(levels_colors, basicColors[2])
for x = 1, mapsize do
map[x] = {}
for y = 1, mapsize do
map[x][y] = 1
end
end
for x = 1, mapsize do
for y = 1, mapsize do
local n = math.random(levels*spread)
if n == levels*spread then
map[x][y] = levels
for i = 1, levels-1 do
local n = levels-i
--[[ First the four sides of the tile, like so:
-- ___#___
-- ___#___
-- ___#___
-- ###@###
-- ___#___
-- ___#___
-- ___#___]]
if x-i >= 1 then
map[x-i][y] = math.max(map[x-i][y], n)
end
if x+i <= mapsize then
map[x+i][y] = math.max(map[x+i][y], n)
end
if y-i >= 1 then
map[x][y-i] = math.max(map[x][y-i], n)
end
if y+i <= mapsize then
map[x][y+i] = math.max(map[x][y+i], n)
end
--[[ Later the diagonals, cause they're important!
-- _______
-- __#_#__
-- _##_##_
-- ___@___
-- _##_##_
-- __#_#__
-- _______]]
for j = 1, i-1 do
if y-i+j >= 1 and x-j >= 1 then
map[x-j][y-i+j] = math.max(map[x-j][y-i+j], n)
end
if y-i+j >= 1 and x+j <= mapsize then
map[x+j][y-i+j] = math.max(map[x+j][y-i+j], n)
end
if y+i-j <= mapsize and x-j >= 1 then
map[x-j][y+i-j] = math.max(map[x-j][y+i-j], n)
end
if y+i-j <= mapsize and x+j <= mapsize then
map[x+j][y+i-j] = math.max(map[x+j][y+i-j], n)
end
end
end
end
end
end
end
function newButton(x, y, w, h, t, var, f, lim)
return {x = x, y = y, width = w, height = h, text = t, variable = var, func = f, limit = lim}
end
function buttonPressed(b)
if b.func == "+" or b.func == "add" then
_G[b.variable] = math.min(_G[b.variable]+1, b.limit)
elseif b.func == "-" or b.func == "sub" then
_G[b.variable] = math.max(_G[b.variable]-1, b.limit)
end
end
function inside(x1, y1, w1, h1, x2, y2, w2, h2)
if x1 >= x2 and y1 >= y2 and x1+w1 < x2+w2 and y1+h1 < y2+h2 then
return true
end
return false
end
function fade(currenttime, maxtime, c1, c2)
local tp = currenttime/maxtime
local ret = {} --return color
for i = 1, #c1 do
ret[i] = c1[i]+(c2[i]-c1[i])*tp
ret[i] = math.max(ret[i], 0)
ret[i] = math.min(ret[i], 255)
end
return ret
end