added Rect class
This commit is contained in:
		
							parent
							
								
									97de68e34b
								
							
						
					
					
						commit
						27f1dc71c0
					
				@ -88,3 +88,7 @@ function Collision:draw(color)
 | 
			
		||||
	love.graphics.setColor(0,1,90,0.5)
 | 
			
		||||
	love.graphics.rectangle("line",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Collision:asRect()
 | 
			
		||||
	return Rect:fromCoords(self.from.x, self.from.y, self.to.x, self.to.y)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										66
									
								
								code/rect.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								code/rect.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,66 @@
 | 
			
		||||
-- based of of plan9's Rectangle struct
 | 
			
		||||
-- rect.max is not counted as "in" the rectangle
 | 
			
		||||
Rect = {}
 | 
			
		||||
Rect.__index = Rect
 | 
			
		||||
 | 
			
		||||
function Rect:fromPoints(pt1, pt2)
 | 
			
		||||
	local o = { min = pt1, max = pt2 }
 | 
			
		||||
	setmetatable(o, self)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:fromCoords(x1, y1, x2, y2)
 | 
			
		||||
	return Rect:fromPoints(Point:new(x1, y1), Point:new(x2, y2))
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- clone refers to a deep copy
 | 
			
		||||
function Rect:clone()
 | 
			
		||||
	return Rect:fromCoods(self.min.x, self.min.y, self.max.x, self.max.y)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- make sure min and max refer to the correct corners
 | 
			
		||||
-- acts in place, returns self
 | 
			
		||||
function Rect:fix()
 | 
			
		||||
	if self.min.x > self.max.x then
 | 
			
		||||
		self.min.x, self.max.x = self.max.x, self.min.x
 | 
			
		||||
	end
 | 
			
		||||
	if self.min.y > self.max.y then
 | 
			
		||||
		self.min.y, self.max.y = self.max.y, self.min.y
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:width()
 | 
			
		||||
	return self.max.x - self.min.x
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:height()
 | 
			
		||||
	return self.max.y - self.min.y
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:size()
 | 
			
		||||
	return Point:new(self:width(), self:height())
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:__add(pt)
 | 
			
		||||
	return Rect:fromPoints(self.min + pt, self.max + pt)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:corners()
 | 
			
		||||
	return {
 | 
			
		||||
		self.min:copy(), -- top left
 | 
			
		||||
		Point:new(self.max.x, self.min.y), -- top right
 | 
			
		||||
		Point:new(self.min.x, self.max.y), -- bottom left
 | 
			
		||||
		self.max:copy(), -- bottom right
 | 
			
		||||
	}
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:containsPoint(pt)
 | 
			
		||||
	return self.min.x < pt.x and
 | 
			
		||||
		self.min.y < pt.y and
 | 
			
		||||
		pt.x < self.max.x and
 | 
			
		||||
		pt.y < self.max.y
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Rect:overlapsRect(other)
 | 
			
		||||
		return self.min.x<other.max.x and self.min.x<other.max.x and
 | 
			
		||||
	       self.min.y<other.max.y and self.min.y<other.max.y
 | 
			
		||||
end
 | 
			
		||||
@ -14,6 +14,7 @@ require "code/in_out"
 | 
			
		||||
 | 
			
		||||
-- classes
 | 
			
		||||
require "code/point"
 | 
			
		||||
require "code/rect"
 | 
			
		||||
require "code/objects"
 | 
			
		||||
require "code/level"
 | 
			
		||||
require "code/camera"
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user