-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worm Generator.lua
92 lines (73 loc) · 2.24 KB
/
Worm Generator.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
--not mine, some cool stuff though
math.randomseed(os.time())
Worm = {
stepDistMin = 5, stepDistMax = 15,
stepNumMin = 500, stepNumMax = 2000,
rotSpeed = 0.2,
rotSpeedX = .1,
rotSpeedY = .4,
rotSpeedZ = 0,
startCFrame = CFrame.new(), nodes = {}
}
function Worm:new(startCFrame)
local object = {}
setmetatable(object, self)
self.__index = self
object.startCFrame = startCFrame or CFrame.new()
object:generate()
return object
end
function Worm:setStepNum(stepNumMin, stepNumMax)
self.stepNumMin = stepNumMin
self.stepNumMax = stepNumMax
end
function Worm:setStepDist(stepDistMin, stepDistMax)
self.stepDistMin = stepDistMin
self.stepDistMax = stepDistMax
end
function Worm:setRotSpeed(r, x, y, z)
self.rotSpeed = r
self.rotSpeedX = x or self.rotSpeedX
self.rotSpeedY = y or self.rotSpeedY
self.rotSpeedZ = z or self.rotSpeedZ
end
function Worm:generate()
local seedX, seedY, seedZ = math.random()*10e5, math.random()*10e5, math.random()*10e5
local stepNum = math.random(self.stepNumMin, self.stepNumMax)
self.nodes = {self.startCFrame}
local lastPoint = self.nodes[1]
local rotX, rotY, rotZ = 0, 0, 0
for i = 1, stepNum do
rotX = rotX + math.noise(i * self.rotSpeed, seedX) * self.rotSpeedX
rotY = rotY + math.noise(i * self.rotSpeed, seedY) * self.rotSpeedY
rotZ = rotZ + math.noise(i * self.rotSpeed, seedZ) * self.rotSpeedZ
rotX = math.min(math.max(rotX, -.1), .1)
rotY = math.min(math.max(rotY, -.1), .1)
rotZ = math.min(math.max(rotZ, -.1), .1)
local nextPoint = lastPoint * CFrame.Angles(rotX, rotY, rotZ)
nextPoint = nextPoint * CFrame.new(0, 0, 10)
table.insert(self.nodes, nextPoint)
lastPoint = nextPoint
end
end
function Worm:visualize()
if self.nodes then
for i = 1, #self.nodes - 1 do
local node1 = self.nodes[i]
local node2 = self.nodes[i + 1]
local dist = (node1.p - node2.p).magnitude
local dir = (node1.p - node2.p).Unit
local beam = Instance.new("Part", game.Workspace)
beam.Anchored = true
beam.Material = "Neon"
beam.Size = Vector3.new(1, 1, dist)
beam.CFrame = CFrame.new(node1.p, node2.p) - (dir * dist * 0.5)
end
end
end
function Worm:getPoints()
return self.points
end
local w = Worm:new(CFrame.new())
w:setRotSpeed(0.05, 0.001, 0.1, 0)
w:visualize()