Skip to content

Instantly share code, notes, and snippets.

@AeteRnuMG
Created February 25, 2024 20:31
Show Gist options
  • Save AeteRnuMG/9343987d0796308f5ab35a389b7ba77e to your computer and use it in GitHub Desktop.
Save AeteRnuMG/9343987d0796308f5ab35a389b7ba77e to your computer and use it in GitHub Desktop.
--- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
--- Instances
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
--- Directories
local PlayerScripts = Player:WaitForChild("PlayerScripts").Rojo
local RojoSharedModules = ReplicatedStorage:WaitForChild("RojoModules")
local TowersModels = ReplicatedStorage:WaitForChild("TowersModels")
--- Modules
local ClientData = require(PlayerScripts:WaitForChild("Data"))
local InputManager = require(PlayerScripts:WaitForChild("InputManager"))
local NetBridges = require(RojoSharedModules:WaitForChild("NetBridges"))
local TowersInfo = require(RojoSharedModules:WaitForChild("TowersInfo"))
local Utils = require(script:WaitForChild("Utils"))
local Selection = require(script:WaitForChild("Selection"))
--- Values
local PlacementConnection: RBXScriptConnection?
local CurrentHollow: Model?
local HollowRange: Part?
local CurrentSlot: string?
local isPlacing = false
--- Makes Mouse Raycast Params
local function GetMouseRayParams()
local MouseRayParams = RaycastParams.new()
MouseRayParams.FilterType = Enum.RaycastFilterType.Exclude
MouseRayParams.FilterDescendantsInstances = { Character, CurrentHollow }
return MouseRayParams
end
--- Turns Given Model's Parts Into Forcefield
local function ModelToHollow(Model: Model?)
if not Model then
return
end
for i, Descendant: BasePart | Instance in pairs(Model:GetDescendants()) do
if Descendant:IsA("BasePart") and not Descendant:HasTag("RedArea") then
Descendant.Material = Enum.Material.ForceField
Descendant.Transparency = .4
end
end
Model.Parent = workspace
local RayResult = Utils.RaycastFromMouse(GetMouseRayParams())
if RayResult then
if RayResult.Position then
Model:PivotTo(CFrame.new(RayResult.Position))
end
end
end
--- Change Color Of Hollow's Parts
-- local function ModelHollowColor(Model: Model, newColor: Color3)
-- for i, Descendant: BasePart? in pairs(Model:GetDescendants()) do
-- if Descendant:IsA("BasePart") then
--
-- if Descendant.Color == newColor then
-- continue
--
-- end
--
-- Descendant.Color = newColor
-- end
-- end
-- end
--- Main Placing Loop
local function StartPlacing()
PlacementConnection = RunService.RenderStepped:Connect(function()
if not (CurrentHollow or isPlacing) and PlacementConnection then
PlacementConnection:Disconnect()
end
local RayResult = Utils.RaycastFromMouse(GetMouseRayParams())
if RayResult and (CurrentHollow and HollowRange) then
local RayCFrame = CFrame.new(RayResult.Position)
local TowerInfo = TowersInfo(CurrentHollow.Name)
if RayResult.Instance:HasTag(TowerInfo['Type']) then
CurrentHollow:PivotTo(CurrentHollow:GetPivot():Lerp(RayCFrame, .3))
HollowRange.Color = Color3.new(0.411764, 1, 0.411764)
else
CurrentHollow:PivotTo(CurrentHollow:GetPivot():Lerp(RayCFrame, .3))
HollowRange.Color = Color3.new(1, 0, 0)
end
local HollowPosition = CurrentHollow:GetPivot().Position
HollowRange.Position = Vector3.new(HollowPosition.X, Utils.GetConstantYPosition(HollowPosition) or HollowPosition.Y, HollowPosition.Z)
end
end)
end
--- Stop Placing Tower, Disconnect Connections And Destroy Models/Parts
local function StopPlacing()
if PlacementConnection then
PlacementConnection:Disconnect()
end
CurrentSlot = nil
isPlacing = false
Utils.TowersRedAreasVisibility(false)
Selection.StartSeeking()
if CurrentHollow then
CurrentHollow:Destroy()
if HollowRange then
HollowRange:Destroy()
end
HollowRange = nil
CurrentHollow = nil
end
end
--- Place Stop Placing COnnection And Request Tower Placement
local function PlaceTower()
local MouseScreenPos = UserInputService:GetMouseLocation()
local Ray = Camera:ScreenPointToRay(MouseScreenPos.X, MouseScreenPos.Y - (Camera.ViewportSize.Y * if not RunService:IsStudio() then 0.03 else 0.075))
local Content = {
Action = "Place",
Slot = CurrentSlot,
RayOrigin = Ray.Origin,
RayDirection = Ray.Direction,
}
NetBridges.Placement:Fire(Content)
print("Fired Placement - Client")
return
end
local function InputHandler(ActionInfo: InputManager.Action)
local SplittedAction = ActionInfo.Action:split(' ')
if SplittedAction[1] == "Slot" then
local TowerId = ClientData.Data.Loadout[ActionInfo.Action]
local TowerInInventory = ClientData.Data.TowersInventory[TowerId]
if isPlacing then
StopPlacing()
return
end
local TowerModel = TowerInInventory and TowersModels:FindFirstChild(TowerInInventory.Name) or nil
if TowerModel then
CurrentSlot = ActionInfo.Action
isPlacing = true
CurrentHollow = TowerModel:Clone()
ModelToHollow(CurrentHollow)
if CurrentHollow then
local Info = TowersInfo(CurrentHollow.Name)
if Info then
HollowRange = Selection.CreateRange(CurrentHollow, Info.Range, CurrentHollow:GetPivot().Position, .55)
end
end
StartPlacing()
Utils.TowersRedAreasVisibility(true)
Selection.StopSeeking()
end
end
if ActionInfo.Action == "Click" and isPlacing then
local RayResult = Utils.RaycastFromMouse(GetMouseRayParams())
if RayResult and RayResult.Instance and CurrentHollow then
local TowerInfo = TowersInfo(CurrentHollow.Name)
if RayResult.Instance:HasTag(TowerInfo['Type']) then
PlaceTower()
end
end
StopPlacing()
end
end
--- Connect To Actions Invoke
InputManager.ActionBegan:Connect(InputHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment