Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local NodesPerStep = 30
- plr = game:GetService("Players").LocalPlayer
- char = plr.Character
- m = plr:GetMouse()
- human = char.Humanoid
- torso = char.Torso
- root = char.HumanoidRootPart
- lleg = char["Left Leg"]
- rleg = char["Right Leg"]
- larm = char["Left Arm"]
- rarm = char["Right Arm"]
- head = char.Head
- lshold = torso["Left Shoulder"]
- rshold = torso["Right Shoulder"]
- neck = torso.Neck
- lhip = torso["Left Hip"]
- rhip = torso["Right Hip"]
- lscf = lshold.C0
- rscf = rshold.C0
- ncf = neck.C0
- lhcf = lhip.C0
- rhcf = rhip.C0
- rad = math.rad
- random = math.random
- sin = math.sin
- cos = math.cos
- hurt = false
- local Maze = {}
- function Maze:New(xSize, ySize)
- local Cells = {}
- local IDs = {}
- local ID = 1
- for x = 0, xSize+1 do
- Cells[x] = {}
- for y = 0, ySize+1 do
- Cells[x][y] = {
- ID = ID;
- X = x;
- Y = y;
- Edge = (x == 0 or y == 0 or x==xSize+1 or y == ySize+1)
- }
- IDs[ID] = Cells[x][y]
- ID = ID + 1
- end
- end
- local NewMaze = {
- Cells = Cells;
- IDs = IDs;
- }
- setmetatable(NewMaze, {
- __index = Maze;
- })
- return NewMaze
- end
- function Maze:NewWalls(x, y)
- local Walls = {}
- if (x == self.Finish.X and y == self.Finish.Y) or
- (x == self.Start.X and y == self.Start.Y) then
- if x == #self.Cells then
- Walls[1] = {self.Cells[x][y].ID, self.Cells[x-1][y].ID}
- Walls[2] = {self.Cells[x][y].ID, false}
- else
- Walls[1] = {self.Cells[x][y].ID, self.Cells[x+1][y].ID}
- Walls[2] = {self.Cells[x][y].ID, false}
- end
- if y == #self.Cells[x] then
- Walls[3] = {self.Cells[x][y].ID, self.Cells[x][y-1].ID}
- Walls[4] = {self.Cells[x][y].ID, false}
- else
- Walls[3] = {self.Cells[x][y].ID, self.Cells[x][y+1].ID}
- Walls[4] = {self.Cells[x][y].ID, false}
- end
- else
- Walls[1] = {self.Cells[x][y].ID, self.Cells[x-1][y].ID}
- Walls[2] = {self.Cells[x][y].ID, self.Cells[x+1][y].ID}
- Walls[3] = {self.Cells[x][y].ID, self.Cells[x][y-1].ID}
- Walls[4] = {self.Cells[x][y].ID, self.Cells[x][y+1].ID}
- end
- return Walls
- end
- function Maze:Generate(start, finish, scale, height)
- self.Finish = finish
- self.Start = start
- self.Scale = scale
- self.Height = height
- self.Model = Instance.new("Model")
- self.Model.Name = "Maze"
- local Walls = self:NewWalls(start.X, start.Y)
- local PermWalls = {}
- local PermCells = {[self.Cells[start.X][start.Y].ID] = true}
- local Rand = math.random
- local function MainLoop()
- while wait() do
- for i = 1, NodesPerStep do
- if not next(Walls, Key) then
- return nil
- end
- local Key = Rand(1, #Walls)
- local w = Walls[Key]
- table.remove(Walls,Key)
- local Other
- local Self
- if PermCells[w[1]] then
- if PermCells[w[2]] then
- PermWalls[#PermWalls+1] = w
- else
- Other = w[2]
- Self = w[1]
- end
- else
- Other = w[1]
- Self = w[2]
- end
- if Other then
- PermCells[Other] = true
- if self.IDs[Other].Edge then
- PermWalls[#PermWalls+1] = w
- else
- for _, Wall in pairs(self:NewWalls(self.IDs[Other].X, self.IDs[Other].Y)) do
- if Wall[2] ~= Self then
- Walls[#Walls+1] = Wall
- end
- end
- end
- end
- end
- end
- end
- MainLoop()
- for _, Wall in pairs(PermWalls) do
- self:CreateWall(Wall[1], Wall[2])
- end
- local xMax = ((#self.Cells - 1))*scale
- local yMax = ((#self.Cells[1] - 1))*scale
- local Floor = Instance.new("Part")
- Floor.Anchored = true
- Floor.BrickColor = BrickColor.new('Bright green')
- Floor.Material = "Grass"
- Floor.Name = "MainBase"
- Floor.Size = Vector3.new(xMax+2, 1.2, yMax+2)
- Floor.Position = Vector3.new(xMax/2+scale/2,-0.4,yMax/2+scale/2)
- Floor.Parent = self.Model
- end
- function Maze:CreateWall(id1, id2)
- local Wall = Instance.new "Part"
- Wall.Anchored = true
- Wall.Name = "Walls"
- Wall.Size = Vector3.new(self.Scale, self.Height, 2)
- local n1 = self.IDs[id1]
- local n2 = self.IDs[id2]
- Wall.CFrame = CFrame.new(
- Vector3.new(n1.X, self.Height/2, n1.Y),
- Vector3.new(n2.X, self.Height/2, n2.Y)
- )
- Wall.Position = Vector3.new((n1.X + n2.X)*self.Scale/2, self.Height/2, (n1.Y + n2.Y)*self.Scale/2)
- Wall.Parent = self.Model
- end
- local m = Maze:New(25,25)
- m:Generate(Vector2.new(1,25), Vector2.new(50,50), 20, 25)
- m.Model.Parent = workspace
- m.Model:MoveTo(workspace:FindFirstChild('Base').Position + Vector3.new(0,65,0))
- for _,c in pairs(m.Model:GetChildren()) do
- if c:IsA('Part') then
- if c.Name == "Walls" then
- c.Anchored = true
- c.CanCollide = false
- c.Name ="Walls"
- c.Transparency = 1
- c.TopSurface = 0
- c.BottomSurface = 0
- c.BrickColor = BrickColor.new('Daisy orange')
- c.Material = "Cobblestone"
- end
- end
- end
- thing = Instance.new('Part', char.Torso)
- thing.Anchored = false
- thing.CanCollide = false
- thing.Transparency = 1
- thing.Name = "Trigger"
- thing.Size = Vector3.new(225, 0, 225)
- thing.Touched:connect(function(hit)
- if hit.Name == "Walls" then
- hit.CanCollide = true
- hit.Transparency = 0
- local s = Instance.new("SelectionBox", char)
- local lasso = Instance.new("SelectionPartLasso",s)
- lasso.Humanoid = char.Humanoid
- lasso.Part = hit
- end
- end)
- thing.TouchEnded:connect(function(unhit)
- if unhit.Name == "Walls" then
- unhit.CanCollide = false
- unhit.Transparency = 1
- for _,c in pairs(char:GetChildren()) do
- if c:IsA('SelectionBox') then
- c:Destroy()
- end
- end
- end
- end)
- char.Torso.CFrame = CFrame.new(0,125,0)
- char.Archivable = true
- game:GetService("RunService").RenderStepped:connect(function()
- thing.CFrame = char.Torso.CFrame
- end)
Add Comment
Please, Sign In to add comment