Repository navigation
Expand file tree
/
Copy pathpipePair.lua
More file actions
84 lines (69 loc) · 2.04 KB
/
Copy pathpipePair.lua
File metadata and controls
84 lines (69 loc) · 2.04 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
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
74
75
76
77
78
79
80
81
82
83
84
local base = require "base"
local bird = require "bird"
PipePair = {
img = nil,
width = 0,
gap = 0,
top = {
x = 0,
y = 0,
height = 0,
width = 0
},
bottom = {
x = 0,
y = 0,
height = 0,
width = 0
},
debug = false
}
function PipePair:new(index, pipeImage)
local o = {}
setmetatable(o, { __index = self })
o.img = pipeImage
o.width = o.img:getWidth() * Scale
o.gap = bird.height * 4
o.gapBetweenPairs = o.width * 3.2
o.top = {
x = love.graphics.getWidth() + o.gapBetweenPairs * index,
y = 0,
height = math.random(110, 390),
width = o.width
}
o.bottom = {
x = o.top.x,
y = o.top.height + o.gap,
height = base.y - o.top.height - o.gap,
width = o.width
}
o.debug = false
return o
end
function PipePair:update(dt)
if self.top.x <= -self.width then
self.top.x = self.top.x + self.gapBetweenPairs * 5
self.top.height = math.random(110, 390)
self.bottom.x = self.top.x
self.bottom.y = self.top.height + self.gap
self.bottom.height = base.y - self.top.height - self.gap
end
self.top.x = self.top.x - base.speed * dt
self.bottom.x = self.bottom.x - base.speed * dt
end
function PipePair:draw()
love.graphics.draw(self.img, self.top.x, self.top.height, 0, Scale, -Scale)
love.graphics.draw(self.img, self.bottom.x, self.bottom.y, 0, Scale, Scale)
if self.debug then
love.graphics.rectangle('line', self.top.x, self.top.y, self.width, self.top.height)
love.graphics.rectangle('line', self.bottom.x, self.bottom.y, self.width, self.bottom.height)
end
end
function PipePair:topContainsPoint(pointX, pointY)
return pointX > self.top.x and pointX < self.top.x + self.top.width and
pointY < self.top.height
end
function PipePair:bottomContainsPoint(pointX, pointY)
return pointX > self.bottom.x and
pointX < self.bottom.x + self.bottom.width and pointY > self.bottom.y
end