51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| Camera = {
 | |
| 	pos = {x = 0, y = 0},
 | |
| 	width = 0,
 | |
| 	height = 0
 | |
| }
 | |
| 
 | |
| function Camera:followPlayer(player)
 | |
| 	local pos = player.pos
 | |
| 	local room = player:getCollidingAt(pos.x,pos.y,LoadedObjects.Rooms)
 | |
| 
 | |
| 	self:positionCenterAt(pos.x, pos.y)
 | |
| 	
 | |
| 	self:confineTo(room)
 | |
| end
 | |
| 
 | |
| function Camera:confineTo(box)
 | |
| 	if box == nil then
 | |
| 		--frameDebug("not in a room")
 | |
| 		return
 | |
| 	end
 | |
| 	--frameDebug("in a room")
 | |
| 	
 | |
| 	local w = self.width/game.scale
 | |
| 	local h = self.height/game.scale
 | |
| 	
 | |
| 	-- bottom edge
 | |
| 	self.pos.y = math.min(self.pos.y+h, box.to.y)-h
 | |
| 	-- right edge
 | |
| 	self.pos.x = math.min(self.pos.x+w, box.to.x)-w
 | |
| 	-- top edge
 | |
| 	self.pos.y = math.max(self.pos.y, box.from.y)
 | |
| 	-- left edge
 | |
| 	self.pos.x = math.max(self.pos.x, box.from.x)
 | |
| end
 | |
| 
 | |
| function Camera:ConfineToLevel()
 | |
| 	self.pos.x = math.max(0,math.min(self.pos.x,LevelData.Width-self.width/game.scale))
 | |
| 	self.pos.y = math.max(0,math.min(self.pos.y,LevelData.Height-self.height/game.scale))
 | |
| end
 | |
| 
 | |
| function Camera:positionCenterAt(x,y)
 | |
| 	self.pos.x = x-self.width/game.scale/2
 | |
| 	self.pos.y = y-self.height/game.scale/2
 | |
| 	--self:ConfineToLevel()
 | |
| end
 | |
| 
 | |
| function Camera:positionAt(x,y)
 | |
| 	self.pos.x = math.floor((x/self.width)*self.width)
 | |
| 	self.pos.y = math.floor((y/self.height)*self.height)
 | |
| end
 |