-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.lua
73 lines (56 loc) · 1.76 KB
/
camera.lua
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
Camera = Core.class(Sprite)
local width = application:getContentWidth()
local height = application:getContentHeight()
function Camera:init(scene)
self.scene = scene
-- Get screen dimensions
--self.screenWidth = application:getContentWidth()
--self.screenHeight = application:getContentHeight()
end
function Camera:update()
local scene = self.scene
local map = scene.map
local player = scene.player
local offsetX = 0
local offsetY = 0
if((scene.worldWidth - player:getX()) < width * 0.5) then
offsetX = -scene.worldWidth + width
elseif(player:getX() >= width * 0.5) then
offsetX = -(player:getX() - width * 0.5)
end
map:setX(offsetX)
if((scene.worldHeight - player:getY()) < height * 0.5) then
offsetY = -scene.worldHeight + height
elseif(player:getY()>= height * 0.5) then
offsetY = -(player:getY() - height * 0.5)
end
map:setY(offsetY)
--print(map:getNumChildren())
--[[
for i = 1, map:getNumChildren() do
--get specific sprite
local sprite = map:getChildAt(i)
-- check if sprite HAS a body (ie, physical object reference we added)
if sprite.body then
--update position to match box2d world object's position
--get physical body reference
local body = sprite.body
--get body coordinates
local bodyX, bodyY = body:getPosition()
--print(bodyX, bodyY)
--apply coordinates to sprite
sprite:setPosition(bodyX, bodyY)
--apply rotation to sprite
sprite:setRotation(body:getAngle() * 180 / math.pi)
end
end
]]--
-- Update all box2d static objects
--[[
local objects = scene.objects
for a =1, #objects do
local object = objects[a]
object.body:setPosition(object:getX() + map:getX(), object:getY() + map:getY())
end
]]--
end