245 lines
No EOL
6.9 KiB
Lua
245 lines
No EOL
6.9 KiB
Lua
require 'util'
|
|
require 'drawutil'
|
|
require 'def'
|
|
|
|
local obj = require 'obj'
|
|
|
|
width = 64
|
|
height = 64
|
|
|
|
tilesize = 32
|
|
zoom = 1
|
|
|
|
world = {}
|
|
|
|
sprites = {}
|
|
|
|
money = 0
|
|
|
|
objects = {}
|
|
|
|
local selectedTile = 1
|
|
local defaultTileRotation = 0
|
|
|
|
function love.load()
|
|
function getSprites(d)
|
|
local dir = "assets/sprites"
|
|
if d then
|
|
dir = dir .. "/" .. d
|
|
end
|
|
local files = love.filesystem.getDirectoryItems(dir)
|
|
for _,file in ipairs(files) do
|
|
if string.sub(file, -4) == ".png" then
|
|
local spritename = string.sub(file, 1, -5)
|
|
local sprite = love.graphics.newImage(dir .. "/" .. file)
|
|
if d then
|
|
spritename = d .. "/" .. spritename
|
|
end
|
|
sprites[spritename] = sprite
|
|
elseif love.filesystem.getInfo(dir .. "/" .. file).type == "directory" then
|
|
local newdir = file
|
|
if d then
|
|
newdir = d .. "/" .. newdir
|
|
end
|
|
getSprites(file)
|
|
end
|
|
end
|
|
end
|
|
|
|
getSprites()
|
|
|
|
for x = 1, width do
|
|
world[x] = {}
|
|
for y = 1, height do
|
|
world[x][y] = {0}
|
|
end
|
|
end
|
|
|
|
love.graphics.setDefaultFilter('nearest', 'nearest')
|
|
end
|
|
|
|
local timer = 0
|
|
function love.update(dt)
|
|
--dt = dt * 0.1
|
|
|
|
for x = 1, width do
|
|
for y = 1, height do
|
|
local tileid = getTile(x, y)
|
|
if tileid[1] ~= 0 then
|
|
local tile = tiles[tileid[1]]
|
|
|
|
if tile.type == 'mine' then
|
|
if ((timer + dt + x * 0.25 + y * 0.5) % (1/tile.obtains.count)) < ((timer + x * 0.25 + y * 0.5) % (1/tile.obtains.count)) then
|
|
local a = -(tileid.rotation or 0) * 90
|
|
local x = x + 0.5 - math.sin(math.rad(a)) * 0.7
|
|
local y = y + 0.5 - math.cos(math.rad(a)) * 0.7
|
|
|
|
table.insert(objects, {
|
|
x = x,
|
|
y = y,
|
|
size = math.random(30, 40)/100,
|
|
type = tile.obtains.id,
|
|
color = {0.5, 0.4, 0.4},
|
|
})
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
timer = timer + dt
|
|
|
|
for i,o in ipairs(objects) do
|
|
obj.updateObject(i, o, dt)
|
|
end
|
|
end
|
|
|
|
function love.draw()
|
|
love.graphics.setBackgroundColor(0.8, 0.7, 0.7)
|
|
|
|
-- layer 1
|
|
for x = 1, width do
|
|
for y =1, height do
|
|
local drawx = x * tilesize
|
|
local drawy = y * tilesize
|
|
|
|
love.graphics.setColor(0.4, 0.3, 0.3)
|
|
love.graphics.rectangle('line', drawx, drawy, tilesize, tilesize)
|
|
|
|
local tileid = getTile(x, y)
|
|
|
|
if tileid[1] ~= 0 then
|
|
local tile = tiles[tileid[1]]
|
|
|
|
local sprite = s('tiles/' .. tile.name)
|
|
|
|
if sprite then
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.draw(sprite, drawx + tilesize/2, drawy + tilesize/2, math.rad((tileid.rotation or 0) * 90), 1, 1, tilesize/2, tilesize/2)
|
|
else
|
|
love.graphics.setColor(0, 0, 0)
|
|
love.graphics.rectangle('fill', drawx, drawy, tilesize, tilesize)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- layer 2
|
|
for x = 0, width do
|
|
for y = 0, height do
|
|
local drawx = x * tilesize
|
|
local drawy = y * tilesize
|
|
|
|
local hovered = mouseInsideRectangle(drawx, drawy, tilesize + 1, tilesize + 1)
|
|
|
|
if hovered then
|
|
love.graphics.setColor(0.8, 0.7, 0.7)
|
|
love.graphics.rectangle('line', drawx, drawy, tilesize, tilesize)
|
|
end
|
|
end
|
|
end
|
|
|
|
for _, o in ipairs(objects) do
|
|
local trans = 1 - (o.despawntimer / 5)
|
|
|
|
love.graphics.setColor({o.color[1], o.color[2], o.color[3], trans})
|
|
love.graphics.rectangle('fill', o.x * tilesize - o.size * tilesize / 2, o.y * tilesize - o.size * tilesize / 2, o.size * tilesize, o.size * tilesize)
|
|
love.graphics.setColor(0, 0, 0, trans)
|
|
love.graphics.rectangle('line', o.x * tilesize - o.size * tilesize / 2, o.y * tilesize - o.size * tilesize / 2, o.size * tilesize, o.size * tilesize)
|
|
end
|
|
|
|
local hoveredTileId = getCameraTile(love.mouse.getX(), love.mouse.getY())
|
|
if hoveredTileId[1] ~= 0 then
|
|
local tmaxwidth = 256
|
|
local padding = 4
|
|
local offset = 16
|
|
|
|
local hoveredTile = tiles[hoveredTileId[1]]
|
|
|
|
local text = hoveredTile.displayname .. '\n' .. hoveredTile.desc
|
|
local textObj = love.graphics.newText(love.graphics.getFont(), text)
|
|
textObj:setf(text, tmaxwidth, 'left')
|
|
|
|
local twidth, theight = textObj:getDimensions()
|
|
local mx, my = love.mouse.getPosition()
|
|
local tilex = math.floor(love.mouse.getX() / tilesize) * tilesize
|
|
local tiley = math.floor(love.mouse.getY() / tilesize) * tilesize
|
|
|
|
love.graphics.setColor(0.9, 0.9, 0.9, 0.7)
|
|
love.graphics.rectangle('fill', mx + offset, my + offset, twidth + padding * 2, theight + padding * 2)
|
|
love.graphics.setColor(0, 0, 0, 0.7)
|
|
love.graphics.draw(textObj, mx + padding + offset, my + padding + offset)
|
|
|
|
if hoveredTileId.rotation then
|
|
love.graphics.setColor(1, 1, 1, 0.7)
|
|
local a = -hoveredTileId.rotation * 90
|
|
arrow(tilex, tiley, tilesize, tilesize, a)
|
|
end
|
|
else
|
|
if selectedTile ~= 0 then
|
|
local drawTile = tiles[selectedTile]
|
|
local tilex = math.floor(love.mouse.getX() / tilesize) * tilesize
|
|
local tiley = math.floor(love.mouse.getY() / tilesize) * tilesize
|
|
|
|
love.graphics.setColor(1, 1, 1, 0.5)
|
|
|
|
local sprite = s('tiles/' .. drawTile.name)
|
|
|
|
if sprite then
|
|
love.graphics.draw(sprite, tilex + tilesize/2, tiley + tilesize/2, math.rad((defaultTileRotation or 0) * 90), 1, 1, tilesize/2, tilesize/2)
|
|
else
|
|
love.graphics.rectangle('fill', tilex, tiley, tilesize, tilesize)
|
|
end
|
|
end
|
|
end
|
|
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.print(love.timer.getFPS() .. 'fps\n' .. money .. '$')
|
|
end
|
|
|
|
function love.mousepressed(x, y, button)
|
|
if button == 1 then
|
|
local tileid = getCameraTile(x, y)
|
|
if tileid[1] ~= 0 then
|
|
local tile = tiles[tileid[1]]
|
|
if tile.type == 'clicker' then
|
|
local a = -tileid.rotation * 90 + math.random(-10, 10)/10
|
|
local x = math.floor(x/tilesize) + 0.5 - math.sin(math.rad(a)) * 0.7
|
|
local y = math.floor(y/tilesize) + 0.5 - math.cos(math.rad(a)) * 0.7
|
|
|
|
for i = 1, tile.obtains.count do
|
|
table.insert(objects, {
|
|
x = x,
|
|
y = y,
|
|
size = math.random(30, 40)/100,
|
|
type = tile.obtains.id,
|
|
color = {0.5, 0.4, 0.4},
|
|
})
|
|
end
|
|
end
|
|
else
|
|
if selectedTile ~= 0 then
|
|
world[math.floor(x/tilesize)][math.floor(y/tilesize)] = {selectedTile, rotation = defaultTileRotation}
|
|
end
|
|
end
|
|
elseif button == 2 then
|
|
local tileid = getCameraTile(x, y)
|
|
if tileid[1] ~= 0 then
|
|
world[math.floor(x/tilesize)][math.floor(y/tilesize)] = {0}
|
|
end
|
|
end
|
|
end
|
|
|
|
function love.wheelmoved(x, y)
|
|
local mx, my = love.mouse.getPosition()
|
|
local tileid = getCameraTile(mx, my)
|
|
|
|
if tileid[1] ~= 0 then
|
|
local rot = math.min(math.max(((tileid.rotation or 0) + math.floor(y)) % 4, 0), 3)
|
|
defaultTileRotation = rot
|
|
tileid.rotation = rot
|
|
world[math.floor(mx/tilesize)][math.floor(my/tilesize)] = tileid
|
|
else
|
|
selectedTile = math.min(math.max(selectedTile + math.floor(y), 0), #tiles)
|
|
end
|
|
end |