summaryrefslogtreecommitdiffhomepage
path: root/camera.lua
blob: e2b2362889eef15a2167eb2e6f0caa0254607456 (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
-- `Camera`
-- Used in drawing.

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

-- Constructor of `Camera`
function Camera:new (world)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.follow = {}
	o.world = world
	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

-- Move follow
function Camera:moveFollow ()
	local x,y,i = 105, 120, 1
	for k,point in pairs(self.world.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