summaryrefslogtreecommitdiff
path: root/utils/utils.lua
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-02-18 20:24:41 +0800
committerEltrac <hi@guhub.cn>2025-02-18 20:24:41 +0800
commit423cfe133ae8a8f9d7a87b2c4d151af3fd7aa7fc (patch)
tree10c4d8eedcc65155b0f234d6d74be6663bcf5bb0 /utils/utils.lua
parent875cc3e70c11e589c548775c3208659f50daf7df (diff)
feat: entering new rooms
- door connects two rooms - detects if player enters a door, if so, then switch room and update player location
Diffstat (limited to 'utils/utils.lua')
-rw-r--r--utils/utils.lua24
1 files changed, 22 insertions, 2 deletions
diff --git a/utils/utils.lua b/utils/utils.lua
index 828d3d7..8425b1d 100644
--- a/utils/utils.lua
+++ b/utils/utils.lua
@@ -14,8 +14,22 @@ function utils.playerCollideWith(x, y, r)
return utils.circlesCollide(G.player.x, G.player.y, G.player.r, x, y, r)
end
-function utils.randomPosition()
- return math.random(love.graphics.getWidth()), math.random(love.graphics.getHeight())
+function utils.circleRectCollide(cx, cy, r, rx, ry, rw, rh)
+ -- 计算圆心到矩形的水平和垂直距离
+ local nearestX = math.max(rx, math.min(cx, rx + rw))
+ local nearestY = math.max(ry, math.min(cy, ry + rh))
+
+ -- 计算圆心到矩形最近边的距离
+ local dx = cx - nearestX
+ local dy = cy - nearestY
+ local distanceSquared = dx * dx + dy * dy -- 避免开方,提高性能
+
+ -- 如果圆的半径大于或等于这个距离,表示有碰撞
+ return distanceSquared <= r * r
+end
+
+function utils.playerCollideWithRect(x, y, w, h)
+ return utils.circleRectCollide(G.player.x, G.player.y, G.player.r, x, y, w, h)
end
-- deal with obstacles
@@ -58,4 +72,10 @@ function utils.hitObstacle(x, y, r)
return false
end
+-- other utilities
+
+function utils.randomPosition()
+ return math.random(love.graphics.getWidth()), math.random(love.graphics.getHeight())
+end
+
return utils