Implement enemy AI and handler; add placeable radar object; enhance maze functionality
This commit is contained in:
@@ -9,4 +9,34 @@ def intersects?(rect1, rect2)
|
||||
return false if y1 >= y2 + h2 # rect1 is below rect2
|
||||
|
||||
true # overlap exists
|
||||
end
|
||||
|
||||
def line_of_sight?(x1, y1, x2, y2, rect)
|
||||
rx, ry, rw, rh = rect
|
||||
rx, ry, rw, rh = rx - 2, ry - 2, rw + 4, rh + 4
|
||||
|
||||
# The 4 edges of the rect as line segments
|
||||
edges = [
|
||||
[rx, ry, rx + rw, ry ], # top
|
||||
[rx, ry + rh, rx + rw, ry + rh], # bottom
|
||||
[rx, ry, rx, ry + rh], # left
|
||||
[rx + rw, ry, rx + rw, ry + rh], # right
|
||||
]
|
||||
|
||||
!(edges.any? { |ex1, ey1, ex2, ey2| segments_intersect?(x1, y1, x2, y2, ex1, ey1, ex2, ey2) })
|
||||
end
|
||||
|
||||
def segments_intersect?(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)
|
||||
dx1 = ax2 - ax1
|
||||
dy1 = ay2 - ay1
|
||||
dx2 = bx2 - bx1
|
||||
dy2 = by2 - by1
|
||||
|
||||
denom = dx1 * dy2 - dy1 * dx2
|
||||
return false if denom == 0 # parallel
|
||||
|
||||
t = ((bx1 - ax1) * dy2 - (by1 - ay1) * dx2).to_f / denom
|
||||
u = ((bx1 - ax1) * dy1 - (by1 - ay1) * dx1).to_f / denom
|
||||
|
||||
t.between?(0, 1) && u.between?(0, 1)
|
||||
end
|
||||
Reference in New Issue
Block a user