Загрузка данных


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))

local getDataFunc = ReplicatedStorage:WaitForChild("GetData")
local interactItemFunc = ReplicatedStorage:WaitForChild("InteractItem")

local Players = game:GetService("Players")
local Crystals = Players.LocalPlayer:WaitForChild("Crystals") 
local Gems = Players.LocalPlayer:WaitForChild("Gems") 

local RemoteEvent2 = ReplicatedStorage.SoundBuy
local RemoteEvent = ReplicatedStorage.SoundOtmena
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local gui = script.Parent
local exit = gui.Container.Exit
local shop = gui.ShopGUI
local Crystals = gui.Container.Crystals
local Gems = gui.Container.Gems
local limit = gui.Container.Limit
local itemsFrame = gui.Container.ItemsFrame
local itemBuyT = gui.Container.ItemsFrame2

local playerData = {}

local function setAnimation(object, animName)
	local humanoid = object:WaitForChild("Humanoid")
	local animationsFolder = object:WaitForChild("Animations")

	if humanoid and animationsFolder then
		local animationObject = animationsFolder:WaitForChild(animName)

		if animationObject then
			local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)

			local playingTracks = animator:GetPlayingAnimationTracks()
			for i, track in pairs(playingTracks) do
				if track.Name == animName then
					return track
				end
			end

			local animationTrack = animator:LoadAnimation(animationObject)		
			return animationTrack		
		end
	end
end

local function playAnimation(object, animName)
	local animationTrack = setAnimation(object, animName)

	if animationTrack then
		animationTrack:Play()
	else
		warn("Animation track does not exist")
		return
	end
end

local function getItemStatus(itemName)
	if table.find(playerData.SelectedTowers, itemName) then
		return "Equipped"
	elseif table.find(playerData.OwnedTowers, itemName) then
		return "Owned"
	else
		return "For Sale"
	end
end

local function interactItem(itemName)
	local data = interactItemFunc:InvokeServer(itemName)
	if data then
		playerData = data
		updateItems()
	end
end

function updateItems()
	Crystals.Text = playerData.Crystals
	Gems.Text = playerData.Gems
	limit.Text = #playerData.SelectedTowers .. "/5"

	for i, tower in pairs(towers) do
		-- Find any old buttons
		local oldButton = itemsFrame:FindFirstChild(tower.Name)
		if oldButton then
			oldButton:Destroy()
		end

		local newButton = itemsFrame.TemplateButton:Clone()
		newButton.Name = tower.Name
		newButton.TowerName.Text = tower.Name
		newButton.Visible = true
		newButton.LayoutOrder = tower.Price
		newButton.Parent = itemsFrame

		-- Создаём WorldModel и клонируем башню
		local towerTemplate = ReplicatedStorage.Towers:WaitForChild(tower.Name):Clone()
		local worldModel = Instance.new("WorldModel")
		worldModel.Parent = newButton.ViewportFrame
		towerTemplate.Parent = worldModel

		local camera = Instance.new("Camera")
		camera.Parent = newButton.ViewportFrame
		newButton.ViewportFrame.CurrentCamera = camera

		-- Правильная настройка камеры: лицом к зрителю
		if towerTemplate.PrimaryPart then
			local partPos = towerTemplate.PrimaryPart.Position
			-- Камера располагается спереди модели (отрицательная Z) и немного выше
			local offset = Vector3.new(0, 1.5, -4)
			camera.CFrame = CFrame.new(partPos + offset, partPos)
		else
			camera.CFrame = CFrame.new(0, 0, 5)
		end

		playAnimation(towerTemplate, "Idle")

		local status = getItemStatus(tower.Name)
		if status == "For Sale" then
			if tower.Currency == "Crystals" then
				newButton.Status.Text = tower.Price
				newButton.Status.TextColor3 = Color3.new(0.545098, 0, 0.819608)
			elseif tower.Currency == "Gems" then
				newButton.Status.Text = tower.Price
				newButton.Status.TextColor3 = Color3.new(0.00392157, 0.768627, 0.901961)
			end
		elseif status == "Equipped" then
			newButton.Status.Text = "Equipped"
			newButton.Status.TextColor3 = Color3.new(1,1,1)
		else
			newButton.Status.Text = ""
		end

		newButton.Activated:Connect(function()
			itemBuyT.Visible = true
			gui.click:Play()

			local olPldButton = itemBuyT:FindFirstChild(tower.Name)
			if olPldButton then
				olPldButton:Destroy()
			end

			local oldPldButton = itemBuyT:GetChildren()
			for x,o in pairs(oldPldButton) do
				if not o:IsA("UICorner" or "UIAspectRatioConstrain") then
					if o:IsA("ImageButton") and o.Visible == true then
						o:Destroy()
					end
				end
			end

			local PldButton = itemBuyT.TemplateButton:Clone()
			PldButton.Name = tower.Name
			PldButton.TowerName.Text = tower.Name
			PldButton.Desc.Range.Text = tower.Range
			PldButton.Desc.Damage.Text = tower.Damage
			PldButton.Desc.Reloading.Text = tower.Reloading
			PldButton.Desc.Cost.Text = tower.Cost
			PldButton.Image = tower.ImageAsset
			PldButton.LayoutOrder = tower.Price
			PldButton.Visible = true
			PldButton.Parent = itemBuyT

			local status = getItemStatus(tower.Name)
			if status == "For Sale" then
				if tower.Currency == "Crystals" then
					PldButton.Status.Text = tower.Price
					PldButton.Status.BackgroundColor3 = Color3.new(0.545098, 0, 0.819608)
				elseif tower.Currency == "Gems" then
					PldButton.Status.Text = tower.Price
					PldButton.Status.BackgroundColor3 = Color3.new(0.00392157, 0.768627, 0.901961)
				end
			elseif status == "Equipped" then
				PldButton.Status.Text = "Equipped"
				PldButton.Status.BackgroundColor3 = Color3.new(0,1,0)
			else
				PldButton.Status.Text = "Unequipped"
				PldButton.Status.BackgroundColor3 = Color3.new(0,1,0)
			end
			PldButton.Status.Activated:Connect(function()
				local status = getItemStatus(tower.Name)
				if status == "Equipped" and limit.Text ~= "1/5" then
					PldButton.Status.Text = "Unequipped"
					PldButton.Status.BackgroundColor3 = Color3.new(0,1,0)
					gui.Equipped:Play()
				elseif status == "Owned" then
					PldButton.Status.Text = "Equipped"
					PldButton.Status.BackgroundColor3 = Color3.new(0,1,0)
					gui.Unequipped:Play()
				end
				if status == "Equipped" and limit.Text == "1/5" then
					gui.otmena:Play()
				end
				interactItem(tower.Name)
			end)
			RemoteEvent2.OnClientEvent:Connect(function()
				local SoundCash
				SoundCash = math.random(1, 3)
				if SoundCash == 1 then
					gui.kassa:Play()
				elseif SoundCash == 2 then
					gui.kassa2:Play()
				else
					gui.kassa3:Play()
				end
				PldButton.Status.Text = "Unequipped"
				PldButton.Status.BackgroundColor3 = Color3.new(0,1,0) 
			end)
		end)
	end
end

RemoteEvent.OnClientEvent:Connect(function()
	gui.otmena:Play()
end)

local function toggleShop()
	gui.Container.Visible = not gui.Container.Visible
	if gui.Container.Visible then
		itemBuyT.Visible = false
		playerData = getDataFunc:InvokeServer()
		updateItems()
		gui.StoreDoorBell:Play()
	end
end

local function setupShop()
	local isInShop = false
	humanoid.Touched:Connect(function(touchedPart)
		if touchedPart == workspace.spawn.lavka.ShopPad then
			if isInShop then return end
			isInShop = true
			toggleShop()
		end
	end)

	shop.Activated:Connect(toggleShop)
	shop.Activated:Connect(function()
		gui.click:Play()
	end)

	exit.Activated:Connect(toggleShop)
	exit.Activated:Connect(function()
		gui.click:Play()
		isInShop = false
	end)
end

setupShop()