-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.lua
51 lines (33 loc) · 1.01 KB
/
background.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
require 'middleclass.lua'
local g = love.graphics
Background = class('Background')
function Background:initialize(player, image, y, color, scrollX, scrollY)
self.player = player
self.image = image
self.y = y
self.color = color
self.scrollX = scrollX
self.scrollY = scrollY
self.bg1X = 0
self.bg2X = g.getWidth()
self.y = y
end
function Background:update(dt)
local delta = dt * self.scrollX * self.player:getSpeed()
self.bg1X = math.floor(self.bg1X - delta)
self.bg2X = math.floor(self.bg2X - delta)
if self.bg1X + g.getWidth() < 0 then
self.bg1X = g.getWidth()
end
if self.bg2X + g.getWidth() < 0 then
self.bg2X = g.getWidth()
end
end
function Background:draw()
g.setColor(255, 255, 255, 255)
local y = self.y - self.image:getHeight()
g.draw(self.image:getImage(), math.floor(self.bg1X), y)
g.draw(self.image:getImage(), math.floor(self.bg2X), y)
g.setColor(unpack(self.color))
g.rectangle("fill", 0, y + self.image:getHeight(), g.getWidth(), g.getHeight() - y + self.image:getHeight())
end