Menu
CoddyTech

הצבת מגדל

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

לחיצה על אחד המשטחים האפורים בונה עליו את המגדל שנבחר. הלקוח קורא את הלחיצה מהעכבר: me:GetMouse(), האירוע Button1Down שלו, והמאפיין Target שלו, החלק שמתחת למצביע. אבל הוא לא יכול לבנות שום דבר בעצמו: המגדלים נמצאים בשרת. הוא שולח בקשה באמצעות RemoteEvent Remotes.Place:

-- לקוח
remotes.Place:FireServer(workspace.Pads.Pad4, "Frost")
-- שרת: Roblox מוסיף את השחקן תחילה
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
end)

מודלי המגדלים ממתינים ב-ServerStorage.Towers; השרת משכפל אחד מהם אל המשטח, כשהוא מורם בחצי מגובה המשטח ועוד 1, כך שהוא עומד עליו.

challenge icon

אתגר

בינוני

בנה את המגדל שנבחר על המשטח שעליו לוחצים.

  1. ב־DefenseClient, קבל את remotes (באמצעות WaitForChild) ואת me, שהוא ה־LocalPlayer.
  2. בעת לחיצה על me:GetMouse().Button1Down, אם mouse.Target נמצא בתוך workspace.Pads ו־selected מוגדר, קרא ל־remotes.Place:FireServer(target, selected).
  3. ב־DefenseServer, remotes.Place.OnServerEvent קורא ל־placeTower(pad, kind): שכפל את ServerStorage.Towers[kind] והצב אותו על המשטח, בתוך workspace.Towers.

לחץ על "שחק", בחר Archer ולחץ על משטח אפור: Archer יעמוד עליו. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

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 אונליין