factorite/main.lua

265 lines
7.3 KiB
Lua
Raw Normal View History

2020-10-04 01:13:34 +00:00
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 = {}
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()
2020-10-04 01:18:24 +00:00
for x = 1, width do
world[x] = {}
for y = 1, height do
world[x][y] = {0}
end
end
love.graphics.setDefaultFilter('nearest', 'nearest')
world[1][1] = {2, rotation = 2}
world[2][1] = {2, rotation = 2}
world[3][1] = {2, rotation = 2}
world[4][1] = {2, rotation = 2}
world[5][1] = {2, rotation = 2}
world[1][3] = {2, rotation = 0}
world[2][3] = {2, rotation = 0}
world[3][3] = {2, rotation = 0}
world[4][3] = {2, rotation = 1}
world[1][2] = {3, rotation = 1}
world[2][2] = {3, rotation = 1}
world[3][2] = {3, rotation = 1}
world[4][2] = {3, rotation = 1}
world[5][2] = {3, rotation = 2}
world[5][3] = {3, rotation = 2}
world[5][4] = {4, rotation = 0}
2020-10-04 01:13:34 +00:00
end
local timer = 0
function love.update(dt)
2020-10-04 01:18:24 +00:00
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 = 0.3,
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}
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
if not pass then
o.velx = -o.velx
o.vely = -o.vely
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
2020-10-04 01:13:34 +00:00
end
function love.draw()
2020-10-04 01:18:24 +00:00
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
love.graphics.setColor(o.color)
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)
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
end
love.graphics.setColor(1, 1, 1)
love.graphics.print(love.timer.getFPS() .. 'fps\n' .. money .. '$')
2020-10-04 01:13:34 +00:00
end
function love.mousepressed(x, y, button)
2020-10-04 01:18:24 +00:00
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 = 0.3,
type = tile.obtains.id,
color = {0.5, 0.4, 0.4},
})
end
end
end
end
2020-10-04 01:13:34 +00:00
end