diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-12-30 12:19:15 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-12-30 12:19:15 +0000 |
commit | a98a8b4506d4dc71b3342b15ae83c91a31090815 (patch) | |
tree | 32de3c3242ac6e60fe666cd919c56c6743e7e98c /main.lua | |
parent | c0b589192625d899bd4b7b20c44491ae58e5cee5 (diff) | |
download | storage-chaos-a98a8b4506d4dc71b3342b15ae83c91a31090815.tar.gz storage-chaos-a98a8b4506d4dc71b3342b15ae83c91a31090815.zip |
Multiple levels, win condition
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 132 |
1 files changed, 101 insertions, 31 deletions
@@ -13,6 +13,15 @@ function love.load() { '#', ' ', '.', '.', ' ', ' ', '#' }, { '#', ' ', ' ', '*', ' ', ' ', '#' }, { '#', '#', '#', '#', '#', '#', '#' } + }, + { + { '#','#','#','#','#','#',' ',' '}, + { '#',' ',' ',' ',' ','#','#','#'}, + { '#',' ','#',' ','$',' ',' ','#'}, + { '#','.','.','.','*','$','@','#'}, + { '#',' ','#',' ','$',' ',' ','#'}, + { '#',' ',' ',' ','#','#','#','#'}, + { '#','#','#','#','#',' ',' ',' '} } } @@ -38,29 +47,44 @@ function love.load() 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 + state = "play" + current_level = 1 + set_level(current_level) +end + +-- return true if all the crates are in storage +function is_level_complete() + for _, row in ipairs(level) do + for _, cell in ipairs(row) do + if cell == '$' then + return false end end - moves = 0 end + return true +end - current_level = 1 - set_level(current_level) +-- set current level based on the index in the levels table +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 +-- perform a move (if possible) function move(target_x, target_y) -- can't push walls if level[player_y + target_y][player_x + target_x] == '#' then @@ -135,13 +159,24 @@ function move(target_x, target_y) level_undo_y = undo_y end -function love.keypressed(key) +-- keypressed handler when the level is compplete +function keypressed_win(key) + if key == "space" then + -- loop if we run out of levels + if current_level == #levels then + current_level = 1 + else + current_level = current_level + 1 + end + -- back to play! + set_level(current_level) + state = "play" + end +end - -- quit game - if key == "escape" then - print("bye bye!") - love.event.quit() - elseif key == "r" then +-- keypressed handler during play +function keypressed_play(key) + if key == "r" then -- reset level set_level(current_level) return @@ -171,6 +206,26 @@ function love.keypressed(key) -- only if we move if target_x ~= 0 or target_y ~= 0 then move(target_x, target_y) + + if is_level_complete() then + win_delay = 0 + state = "win" + end + end +end + +function love.keypressed(key) + + -- quit game + if key == "escape" then + print("bye bye!") + love.event.quit() + end + + if state == "play" then + keypressed_play(key) + elseif state == "win" then + keypressed_win(key) end end @@ -192,16 +247,19 @@ function love.draw() love.graphics.print("Level " .. current_level .. " Moves " .. moves, 20, 5 * game.scale) - love.graphics.print("R:restart", 20, 205 * game.scale) + -- these options anly exist when we are playing + if state == "play" then + 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) + -- 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("ESC:exit", 20 + 17 * 8 * game.scale, 205 * game.scale) + end love.graphics.print("Storage Chaos", 20, 220 * game.scale) @@ -234,6 +292,18 @@ function love.draw() end end + -- if the level is completed, show an verlay + if state == "win" then + if win_delay < 64 then + win_delay = win_delay + 1 + else + love.graphics.setColor(0.3, 0.3, 0.5) + love.graphics.rectangle("fill", 0, 80 * game.scale, game.width * game.scale, 60 * game.scale) + love.graphics.setColor(1.0, 1.0, 1.0) + love.graphics.print("Level completed!", 96 * game.scale, 90 * game.scale) + love.graphics.print("SPACE:next ESC:quit", 80 * game.scale, 115 * game.scale) + end + end end function love.keyreleased(key) |