summaryrefslogtreecommitdiffhomepage
path: root/camera.lua
blob: 2893409d9f09fe194d8eac4d888d27e5e555e7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- `Camera`
-- Used in drawing.

-- Metatable of `Camera`
Camera = {
	x = 0,
	y = 0,
	scale = 4,
	follow = nil
}

-- Constructor of `Camera`
function Camera:new ()
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.follow = {}
	return o
end

-- Drawing offsets
function Camera:getOffsets ()
	return -self.x,-self.y
end

-- Position
function Camera:setPosition (x, y)
	local x = x or 0
	local y = y or 0
	self.x, self.y = x, y
end

-- Add follow position
function Camera:addFollow (x, y, w)
	local w = 1
	if x ~= nil and y ~= nil then
		table.insert(self.follow, {["x"] = x, ["y"] = y, ["w"] = w})
	end
end

-- Move follow
function Camera:moveFollow ()
	local x,y,i = 145, 120, 1
	for k,point in pairs(Nauts) do
		i = i + 1
		x = math.max(math.min(point.body:getX(),290),0) + x
		y = math.max(math.min(point.body:getY(),180),20) + y
	end
	x = x / i - 145
	y = y / i - 90
	self:setPosition(x,y)
end