diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-12-28 16:11:24 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-12-28 16:16:00 +0000 |
commit | 4f8d6dd049d4f4ffa1373d084dc800de3d80705b (patch) | |
tree | 12148f9cbf577a9d453621ae20d2e851ee5e000f /main.lua | |
download | storage-chaos-4f8d6dd049d4f4ffa1373d084dc800de3d80705b.tar.gz storage-chaos-4f8d6dd049d4f4ffa1373d084dc800de3d80705b.zip |
Initial import
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..e5aed19 --- /dev/null +++ b/main.lua @@ -0,0 +1,240 @@ +require("game") + +function love.load() + + -- the level data is a matrix! + -- limitation: there MUST be walls limiting the level + levels = { + { + { '#', '#', '#', '#', '#', '#', '#' }, + { '#', '.', '@', ' ', '#', ' ', '#' }, + { '#', '$', '*', ' ', '$', ' ', '#' }, + { '#', ' ', ' ', ' ', '$', ' ', '#' }, + { '#', ' ', '.', '.', ' ', ' ', '#' }, + { '#', ' ', ' ', '*', ' ', ' ', '#' }, + { '#', '#', '#', '#', '#', '#', '#' } + } + } + + -- use a monospaced font (TODO: licence) + font = love.graphics.newFont("data/Monocraft.otf", 12 * game.scale) + + -- load graphics + ground = love.graphics.newImage("data/ground.png") + crate = love.graphics.newImage("data/crate.png") + wall = love.graphics.newImage("data/wall.png") + goal = love.graphics.newImage("data/goal.png") + crate_goal = love.graphics.newImage("data/crate_goal.png") + + player_frames = { + love.graphics.newImage("data/player_01.png"), + love.graphics.newImage("data/player_02.png") + } + player_frame = 1 + player_delay = 0 + + -- where the player is + player_x = 0 + player_y = 0 + moves = 0 + + function set_level(number) + level_undo = {} + level_undo_x = 0 + level_undo_y = 0 + + level = {} + for y, row in ipairs(levels[number]) do + level[y] = {} + for x, cell in ipairs(row) do + level[y][x] = cell + if cell == '@' or cell == '+' then + player_x = x + player_y = y + end + end + end + moves = 0 + end + + current_level = 1 + set_level(current_level) +end + +function move(target_x, target_y) + -- can't push walls + if level[player_y + target_y][player_x + target_x] == '#' then + return + end + + local current = level[player_y][player_x] + local destination = level[player_y + target_y][player_x + target_x] + + -- copy current level and player position to use it for undo + local undo = {} + local undo_x = 0 + local undo_y = 0 + for y, row in ipairs(level) do + undo[y] = {} + for x, cell in ipairs(row) do + undo[y][x] = cell + if cell == '@' or cell == '+' then + undo_x = x + undo_y = y + end + end + end + + -- pushing a box + if destination == '$' or destination == '*' then + local destination_crate = level[player_y + target_y * 2][player_x + target_x * 2] + -- can we move it? + if destination_crate == ' ' or destination_crate == '.' then + -- first erase the current position + if destination == '$' then + level[player_y + target_y][player_x + target_x] = ' ' + destination = ' ' + else + level[player_y + target_y][player_x + target_x] = '.' + destination = '.' + end + + -- second draw on the new one + if destination_crate == ' ' then + level[player_y + target_y * 2][player_x + target_x * 2] = '$' + else + level[player_y + target_y * 2][player_x + target_x * 2] = '*' + end + else + -- we can't, so we don't move + return + end + end + + -- first erase the current position + if current == '@' then + level[player_y][player_x] = ' ' + else + level[player_y][player_x] = '.' + end + + -- second draw on the new one + if destination == ' ' then + level[player_y + target_y][player_x + target_x] = '@' + else + level[player_y + target_y][player_x + target_x] = '+' + end + + player_x = player_x + target_x + player_y = player_y + target_y + moves = moves + 1 + + -- save the previous move for UNDO + level_undo = undo + level_undo_x = undo_x + level_undo_y = undo_y +end + +function love.keypressed(key) + + -- quit game + if key == "escape" then + print("bye bye!") + love.event.quit() + elseif key == "r" then + -- reset level + set_level(current_level) + return + elseif key == "u" and #level_undo > 0 then + -- undo move + level = level_undo + player_x = level_undo_x + player_y = level_undo_y + level_undo = {} + moves = moves - 1 + end + + -- where are we moving + local target_x = 0 + local target_y = 0 + + if key == "up" then + target_y = -1 + elseif key == "down" then + target_y = 1 + elseif key == "left" then + target_x = -1 + elseif key=="right" then + target_x = 1 + end + + -- only if we move + if target_x ~= 0 or target_y ~= 0 then + move(target_x, target_y) + end +end + +function love.update(dt) + player_delay = player_delay + dt * 3 + if player_delay > 1 then + player_delay = 0 + if player_frame == 1 then + player_frame = 2 + else + player_frame = 1 + end + end +end + +function love.draw() + + love.graphics.setFont(font) + + love.graphics.print("Level " .. current_level .. " Moves " .. moves, 20, 5 * game.scale) + + love.graphics.print("R:restart", 20, 205 * game.scale) + + -- change colour if there's not undo + if #level_undo == 0 then + love.graphics.setColor(0.5, 0.3, 0.3) + end + love.graphics.print("U:undo", 20 + 10 * 8 * game.scale, 205 * game.scale) + love.graphics.setColor(1, 1, 1) + + love.graphics.print("ESC:exit", 20 + 17 * 8 * game.scale, 205 * game.scale) + + love.graphics.print("Storage Chaos", 20, 220 * game.scale) + + for y, row in ipairs(level) do + for x, cell in ipairs(row) do + -- for testing + -- love.graphics.print(cell, 10 + x * 12 * game.scale, 10 + y * 12 * game.scale) + + draw_x = 10 + x * 64 + draw_y = 10 + y * 64 + + -- always draw the ground + love.graphics.draw(ground, draw_x, draw_y) + if cell == '#' then + love.graphics.draw(wall, draw_x, draw_y) + elseif cell == '$' then + love.graphics.draw(crate, draw_x, draw_y) + elseif cell == '.' then + love.graphics.draw(goal, draw_x, draw_y) + elseif cell == '*' then + love.graphics.draw(crate_goal, draw_x, draw_y) + end + + if cell == '@' then + love.graphics.draw(player_frames[player_frame], draw_x, draw_y) + elseif cell == '+' then + love.graphics.draw(goal, draw_x, draw_y) + love.graphics.draw(player_frames[player_frame], draw_x, draw_y) + end + end + end + +end + +function love.keyreleased(key) +end |