-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.lua
91 lines (86 loc) · 3.26 KB
/
wire.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Wire = {id, type, sourceNode, destNode, active, color}
function Wire:new(o, sourceNode, destNode, type, active)
o = o or {}
setmetatable(o, self)
self.__index = self
o.type = type or 1
o.sourceNode = sourceNode
o.destNode = destNode
o.active = active or true
o.color = {
r = 0,
g = 0,
b = 0,
a = 1
}
return o
end
--- draws a tangent line between sourceNode and destNode with radius `nailRadius`.
--- the specific line is based on `self.type`
---@param nailRadius integer "the radius of the nail (in pixels)"
---@return boolean "`true` if the string was actually drawn, `false` otherwise"
function Wire:draw(x, y, nailRadius, ppu, canvas)
if (nailRadius <= 0 and self.type ~= 1) or not self.active then
return false
end
local source = self.sourceNode
local dest = self.destNode
local x1, x2, y1, y2
if self.type == 1 or self.type == 2 then
local phi = math.atan2(source.y - dest.y, source.x - dest.x)
local theta = phi + (math.pi / 2)
if self.type == 1 then
-- "right" outer tangent
x1 = source.x + math.cos(theta) * nailRadius
x2 = dest.x + math.cos(theta) * nailRadius
y1 = source.y + math.sin(theta) * nailRadius
y2 = dest.y + math.sin(theta) * nailRadius
elseif self.type == 2 then
-- "left" outer tangent
x1 = source.x - math.cos(theta) * nailRadius
x2 = dest.x - math.cos(theta) * nailRadius
y1 = source.y - math.sin(theta) * nailRadius
y2 = dest.y - math.sin(theta) * nailRadius
end
elseif self.type == 3 or self.type == 4 then
local xDelta = dest.x - source.x
local yDelta = dest.y - source.y
local distanceSquared = xDelta * xDelta + yDelta * yDelta
local meh = math.sqrt(distanceSquared - (nailRadius * nailRadius)) * (nailRadius / distanceSquared) -- it's called meh because i don't know what it does.
local xOffset
local yOffset
-- "right" inner tangent
xOffset = (xDelta * (nailRadius * nailRadius) / distanceSquared) - (meh * -yDelta)
yOffset = (yDelta * (nailRadius * nailRadius) / distanceSquared) - (meh * xDelta)
if self.type == 4 then
-- "left" inner tangent.
xOffset = xOffset + (meh * -yDelta * 2)
yOffset = yOffset + (meh * xDelta * 2)
end
x1 = xOffset + source.x
x2 = xDelta - xOffset + source.x
y1 = yOffset + source.y
y2 = yDelta - yOffset + source.y
end
x1, y1 = worldToScreenSpace(x1, y1, ppu, canvas)
x2, y2 = worldToScreenSpace(x2, y2, ppu, canvas)
x1 = x1 + x
y1 = y1 + y
x2 = x2 + x
y2 = y2 + y
-- unnecessary fancy color lerping
-- if canvas == nil then
-- local oldColor = {love.graphics.getColor()}
--
-- print(self.color.r, self.color.g, self.color.b, self.color.a)
-- love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.color.a)
-- love.graphics.line(x1, y1, x2, y2)
-- love.graphics.setColor(oldColor)
-- else
-- if canvas == nil then
-- local oldColor = {love.graphics.getColor()}
-- else
love.graphics.line(x1, y1, x2, y2)
-- end
return true
end