-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax.lua
62 lines (49 loc) · 1.42 KB
/
parallax.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
--[[
Requires vector of maps V with each map having:
- image: The image of the plane
- speed: Speed of the moving map (tiles per second)
The order is important, the deepest maps first
example code:
(GLOBAL)
local parallaxBackground
(LOAD)
local planoDeep = {
love.graphics.newImage("planoDeep.png"),
speed = love.graphics.getWidth()/2
}
local planoBackground = {
love.graphics.newImage("planoBackground.png"),
speed = love.graphics.getWidth()/5
}
parallaxBackground = {planoDeep, planoBackground}
(UPDATE)
-- IF YOU WANT TO MOVE WITH THE DEFAULT SPEED, DOESN NOT NEED THE PARAMETER mov
parallax_update(dt, parallaxBackground)
-- MOV is to change somehow the SPEED, for example, make the parallax move 50% slower -> mov = 0.5
parallax_update(dt, p, 0.5)
(DRAW)
parallax_draw(parallaxBackground)
]]
local updateWithVel
function parallax_new(listOfMaps)
for i,v in ipairs(listOfMaps) do
v.width = v.image:getWidth()
end
return {list = listOfMaps}
end
function parallax_update(dt, p, mov)
if mov == nil then move = 1 end
for i,v in ipairs(p.list) do
v.position = v.position + v.speed*mov*dt
if v.position > v.width then
v.position = v.position%v.width
end
end
end
function parallax_draw(p)
for i,v in ipairs(p.list) do
local pos = v.position
love.graphics.draw(v.image, -pos, 0)
love.graphics.draw(v.image, v.width-pos, 0)
end
end