summaryrefslogtreecommitdiffhomepage
path: root/not/Camera.lua
blob: 79ebd09f0a2356be2583fd4e46174e5a9efc4a64 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
--- Used in drawing other stuff in places.
Camera = require "not.Object":extends()

function Camera:new (world)
	self.world = world
	self.x = 0
	self.y = 0
	self.dest_y = 0
	self.dest_x = 0
	self.timer = 0
	self.delay = 0
	self.origin_x = 0
	self.origin_y = 0
	self.shake_x = 0
	self.shake_y = 0
	self:setPosition(self:follow())
	self:setDestination(self:follow())
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

function Camera:getPosition ()
	return self.x, self.y
end

function Camera:getPositionScaled ()
	return self.x*getScale(), self.y*getScale()
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

-- Translate points
function Camera:translatePosition (x, y)
	local x = x or 0
	local y = y or 0
	return (x-self.x)*getScale(), (y-self.y)*getScale()
end

function Camera:translatePoints(...)
	local a = {...}
	local r = {}
	local x,y = self:getOffsets()
	for k,v in pairs(a) do
		if k%2 == 1 then
			table.insert(r, (v + x) * getScale())
		else
			table.insert(r, (v + y) * getScale())
		end
	end
	return r
end

-- Shake it
-- Really bad script, but for now it works
function Camera:shake ()
	if self.shake_x == 0 then
		self.shake_x = math.random(-10, 10) * 2
	elseif self.shake_x > 0 then
		self.shake_x = math.random(-10, -1) * 2
	elseif self.shake_x < 0 then
		self.shake_x = math.random(10, 1) * 2
	end
	if self.shake_y == 0 then
		self.shake_y = math.random(-10, 10) * 2
	elseif self.shake_y > 0 then
		self.shake_y = math.random(-10, -1) * 2
	elseif self.shake_y < 0 then
		self.shake_y = math.random(10, 1) * 2
	end
	local x = self.origin_x + self.shake_x
	local y = self.origin_y + self.shake_y
	self:setDestination(x, y)
end

function Camera:startShake ()
	self.timer = 0.3
	self.origin_x, self.origin_y = self:getPosition()
end

-- Move follow
function Camera:follow ()
	local map = self.world.map
	local sum_x,sum_y,i = map.center.x, map.center.y, 1
	for k,naut in pairs(self.world:getNautsAll()) do
		local naut_x,naut_y = naut:getPosition()
		if math.abs(naut_x - map.center.x) < map.width/2 and
		   math.abs(naut_y - map.center.y) < map.height/2 then
			i = i + 1
			sum_x = naut_x + sum_x
			sum_y = naut_y + sum_y
		end
	end
	local x = sum_x / i - love.graphics.getWidth()/getScale()/2
	local y = sum_y / i - love.graphics.getHeight()/getScale()/2 + 4*getScale() -- hotfix
	return x,y
end

-- Update
function Camera:update (dt)
	if self.timer > 0 then
		self.timer = self.timer - dt
		if self.delay <= 0 then
			self:shake()
			self.delay = 0.02
		else
			self.delay = self.delay - dt
		end
	else
		self:setDestination(self:follow())
	end
	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