-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildManagerComponent.lua
More file actions
82 lines (82 loc) · 2.91 KB
/
BuildManagerComponent.lua
File metadata and controls
82 lines (82 loc) · 2.91 KB
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
local BuildManagerComponent = {}
--|| MODULE SETTINGS ||--
BuildManagerComponent.GridSize = 16
BuildManagerComponent.BuildDistance = 2
--|| MODULE VARIABLES ||--
BuildManagerComponent.isBuilding = false
BuildManagerComponent.SelectedBuild = "Wall"
--|| TABLES & DICTIONARIES ||--
local BuildingKeybinds = {
[Enum.KeyCode.Q] = "Wall",
[Enum.KeyCode.C] = "Floor",
[Enum.KeyCode.V] = "Ramp"
}
--|| SERVICES ||--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--|| FOLDERS ||--
local BuildingEventsFolder = ReplicatedStorage:WaitForChild("BuildingEvents")
--|| REMOTE EVENTS ||--
local PlaceBuildEvent = BuildingEventsFolder:WaitForChild("PlaceBuild")
--|| PRIVATE FUNCTIONS ||--
local function GridSnap(Value, Size)
return (math.floor(Value/Size + 0.5) * Size)
end
local function GetTouchingParts(Part)
local Connection = Part.Touched:Connect(function() end)
local Results = Part:GetTouchingParts()
Connection:Disconnect()
return Results
end
--|| MODULE FUNCTIONS ||--
function BuildManagerComponent.GetNextBuildPosition(HumanoidRootPartPosition, MouseLookVector3)
local DirectionVector3 = MouseLookVector3 * BuildManagerComponent.BuildDistance
DirectionVector3 += HumanoidRootPartPosition
return Vector3.new(
GridSnap(DirectionVector3.X, BuildManagerComponent.GridSize),
GridSnap(DirectionVector3.Y, BuildManagerComponent.GridSize) + BuildManagerComponent.GridSize/2,
GridSnap(DirectionVector3.Z, BuildManagerComponent.GridSize)
)
end
function BuildManagerComponent.GetNextBuildRotation(Vector)
if(typeof(Vector) == "Vector3") then
local Y = math.atan2(Vector.X, Vector.Z)
return Vector3.new(0,GridSnap(Y, math.rad(-90)), 0)
end
end
--|| TOGGLE BUILDING ||--
function BuildManagerComponent.ToggleBuildMode(ActionName, InputState, InputObject)
if ActionName == "ToggleBuild" then
if InputState == Enum.UserInputState.Begin then
BuildManagerComponent.isBuilding = not BuildManagerComponent.isBuilding
warn("Toggled Build Mode: ", BuildManagerComponent.isBuilding)
end
end
end
--|| SWITCH TO A DIFFERENT BUILD( Ramp, Wall, Floor, Etc) ||--
function BuildManagerComponent.SwitchBuild(ActionName, InputState, InputObject)
if(ActionName == "SwitchBuild") then
if(InputState == Enum.UserInputState.Begin) then
BuildManagerComponent.isBuilding = true
if(BuildManagerComponent.isBuilding and BuildingKeybinds[InputObject.KeyCode]) then
BuildManagerComponent.SelectedBuild = BuildingKeybinds[InputObject.KeyCode]
end
end
end
end
function BuildManagerComponent.PlaceBuild(BuildMesh, BuildName)
if(BuildManagerComponent.isBuilding) then
local Results = GetTouchingParts(BuildMesh)
local Placeable = true
for _, Build in pairs(Results) do
if Build.Name == BuildMesh.Name then
if Build.Position == BuildMesh.Position then
Placeable = false
end
end
end
if(Placeable) then
PlaceBuildEvent:FireServer(BuildName, BuildMesh.Position, BuildMesh.Orientation)
end
end
end
return BuildManagerComponent