aboutsummaryrefslogtreecommitdiff
path: root/main.lua
blob: 832afea388dd62305c54f37d8bbc76ecedcc7629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
require("game")

function love.load()

    -- the level data is a matrix!
    -- limitation: there MUST be walls limiting the level
    levels = {
        {
            { '#', '#', '#', '#', '#', '#', '#' },
            { '#', '.', '@', ' ', '#', ' ', '#' },
            { '#', '$', '*', ' ', '$', ' ', '#' },
            { '#', ' ', ' ', ' ', '$', ' ', '#' },
            { '#', ' ', '.', '.', ' ', ' ', '#' },
            { '#', ' ', ' ', '*', ' ', ' ', '#' },
            { '#', '#', '#', '#', '#', '#', '#' }
        },
        {
            { '#', '#', '#', '#', '#', '#', 'x', 'x' },
            { '#', ' ', ' ', ' ', ' ', '#', '#', '#' },
            { '#', ' ', '#', ' ', '$', ' ', ' ', '#' },
            { '#', '.', '.', '.', '*', '$', '@', '#' },
            { '#', ' ', '#', ' ', '$', ' ', ' ', '#' },
            { '#', ' ', ' ', ' ', '#', '#', '#', '#' },
            { '#', '#', '#', '#', '#', 'x', 'x', 'x' }
        }
    }

    -- 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")

    -- load sounds
    move_sound = love.audio.newSource("data/move.wav", "static")
    crate_sound = love.audio.newSource("data/crate_move.wav", "static")
    undo_sound = love.audio.newSource("data/undo.wav", "static")
    restart_sound = love.audio.newSource("data/restart.wav", "static")
    win_sound = love.audio.newSource("data/win.wav", "static")

    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

    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
    end
    return true
end

-- set current level based on the index in the levels table
function set_level(number)
    state_undo = {}

    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
        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

    local playing = false
    -- 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

            playing = true
            crate_sound:play()
        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

    if playing == false then
        move_sound:play()
    end

    -- save the previous move for UNDO
    table.insert(state_undo, 1, {undo, undo_x, undo_y})
end

-- 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

-- keypressed handler during play
function keypressed_play(key)
    if key == "r" then
        -- restart level
        set_level(current_level)
        restart_sound:play()
        return
    elseif key == "u" and #state_undo > 0 then
        -- undo move
        local state = table.remove(state_undo, 1)
        level = state[1]
        player_x = state[2]
        player_y = state[3]
        moves = moves - 1
        undo_sound:play()
    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)

        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

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)

    -- 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 #state_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)
    end

    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

            -- draw ground unless in empty
            if cell ~= 'x' then
                love.graphics.draw(ground, draw_x, draw_y)
            end
            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

    -- if the level is completed, show an verlay
    if state == "win" then
        if win_delay < 64 then
            win_delay = win_delay + 1
            if win_delay == 64 then
                win_sound:play()
            end
        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)
end