From f72520689a85b3d1367fb15f60fed94b66dcd4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 29 Jun 2019 14:10:30 +0300 Subject: [PATCH] Treat orb as passable --- bundle/main.lua | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/bundle/main.lua b/bundle/main.lua index 210df5f..2b882a6 100644 --- a/bundle/main.lua +++ b/bundle/main.lua @@ -123,16 +123,16 @@ function spawnPlayer() player_y = spawn_y -- Check what direction we can spawn the player - if cavern[player_x][player_y + 1] == tiletypes.empty then + if passable(player_x, player_y + 1) then -- There's space under, spawn facing up player_direction = directions.up - elseif cavern[player_x + 1][player_y] == tiletypes.empty then + elseif passable(player_x + 1, player_y) then -- There's space to the right, spawn facing left player_direction = directions.left - elseif cavern[player_x - 1][player_y] == tiletypes.empty then + elseif passable(player_x - 1, player_y) then -- There's space to the left, spawn facing right player_direction = directions.right - elseif cavern[player_x][player_y - 1] == tiletypes.empty then + elseif passable(player_x, player_y - 1) then -- There's space above, spawn facing down player_direction = directions.down else @@ -147,7 +147,7 @@ function spawnOrb() repeat orb_x = math.random(2, cavern.width - 1) orb_y = math.random(2, cavern.height - 1) - until cavern[orb_x][orb_y] == tiletypes.empty + until passable(orb_x, orb_y) cavern[orb_x][orb_y] = tiletypes.orb end @@ -270,22 +270,22 @@ function stepVisibilityMapGeneration(queue) for i, item in ipairs(queue) do if item.direction == directions.up then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then table.insert(new_queue, {x = item.x, y = item.y - 1, direction = directions.up}) end elseif item.direction == directions.left then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then table.insert(new_queue, {x = item.x - 1, y = item.y, direction = directions.left}) end elseif item.direction == directions.down then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then table.insert(new_queue, {x = item.x, y = item.y + 1, direction = directions.down}) end elseif item.direction == directions.right then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then table.insert(new_queue, {x = item.x + 1, y = item.y, direction = directions.right}) end elseif item.direction == directions.upleft then @@ -301,9 +301,9 @@ function stepVisibilityMapGeneration(queue) -- it is not. We can therefore check whether the movement is valid -- by testing if either the cell to our right or below us (the -- directions opposite to the ones in an up.left movement) is empty - if cavern[item.x + 1][item.y] == tiletypes.empty or cavern[item.x][item.y + 1] == tiletypes.empty then + if passable(item.x + 1, item.y) or passable(item.x, item.y + 1) then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then -- ⌜^ -- ⌜ -- v⌟ table.insert(new_queue, {x = item.x + 1, y = item.y, direction = directions.right}) @@ -337,9 +337,9 @@ function stepVisibilityMapGeneration(queue) end elseif item.direction == directions.upright then -- See under item.direction == directions.upleft - if cavern[item.x - 1][item.y] == tiletypes.empty or cavern[item.x][item.y + 1] == tiletypes.empty then + if passable(item.x - 1, item.y) or passable(item.x, item.y + 1) then visibility_map[item.x][item.y] = true - if cavern[item.x][item.y] == tiletypes.empty then + if passable(item.x, item.y) then -- ^⌝ -- ⌝ x> table.insert(new_queue, {x = item.x, y = item.y - 1, direction = directions.up}) @@ -376,6 +376,13 @@ function rememberVisible() end end +-- ------------------------------------------------------------------ +-- Collision +-- ------------------------------------------------------------------ +function passable(x, y) + return cavern[x][y] == tiletypes.empty or cavern[x][y] == tiletypes.orb +end + -- ------------------------------------------------------------------ -- Player helper functions -- ------------------------------------------------------------------ @@ -517,8 +524,7 @@ function movePlayer(direction) local body_x, body_y = getBodyLocation(player_x + dx, player_y + dy, new_direction) - if cavern[player_x + dx][player_y + dy] == tiletypes.empty and - cavern[body_x][body_y] == tiletypes.empty then + if passable(player_x + dx, player_y + dy) and passable(body_x, body_y) then player_x = player_x + dx player_y = player_y + dy player_direction = new_direction