end
end
function move_down()
if player.y >= #level_state then return end
local src = level_state[player.y][player.x]
local dest = level_state[player.y+1][player.x]
if dest == RGB_WALL then return end
if dest == RGB_CRATE or dest == RGB_CRATE_SOLV then
if player.y >= #level_state-1 then return end
local dest2 = level_state[player.y+2][player.x]
if dest2 ~= RGB_VACANT and dest2 ~= RGB_TARGET then return end
move_crate_to(player.y+2, player.x)
remove_crate_from(player.y+1, player.x)
end
move_player_to(player.y+1, player.x)
remove_player_from(player.y, player.x)
end
function move_up()
if player.y <= 1 then return end
local src = level_state[player.y][player.x]
local dest = level_state[player.y-1][player.x]
if dest == RGB_WALL then return end
if dest == RGB_CRATE or dest == RGB_CRATE_SOLV then
if player.y <= 2 then return end
local dest2 = level_state[player.y-2][player.x]
if dest2 ~= RGB_VACANT and dest2 ~= RGB_TARGET then return end
move_crate_to(player.y-2, player.x)
remove_crate_from(player.y-1, player.x)
end
move_player_to(player.y-1, player.x)
remove_player_from(player.y, player.x)
end
function move_left()
if player.x <= 1 then return end
local src = level_state[player.y][player.x]
local dest = level_state[player.y][player.x-1]
if dest == RGB_WALL then return end
if dest == RGB_CRATE or dest == RGB_CRATE_SOLV then
if player.x <= 2 then return end
local dest2 = level_state[player.y][player.x-2]
if dest2 ~= RGB_VACANT and dest2 ~= RGB_TARGET then return end
move_crate_to(player.y, player.x-2)
remove_crate_from(player.y, player.x-1)
end
move_player_to(player.y, player.x-1)
remove_player_from(player.y, player.x)
end
function move_right()
if player.x >= #level_state[1] then return end
local src = level_state[player.y][player.x]
local dest = level_state[player.y][player.x+1]
if dest == RGB_WALL then return end
if dest == RGB_CRATE or dest == RGB_CRATE_SOLV then
if player.x >= #level_state[1]-1 then return end
local dest2 = level_state[player.y][player.x+2]
if dest2 ~= RGB_VACANT and dest2 ~= RGB_TARGET then return end
move_crate_to(player.y, player.x+2)
remove_crate_from(player.y, player.x+1)
end
move_player_to(player.y, player.x+1)
remove_player_from(player.y, player.x)
end
function move_player_to(y, x)
local old = level_state[y][x]
if old == RGB_VACANT then
level_state[y][x] = RGB_PLAYER
elseif old == RGB_TARGET then
level_state[y][x] = RGB_PLAYER_TGT
else
error('cannot move player to '..old)