-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpfuldog.lua
199 lines (175 loc) · 5.09 KB
/
helpfuldog.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require("table")
---
-- Clamps a value to a certain range.
-- @param min - The minimum value.
-- @param val - The value to clamp.
-- @param max - The maximum value.
function clamp(min, val, max)
return math.max(min, math.min(val, max));
end
function randomRange(minNum, maxNum)
return (math.floor(math.random() * (maxNum - minNum + 1)) + minNum);
end
--Find vector from arg1 to arg2. if revrse is true, it finds the vector pointing away from arg1.
function findVector(from, to, power, reverse)
v = {x=to.x - from.x, y=to.y - from.y}
v = normalize(v, power);
if (reverse) then
v.x = v.x * -1
v.y = v.y * -1
end
return v;
end
function vectorToAngle(x, y)
radians = math.atan2(y, x)
return radians
end
function toRadians(degrees)
r = degrees * (math.pi / 180);
return r;
end
function toDegrees(radians)
d = radians * (180/math.pi);
return d;
end
function snapToGrid(num, gridsize, useFloor)
if not useFloor then
return math.floor(num / gridsize) * gridsize;
else return math.floor(num / gridsize) * gridsize
end
end
function angleToPoint(radians)
return {x=math.cos(radians), y=math.sin(radians)};
end
function angleXY(object, angle, length, x, y)
length = length or 1
x = x or 0
y = y or 0
RAD = math.pi / -180;
angle = angle * RAD;
object.x = math.cos(angle) * length + x;
object.y = math.sin(angle) * length + y;
end
function angleXYToPoint(object, angle, length, x, y)
local length = length or 1
local x = x or 0
local y = y or 0
RAD = math.pi / -180;
angle = angle * RAD;
p = {x=math.sin(angle) * length + x, y=math.cos(angle) * length + y}
return p;
end
--returns true if val1 is close to val2, within the margin of error.
function isCloseTo(val1, val2, marginOfError)
if (val1 < val2 + marginOfError and val1 > val2 - marginOfError) then return true end
return false;
end
function isBetween(pointA, pointB, betweenPoint, error)
crossproduct = (betweenPoint.y - pointA.y) * (pointB.x - pointA.x) - (betweenPoint.x - pointA.x) * (pointB.y - pointA.y);
if (math.abs(crossproduct) > error) then return false end;
dotproduct = (betweenPoint.x - pointA.x) * (pointB.x - pointA.x) + (betweenPoint.y - pointA.y) * (pointB.y - pointA.y);
if (dotproduct < 0) then return false end
squaredlengthba = (pointB.x - pointA.x) * (pointB.x - pointA.x) + (pointB.y - pointA.y) * (pointB.y - pointA.y);
if (dotproduct > squaredlengthba) then return false end;
return true;
end
function map(x, fromMin, fromMax, toMin, toMax)
return toMin + ((x - fromMin) / (fromMax - fromMin)) * (toMax - toMin);
end
function normalize(vector, length)
local length = length or 1
local magnitude = math.sqrt((vector.x * vector.x) + (vector.y * vector.y))
local normal = {x=vector.x / magnitude, y=vector.y / magnitude}
normal.x = normal.x * length
normal.y = normal.y * length
return normal
end
function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
end
function lineStipple( x1, y1, x2, y2, dash, gap )
local dash = dash or 10
local gap = dash + (gap or 10)
local steep = math.abs(y2-y1) > math.abs(x2-x1)
if steep then
x1, y1 = y1, x1
x2, y2 = y2, x2
end
if x1 > x2 then
x1, x2 = x2, x1
y1, y2 = y2, y1
end
local dx = x2 - x1
local dy = math.abs( y2 - y1 )
local err = dx / 2
local ystep = (y1 < y2) and 1 or -1
local y = y1
local maxX = x2
local pixelCount = 0
local isDash = true
local lastA, lastB, a, b
for x = x1, maxX do
pixelCount = pixelCount + 1
if (isDash and pixelCount == dash) or (not isDash and pixelCount == gap) then
pixelCount = 0
isDash = not isDash
a = steep and y or x
b = steep and x or y
if lastA then
love.graphics.line( lastA, lastB, a, b )
lastA = nil
lastB = nil
else
lastA = a
lastB = b
end
end
err = err - dy
if err < 0 then
y = y + ystep
err = err + dx
end
end
end
function sign(num)
if (num > 0) then return 1
elseif num < 0 then return -1
else return 0
end
end
function pixelLine(x1, y1, x2, y2)
local dir= findVector({x=x1, y=y1}, {x=x2, y=y2}, 1)
local xPos = x1
local yPos = y1
while (distance(xPos, yPos, x2, y2) > 1) do
xPos = xPos + dir.x
yPos = yPos + dir.y
love.graphics.rectangle("fill", snapToGrid(xPos, 1), snapToGrid(yPos, 1), 2, 2);
end
end
function dashedPixelLine(x1, y1, x2, y2, dash, gap)
local dir= findVector({x=x1, y=y1}, {x=x2, y=y2}, 1)
local xPos = x1
local yPos = y1
local count = 0
while (distance(xPos, yPos, x2, y2) > 1) do
count = count + 2
xPos = xPos + dir.x
yPos = yPos + dir.y
if (count > gap) then
love.graphics.rectangle("fill", snapToGrid(xPos, 1), snapToGrid(yPos, 1), 2, 2);
end
if (count > dash + gap) then count = 0 end
end
end
function lerp(a, b, t)
return a + (b - a) * t;
end