Menu
CoddyTech

商店

Coddy Roblox 游戏:塔防 课程的第 7 课,共 22 课。

现在来处理玩家这一侧。DefenseClient 是 HUD 内的一个 LocalScript,HUD 是 Roblox 复制到每位玩家屏幕上的 ScreenGui,因此 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. 对于 hud.Shop 中的每个 TextButton,将其文本设置为 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 在线编辑器