Untitled

mail@pastecode.io avatar
unknown
lua
a year ago
6.6 kB
1
Indexable
Never
--Service 
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--ReplicatedValues
local Events = ReplicatedStorage:WaitForChild("Events")
local TowerModels = ReplicatedStorage:WaitForChild("TowerModels")
local Modules = ReplicatedStorage:WaitForChild("Modules")

--Events
local TowerPlacingEvent = Events:WaitForChild("TowerPlacing")

--Modules
local TowerInfoDataBase = require(Modules:WaitForChild("TowerInfoDataBase"))
local TowerDataBase = require(Modules:WaitForChild("TowerDataBase"))

--WorkspaceValues
local Camera = workspace.CurrentCamera

--PlayerValues
local Player = Players.LocalPlayer
local PlayerModels = {}

--PlayerUIValues
local PlayerUI = Player:WaitForChild("PlayerGui")
local MainUI = PlayerUI:WaitForChild("Main")
local PlayerUISize = Camera.ViewportSize.Magnitude
local CreatorUISize = 2136.795654296875


--IconsUI
local IconsUI = MainUI:WaitForChild("Icons")
local IconsFrameUI = IconsUI:WaitForChild("Icons")

--TooBarUI
local ToolBarUI = MainUI:WaitForChild("ToolBar")
local ToolBarFrameUI = ToolBarUI:WaitForChild("ToolBar")

--UpgradeMenuUI
local UIPreference = "Vertical"
local UpgradeMenuUI = MainUI:WaitForChild("UpgradeMenu")
local NextUpgradeDesc = UpgradeMenuUI:WaitForChild("NextUpgradeDesc")
local VerticalMenu = UpgradeMenuUI:WaitForChild("VerticalMenu")
local HorizontalMenu = UpgradeMenuUI:WaitForChild("HorizontalMenu")

--TowerHelpers
local SelectedTower = nil
local PlacingTower = nil

--FunctionsDefine

	--TowerFunctions
local function MouseRayCast(Blacklist)
	local MousePosition = UserInputService:GetMouseLocation()
	local MouseRay = Camera:ViewportPointToRay(MousePosition.X, MousePosition.Y)
	local RayCastParams = RaycastParams.new()
	RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParams.FilterDescendantsInstance = Blacklist
	local RayCastResult = game.Workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 5000, RayCastParams)
	return RayCastResult
end

local function TowerPlaced(Condition)
	if Condition == true then
		RunService:UnbindFromRenderStep("TowerPlacing")
		SelectedTower = PlacingTower
		PlacingTower.Parent = workspace.Towers.Spawned
		TowerPlacingEvent:FireServer(TowerDataBase.Placing[1], PlacingTower.PrimaryPart.CFrame)
		table.insert(TowerDataBase.Placed, TowerDataBase.Placing[1])
		PlacingTower.Name = #TowerDataBase.Placed
		TowerDataBase.Placing[1] = nil
		PlacingTower = nil
		warn("Tower Placed!")
	else
		RunService:UnbindFromRenderStep("TowerPlacing")
		PlacingTower:Destroy()
		TowerDataBase.Placing[1] = nil
		PlacingTower = nil
		warn("Tower destroyed")
	end
	
end

TowerPlacingEvent.OnClientEvent:Connect(function(TowerPlacer, TowerData, TowerModelCFrame, TowerNumber)
	if Player ~= TowerPlacer then
		table.insert(TowerDataBase.Placing, TowerData)
		if TowerModels:FindFirstChild(TowerData["RealName"]) then
			local TowerModel = TowerModels:FindFirstChild(TowerData["RealName"]):Clone()
			TowerModel.PrimaryPart.CFrame = TowerModelCFrame
			TowerModel.Parent = workspace.Towers.Spawned
			TowerModel.Name = TowerNumber
		end
	end
end)

local function TowerPlacing(TowerModel, RayCast)
	RayCast = MouseRayCast({TowerModel, workspace.Enemies, workspace.Towers, PlayerModels})
	if RayCast then
		TowerModel.PrimaryPart.CFrame = CFrame.new(RayCast.Position + Vector3.new(0,TowerDataBase.Placing[1].YOffset, 0)) * CFrame.Angles(0, math.rad(TowerDataBase.Placing[1].Rotation), 0)
	end
end

local function TowerSetup(Tower, ObjectName)
	local TowerModel = TowerModels[Tower]:Clone()
	TowerModel.Parent = workspace.Towers.Placing
	
	if TowerModel then
		local RayCast = nil
		PlacingTower = TowerModel
		
		local Table = {}
		for ItemName, Item in pairs(TowerInfoDataBase[Tower]) do
			Table[ItemName] = Item
		end
		Table["RealName"] = Tower
		Table["SlotName"] = ObjectName
		table.insert(TowerDataBase.Placing, Table)
		
		RunService:BindToRenderStep("TowerPlacing", 0, function(DeltaTime)
			TowerPlacing(TowerModel, RayCast)
		end)
	else
		warn(Tower, "is not a model!!!!")
	end
end

	--IconsInitiate
local function IconsInitiate()
	for Count, Object in pairs(IconsFrameUI:GetChildren()) do
		if Object:IsA("TextButton") then
			--TBC
		end
	end
end

	--ToolBarInitiate
local function ToolBarInitiate()
	for Count, Object in pairs(ToolBarFrameUI:GetChildren()) do
		if Object:IsA("TextButton") then
			Object.Activated:Connect(function()
				if PlacingTower == nil then
				    TowerSetup(Object.TowerName.Value, Object.Name)
				elseif TowerDataBase.Placing[1].SlotName == Object.Name then
					TowerPlaced(false)
				elseif PlacingTower ~= true then
					TowerPlaced(false)
					TowerSetup(Object.TowerName.Value, Object.Name)
				end
			end)
		end
	end
end

--UiStrokesAdjust
local function UIStrokesAdjust()
	for objectName, Object in ipairs(PlayerUI:GetDescendants()) do
		if Object:IsA("UIStroke") then
			Object.Thickness *= PlayerUISize / CreatorUISize
		end
	end
end
--InputServiceInitiate
local function InputServiceInitiate()
	UserInputService.InputEnded:Connect(function(InputObject, Processed)
		if Processed then
			return
		end
		
		if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
			if PlacingTower ~= nil then
				TowerPlaced(true)
			else
				warn("Not placing a tower!")
			end
		end
	end)
end

--InitiatePlayerScan
local function PlayerScan()
	for Name, Player in ipairs(Players:GetPlayers()) do
		if Player.Character or Player.CharacterAdded:Wait() then
			table.insert(PlayerModels, Player.Character)
		end
	end
end

Players.PlayerAdded:Connect(function(Player)
	if Player.Character or Player.CharacterAdded:Wait() then
		table.insert(PlayerModels, Player.Character)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	PlayerModels[Player] = nil
end)

--RenderSteppedLoop
RunService.RenderStepped:Connect(function(DeltaTime)
	if SelectedTower then
		UpgradeMenuUI.Enabled = true
		if UIPreference == "Vertical" then
			VerticalMenu.Visible = true
			HorizontalMenu.Visible = false
		else
			HorizontalMenu.Visible = true
			VerticalMenu.Visible = false
		end
	else
		UpgradeMenuUI.Enabled = false
	end
end)

--Functions Run

	--Player
PlayerScan()

	--UI
ToolBarInitiate()
IconsInitiate()
UIStrokesAdjust()

	--Input
InputServiceInitiate()