diff options
Diffstat (limited to 'utils/utils.lua')
| -rw-r--r-- | utils/utils.lua | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/utils/utils.lua b/utils/utils.lua index d207049..3a2dd05 100644 --- a/utils/utils.lua +++ b/utils/utils.lua @@ -2,6 +2,37 @@ utils = {} -- deal with obstacles +function utils.isBlocked(body, x, y, dir, axis, dv) + local function meetWall() + local borders = G.currentRoom.borders + local r, axisVal = body.r, (axis == "x" and x or y) + dv + + local hitRoomBorder = false + if dir==Direction.RIGHT then + hitRoomBorder = axisVal+r >= borders[Direction.RIGHT] + elseif dir==Direction.LEFT then + hitRoomBorder = axisVal-r <= borders[Direction.LEFT] + elseif dir==Direction.TOP then + hitRoomBorder = axisVal-r <= borders[Direction.TOP] + elseif dir==Direction.BOTTOM then + hitRoomBorder = axisVal+r >= borders[Direction.BOTTOM] + end + + return hitRoomBorder + end + + local function meetObstacle() + for _, obstacle in ipairs(G.currentRoom.obstacles) do + if obstacle:isMet(body, x, y, dv, dir) then + return true + end + end + return false + end + + return meetWall() or meetObstacle() +end + -- detect if object hit obstacles with no specific direction function utils.hitObstacle(x, y, r) local borders = G.currentRoom.borders |
