-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvine.lua
More file actions
66 lines (52 loc) · 1.46 KB
/
Copy pathvine.lua
File metadata and controls
66 lines (52 loc) · 1.46 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
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "constants"
local gfx = playdate.graphics
class("Vine").extends(gfx.sprite)
-- Constructor
function Vine:init(x, y, height, radius)
self.height = height or 100
self.radius = radius or 5
self.segmentHeight = 16
self:refresh()
self:moveTo(x, y)
self:setUpdatesEnabled(true)
self.collisionType = kCollisionGroupVine
self:add()
end
-- Refresh
function Vine:refresh()
-- Draw the line
local vineWidth = self.radius * 2 + 2
local vineImage = gfx.image.new(vineWidth, self.height)
gfx.pushContext(vineImage)
-- Draw the waves
local centerX = vineWidth / 2
gfx.setColor(gfx.kColorBlack)
gfx.drawLine(centerX, 0, centerX, self.height)
for i = 0, self.height, 2 do
local angle = i * 0.2
local offset = math.sin(angle) * self.radius
gfx.drawPixel(centerX + offset, i)
gfx.drawPixel(centerX - offset, i)
end
gfx.popContext()
self:setImage(vineImage)
end
-- Get the top
function Vine:getTopY()
return self.y - self.height / 2
end
-- Get the bottom
function Vine:getBottomY()
return self.y + self.height / 2
end
-- Figure how many 16px segments there are
function Vine:getNumSegments()
return math.floor(self.height / self.segmentHeight)
end
-- Centerpoint
function Vine:getSegmentCenterY(index)
return self:getTopY() + (index * self.segmentHeight) + self.segmentHeight / 2
end