require 'util' require 'drawutil' require 'def' -- crusty swamp ass language An Awesome Project width = 64 height = 64 tilesize = 32 zoom = 1 world = {} sprites = {} money = 0 local 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) 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 o.x = o.x or 0 o.y = o.y or 0 o.velx = o.velx or 0 o.vely = o.vely or 0 o.size = o.size or 0.5 o.color = o.color or {1, 1, 1} o.despawntimer = o.despawntimer or 0 local pass = true local object = objectTypes[o.type] local onTileId = getTile(math.floor(o.x), math.floor(o.y)) if onTileId[1] ~= 0 then local onTile = tiles[onTileId[1]] local a = -(onTileId.rotation or 0) * 90 if onTile.type == 'conveyor' then o.velx = o.velx - math.sin(math.rad(a)) * onTile.speed o.vely = o.vely - math.cos(math.rad(a)) * onTile.speed elseif onTile.type == 'seller' then local movementAngle = math.deg(math.atan2(o.vely, o.velx)) - 90 if onTileId.rotation then local sellerAngle = onTileId.rotation * 90 if math.abs(movementAngle - sellerAngle) < 45 then table.remove(objects, i) money = money + object.cost else pass = false end end else pass = false end end --[[ for i2,o2 in ipairs(objects) do if i ~= i2 and rectangleTouchingRectangle(o.x - o.size, o.y - o.size, o.size, o.size, o2.x - o2.size, o2.y - o2.size, o2.size, o2.size) then pass = false end end -- this doesnt work ]] if not pass then o.velx = -o.velx o.vely = -o.vely end if math.abs(o.velx) < 0.01 and math.abs(o.vely) < 0.01 then o.despawntimer = o.despawntimer + dt else o.despawntimer = 0 end if o.despawntimer > 5 then table.remove(objects, i) end o.x = o.x + o.velx * dt o.y = o.y + o.vely * dt o.velx = o.velx * 0.8 o.vely = o.vely * 0.8 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 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