From 6b8a96f358de168aeb107c6cb42e595ba19cd01f Mon Sep 17 00:00:00 2001 From: Eltrac Date: Sat, 22 Feb 2025 11:00:30 +0800 Subject: refactor: reuse isBlocked() function --- utils/utils.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'utils') 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 -- cgit v1.2.3