2024-12-03 18:40:14 -03:00

235 lines
6.2 KiB
Lua

-- main.lua
-- Constants
local TILE_SIZE = 40
local SCREEN_WIDTH = 800
local SCREEN_HEIGHT = 600
local MOV_COOLDOWN = 0.2
local EXPLOSION_TIMEOUT = 0.1
-- Player state
local player = {
x = 5 * TILE_SIZE,
y = 5 * TILE_SIZE,
speed = TILE_SIZE,
bombTimer = 0,
maxBombs = 2,
bombCount = 0,
bombSize = 1,
}
-- Box state
local boxes = {}
-- Bomb state
local bombs = {}
-- Explosions state
local explosions = {}
local time = 1
local function search_box(x, y)
for i, box in ipairs(boxes) do
if box.x == x and box.y == y then
return i
end
end
return nil
end
local function search_bomb(x, y)
for i, bomb in ipairs(bombs) do
if bomb.x == x and bomb.y == y then
return i
end
end
return nil
end
local function generate_box()
local x = math.random(0, SCREEN_WIDTH / TILE_SIZE - 1) * TILE_SIZE
local y = math.random(0, SCREEN_HEIGHT / TILE_SIZE - 1) * TILE_SIZE
local count = 0
while (search_box(x, y) or (x == player.x and y == player.y)) and count < 100 do
x = math.random(0, SCREEN_WIDTH / TILE_SIZE - 1) * TILE_SIZE
y = math.random(0, SCREEN_HEIGHT / TILE_SIZE - 1) * TILE_SIZE
count = count + 1
end
if count == 100 then
print("Não foi possível achar uma posição para a caixa")
return
end
table.insert(boxes, {x = x, y = y})
end
function love.keypressed(key)
if key == "b" then
generate_box()
return
end
if key == "i" then
player.bombSize = player.bombSize + 1
print("Novo tamanho de bomba: ", player.bombSize)
return
end
if key == "o" then
player.bombSize = math.max(0, player.bombSize - 1)
print("Novo tamanho de bomba: ", player.bombSize)
return
end
if key == "n" then
player.maxBombs = player.maxBombs + 1
print("Nova quantidade máxima de bombas: ", player.maxBombs)
return
end
if key == "m" then
player.maxBombs = math.max(0, player.maxBombs - 1)
print("Nova quantidade máxima de bombas: ", player.maxBombs)
return
end
end
-- Set up initial map
function love.load()
love.window.setMode(SCREEN_WIDTH, SCREEN_HEIGHT)
love.graphics.setBackgroundColor(0.1, 0.1, 0.1)
math.randomseed(os.time())
-- Generate some destructible boxes randomly
for i = 1, 10 do
generate_box()
end
end
local function explode_bomb(i)
local x = bombs[i].x
local y = bombs[i].y
table.remove(bombs, i)
player.bombCount = player.bombCount - 1
local adds = {
{x = 1, y = 0},
{x = 0, y = 1},
{x = -1, y = 0},
{x = 0, y = -1}
}
table.insert(explosions, {x = x, y = y, time = 0})
for cur = 1, player.bombSize do
for _, add in ipairs(adds) do
local xd = x + cur * add.x * TILE_SIZE
local yd = y + cur * add.y * TILE_SIZE
if xd >= 0 and xd <= SCREEN_WIDTH - TILE_SIZE and yd >= 0 and yd <= SCREEN_WIDTH - TILE_SIZE then
table.insert(explosions, {x = xd, y = yd, time = 0})
local box = search_box(xd, yd)
if box then
table.remove(boxes, box)
end
end
end
end
end
-- Player movement handling
function love.update(dt)
-- Player movement
time = time + dt
if time >= MOV_COOLDOWN then
if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
if not search_box(player.x, player.y - player.speed) then
player.y = player.y - player.speed
end
end
if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
if not search_box(player.x, player.y + player.speed) then
player.y = player.y + player.speed
end
end
if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
if not search_box(player.x - player.speed, player.y) then
player.x = player.x - player.speed
end
end
if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
if not search_box(player.x + player.speed, player.y) then
player.x = player.x + player.speed
end
end
time = 0
end
-- Ensure player stays inside the screen boundaries
player.x = math.max(0, math.min(SCREEN_WIDTH - TILE_SIZE, player.x))
player.y = math.max(0, math.min(SCREEN_HEIGHT - TILE_SIZE, player.y))
-- Bomb timer (when bomb is placed, it starts ticking down)
for i, bomb in ipairs(bombs) do
bomb.timer = bomb.timer - dt
if bomb.timer <= 0 then
explode_bomb(i)
end
end
for i, explosion in ipairs(explosions) do
explosion.time = explosion.time + dt
if explosion.time >= EXPLOSION_TIMEOUT then
table.remove(explosions, i)
end
end
-- Place bomb
if love.keyboard.isDown("space") then
-- Prevent placing multiple bombs at once
if player.bombTimer <= 0 and player.bombCount < player.maxBombs and not search_bomb(player.x, player.y) then
table.insert(bombs, {x = player.x, y = player.y, timer = 2, size = player.bombSize}) -- Bomb explodes after 2 seconds
player.bombTimer = 0.15
player.bombCount = player.bombCount + 1
end
end
-- Update bomb cooldown
player.bombTimer = math.max(0, player.bombTimer - dt)
end
-- Drawing the game world
function love.draw()
-- Draw destructible boxes
love.graphics.setColor(1, 1, 0)
for _, box in ipairs(boxes) do
love.graphics.rectangle("fill", box.x, box.y, TILE_SIZE, TILE_SIZE)
end
-- Draw player
love.graphics.setColor(0, 0, 1)
love.graphics.circle("fill", player.x + TILE_SIZE / 2, player.y + TILE_SIZE / 2, TILE_SIZE / 2)
-- Draw bombs
love.graphics.setColor(1, 0, 0)
for _, bomb in ipairs(bombs) do
love.graphics.circle("fill", bomb.x + TILE_SIZE / 2, bomb.y + TILE_SIZE / 2, TILE_SIZE / 2)
end
-- Draw explosions
love.graphics.setColor(1, 0, 0)
for _, explosion in ipairs(explosions) do
love.graphics.rectangle("fill", explosion.x, explosion.y, TILE_SIZE, TILE_SIZE)
end
end