Menu
CoddyTech

החנות

שיעור 7 מתוך 22 בקורס משחק Roblox: הגנת מגדלים של Coddy.

עכשיו הצד של השחקן. DefenseClient הוא LocalScript בתוך ה-HUD, שהוא ScreenGui ש-Roblox מעתיקה למסך של כל שחקן, ולכן script.Parent הוא ה-HUD. המסגרת Shop שלו מכילה TextButton לכל מגדל, ששמו כשם המגדל.

LocalScript יכול להתחיל לפעול לפני שאובייקטי המשחק מגיעים למכשיר של השחקן, ולכן הוא מוצא אותם באמצעות WaitForChild. האירוע Activated של כפתור מופעל כשלוחצים עליו או מקישים עליו:

local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))
local button = script.Parent.Shop.Frost
button.Text = "Frost tower"
button.Activated:Connect(function()
	print("Frost picked")
end)
challenge icon

אתגר

בינוני

הוסף מחירים לכפתורי החנות ב־DefenseClient.

  1. הוסף local Settings = require(game.ReplicatedStorage:WaitForChild("Settings")) וגם local hud = script.Parent.
  2. עבור כל TextButton שנמצא ב־hud.Shop, הגדר את הטקסט שלו ל־button.Name .. " $" .. Settings.Towers[button.Name].Price.
  3. בעת הפעלת Activated שלו, שמור את שמו ב־local selected.

לחץ על "שחק": החנות מציגה Archer $60, Cannon $120 ו־Frost $90. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local enemies = workspace.Enemies

print("The base can take " .. Settings.BaseHealth .. " hits")

-- נקודות הציון, לפי הסדר (הן נקראות 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

-- הבריאות של הבסיס, כתכונה של workspace: כל לקוח יכול לקרוא אותה
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)

-- כל שחקן מקבל Money בלוח המובילים
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = Settings.StartMoney
	money.Parent = leaderstats
end)

-- אויב הגיע לבסיס
local function damageBase(amount)
	local health = math.max(0, workspace:GetAttribute("BaseHealth") - amount)
	workspace:SetAttribute("BaseHealth", health)
	print("Base: " .. health)
end

-- הולך עם אויב לאורך המסלול. MoveTo מפסיק לנסות אחרי 8 שניות ומדווח על כך
-- (MoveToFinished עם false), לכן מורים לאויב איטי להתקדם שוב עד שיגיע.
local function walkPath(enemy)
	local humanoid = enemy.Humanoid
	for i = 2, #waypoints do
		repeat
			humanoid:MoveTo(waypoints[i].Position)
		until humanoid.MoveToFinished:Wait() or not enemy.Parent
		if not enemy.Parent then
			return
		end
	end
	-- הוא הגיע לבסיס
	damageBase(Settings.Enemies[enemy.Name].Damage)
	enemy:Destroy()
end

-- אויב חדש בשער, מתקדם לאורך המסלול
local function spawnEnemy(kind)
	local enemy = ServerStorage.Enemies[kind]:Clone()
	-- מוצב ממש מעל נקודת הציון הראשונה
	enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
	local info = Settings.Enemies[kind]
	enemy.Humanoid.MaxHealth = info.Health
	enemy.Humanoid.Health = info.Health
	enemy.Humanoid.WalkSpeed = info.Speed
	enemy.Parent = enemies
	task.spawn(walkPath, enemy)
	return enemy
end

-- גל אחד: האויבים שלו בזה אחר זה, ואז ממתינים עד שכולם ייעלמו
local function runWave(list)
	for _, kind in list do
		spawnEnemy(kind)
		task.wait(Settings.SpawnGap)
	end
	while #enemies:GetChildren() > 0 do
		task.wait(0.2)
	end
end

-- הגלים, כל אחד אחרי הפסקה
for i, list in Settings.Waves do
	task.wait(Settings.WaveBreak)
	runWave(list)
end

כל השיעורים ביחידה משחק Roblox: הגנת מגדלים

תרגלו בעצמכם: Roblox אונליין