-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.moon
More file actions
47 lines (31 loc) · 904 Bytes
/
background.moon
File metadata and controls
47 lines (31 loc) · 904 Bytes
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
{graphics: g} = love
class ParalaxBg
layers: 4
speed: 1
lazy sprite: -> imgfy "images/ground.png"
new: =>
@sprite\set_wrap "repeat", "clamp"
@base_offsets = setmetatable {}, __index: (key) =>
r = rand 0, 100
@[key] = r
r
update: (dt) =>
draw: (viewport, bounds) =>
w,h = @sprite\width!, @sprite\height!
vw = viewport.w
for l=@layers,1,-1
t = (@layers - (l - 1)) / @layers
COLOR\push { t * 255, t * 255, t * 255 }
scale = math.sqrt t
real_w = w * scale
times = math.floor(vw / real_w) + 2
q = g.newQuad 0, 0, w * times, h, w, h
g.push!
g.translate viewport.x, bounds.h
px = -(l - 1) / @layers + 1
g.translate -((@base_offsets[l] + viewport.x) * px % real_w), (l - 1) * -10
g.scale scale, scale
@sprite\draw q, 0, -h
g.pop!
COLOR\pop!
{ :ParalaxBg }