From ae3ee8fcb2b621be685f280c5f560bc65efe6926 Mon Sep 17 00:00:00 2001 From: jill Date: Sun, 4 Oct 2020 14:57:05 +0300 Subject: [PATCH] temp block placing --- main.lua | 86 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/main.lua b/main.lua index 387f8fc..cc29d81 100644 --- a/main.lua +++ b/main.lua @@ -16,6 +16,9 @@ money = 0 local objects = {} +local selectedTile = 1 +local defaultTileRotation = 0 + function love.load() function getSprites(d) local dir = "assets/sprites" @@ -51,26 +54,6 @@ function love.load() 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} end local timer = 0 @@ -90,7 +73,7 @@ function love.update(dt) table.insert(objects, { x = x, y = y, - size = 0.3, + size = math.random(30, 40)/100, type = tile.obtains.id, color = {0.5, 0.4, 0.4}, }) @@ -109,6 +92,7 @@ function love.update(dt) 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] @@ -137,17 +121,30 @@ function love.update(dt) 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 @@ -201,9 +198,11 @@ function love.draw() end for _, o in ipairs(objects) do - love.graphics.setColor(o.color) + 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) + 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 @@ -234,6 +233,22 @@ function love.draw() 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) @@ -254,12 +269,35 @@ function love.mousepressed(x, y, button) table.insert(objects, { x = x, y = y, - size = 0.3, + 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 \ No newline at end of file