summaryrefslogtreecommitdiffhomepage
path: root/camera.lua
blob: adc1041e782c09d05973323156d83b9a36d36013 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- `Camera`
-- Used in drawing.

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

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

-- Destination
function Camera:setDestination (x, y)
	local x = x or 0
	local y = y or 0
	self.dest_x, self.dest_y = x, y
end

function Camera:getDestination ()
	return self.dest_x, self.dest_y
end
	
-- Move follow
function Camera:follow ()
	local x,y,i = 145, 90, 1
	for k,point in pairs(self.world.Nauts) do
		if point.body:getX() > -20 and point.body:getX() < 310 and
		   point.body:getY() > -70 and point.body:getY() < 200 then
			i = i + 1
			x = point.body:getX() + x
			y = point.body:getY() + y
		end
	end
	x = x / i - 145
	y = y / i - 90
	self:setDestination(x,y)
end

-- Update
function Camera:update (dt)
	self:follow()
	local dx, dy = self:getDestination()
	dx = (dx - self.x) * 6 * dt
	dy = (dy - self.y) * 6 * dt
	self:setPosition(self.x + dx, self.y + dy)
end