diff options
| author | Eltrac <hi@guhub.cn> | 2025-02-22 11:00:30 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-02-22 11:00:30 +0800 |
| commit | 6b8a96f358de168aeb107c6cb42e595ba19cd01f (patch) | |
| tree | e818d7a3d784e2b7911f27cfb25ca5086b1f4f89 /utils/utils.lua | |
| parent | 3604290b057ddaf3ea0a63ea934b58c548dad3c9 (diff) | |
refactor: reuse isBlocked() function
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 |
