-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.lua
More file actions
48 lines (36 loc) · 1.49 KB
/
Copy pathcamera.lua
File metadata and controls
48 lines (36 loc) · 1.49 KB
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
function makeCamera(world, xPixel, yPixel)
-- Idea: Camera is just an invisible body, who's
-- position in the world determines the transformation matrix
-- and who's position is constrained by the map
return love.physics.newBody(world, xPixel, yPixel, "dynamic")
end
function moveCamera(camera, dt)
if player then
local uiSize = screen.height/5
local halfWidth = screen.width/2
local halfHeight = (screen.height + uiSize)/2
local halfTile = screen.tileSize/2
local tx = math.max(player.body:getX(), halfWidth - halfTile)
tx = math.min(tx, level.pixelWidth - halfWidth + screen.tileSize)
local dx = tx - camera:getX() - halfWidth
--local ty = math.max(player.body:getY(), halfHeight - halfTile)
local ty = math.max(player.body:getY() + uiSize, halfHeight - halfTile) -- hard to say if 2*half is better, or half
ty = math.min(ty, level.pixelHeight + screen.tileSize - uiSize)
local dy = ty - camera:getY() - halfHeight
local dist2 = dx*dx + dy*dy
if player.invulnTime and player.invulnTime > 0 then
local ddx, ddy = getRandomVector(100)
dx = dx + ddx
dy = dy + ddy
end
if dist2 > 0.001 then
camera:setLinearVelocity(dx * dist2 * dt / 16, dy * dist2 * dt / 16)
else
camera:setLinearVelocity(0, 0)
end
end
end
function getRandomVector(magnitude)
local angle = 2 * math.pi * math.random()
return magnitude * math.cos(angle), magnitude * math.sin(angle)
end