chore: init from rojo-to-rbxlx
This commit is contained in:
140
src/ReplicatedStorage/Shared/PlacementManager.lua
Normal file
140
src/ReplicatedStorage/Shared/PlacementManager.lua
Normal file
@@ -0,0 +1,140 @@
|
||||
local PlacementManager = {}
|
||||
|
||||
local ChunkManager = require("./ChunkManager")
|
||||
local Util = require("./Util")
|
||||
|
||||
PlacementManager.ChunkFolder = ChunkManager.ChunkFolder
|
||||
|
||||
local raycastParams = RaycastParams.new()
|
||||
raycastParams.FilterDescendantsInstances = {PlacementManager.ChunkFolder}
|
||||
raycastParams.FilterType = Enum.RaycastFilterType.Include
|
||||
raycastParams.IgnoreWater = true
|
||||
|
||||
if _G.SB then return nil end
|
||||
_G.SB = true
|
||||
|
||||
PlacementManager.SelectionBox = script.SelectionBox:Clone()
|
||||
PlacementManager.SelectionBox.Name = "$SelectionBox"..(game:GetService("RunService"):IsServer() and "_SERVER" or "")
|
||||
PlacementManager.SelectionBox.Parent = game:GetService("Workspace"):FindFirstChildOfClass("Terrain")
|
||||
|
||||
-- Trash method TODO: Fix this
|
||||
local function findParent(i: Instance): Instance
|
||||
local f = i:FindFirstAncestorOfClass("Folder")
|
||||
local d = i
|
||||
repeat
|
||||
d = d.Parent
|
||||
until d.Parent == f
|
||||
return d
|
||||
end
|
||||
|
||||
local Mouse: Mouse = nil
|
||||
|
||||
-- Gets the block and normalid of the block (and surface) the player is looking at
|
||||
function PlacementManager:Raycast()
|
||||
if not Mouse then
|
||||
Mouse = game:GetService("Players").LocalPlayer:GetMouse()
|
||||
end
|
||||
task.synchronize()
|
||||
local objLookingAt = Mouse.Target
|
||||
local dir = Mouse.TargetSurface
|
||||
if not objLookingAt then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
|
||||
--if not objLookingAt:IsDescendantOf(ChunkManager.ChunkFolder) then return end
|
||||
local parent = findParent(objLookingAt)
|
||||
if parent:GetAttribute("ns") == true then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
PlacementManager.SelectionBox.Adornee = parent
|
||||
script.RaycastResult.Value = parent
|
||||
return parent, dir
|
||||
end
|
||||
|
||||
function PlacementManager:RaycastGetResult()
|
||||
return script.RaycastResult.Value
|
||||
end
|
||||
|
||||
local placeRemote = game:GetService("ReplicatedStorage").PlaceBlock
|
||||
local breakRemote = game:GetService("ReplicatedStorage").BreakBlock
|
||||
local tickRemote = game:GetService("ReplicatedStorage").Tick
|
||||
|
||||
-- FIRES REMOTE
|
||||
function PlacementManager:PlaceBlock(cx, cy, cz, x, y, z, blockData)
|
||||
--print("placeblock")
|
||||
--local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
--chunk:CreateBlock(x, y, z, blockData)
|
||||
placeRemote:FireServer(cx, cy, cz, x, y, z, blockData)
|
||||
end
|
||||
|
||||
-- FIRES REMOTE
|
||||
function PlacementManager:BreakBlock(cx, cy, cz, x, y, z)
|
||||
--print("breakblock")
|
||||
--local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
--chunk:RemoveBlock(x, y, z)
|
||||
breakRemote:FireServer(cx, cy, cz, x, y, z)
|
||||
end
|
||||
|
||||
-- CLIENTSIDED
|
||||
function PlacementManager:PlaceBlockLocal(cx, cy, cz, x, y, z, blockData)
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
chunk:CreateBlock(x, y, z, blockData)
|
||||
end
|
||||
|
||||
-- CLIENTSIDED
|
||||
function PlacementManager:BreakBlockLocal(cx, cy, cz, x, y, z)
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
chunk:RemoveBlock(x, y, z)
|
||||
end
|
||||
|
||||
function PlacementManager:GetBlockAtMouse(): nil | {chunk:Vector3, block: Vector3}
|
||||
local selectedPart = PlacementManager:RaycastGetResult()
|
||||
--print(selectedPart and selectedPart:GetFullName() or nil)
|
||||
if selectedPart == nil then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
return nil
|
||||
end
|
||||
if not selectedPart.Parent then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
return nil
|
||||
end
|
||||
local chunkCoords = Util.BlockPosStringToCoords(selectedPart.Parent.Name)
|
||||
local blockCoords = Util.BlockPosStringToCoords(selectedPart.Name)
|
||||
|
||||
return {
|
||||
chunk = chunkCoords,
|
||||
block = blockCoords
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
function PlacementManager:Init()
|
||||
game:GetService("RunService").Heartbeat:Connect(function()
|
||||
local a,b = pcall(function()
|
||||
PlacementManager:Raycast()
|
||||
end)
|
||||
if not a then
|
||||
task.synchronize()
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
task.desynchronize()
|
||||
end
|
||||
end)
|
||||
tickRemote.OnClientEvent:Connect(function(m, cx, cy, cz, x, y, z, d)
|
||||
--warn("PROPOGATED TICK", m, cx, cy, cz, x, y, z, d)
|
||||
if m == "B_C" then
|
||||
PlacementManager:PlaceBlockLocal(cx, cy, cz, x, y ,z, d)
|
||||
end
|
||||
if m == "B_D" then
|
||||
PlacementManager:BreakBlockLocal(cx, cy, cz, x, y ,z)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return PlacementManager
|
||||
Reference in New Issue
Block a user