升级
Coddy Roblox 游戏:塔防 课程的第 14 课,共 22 课。
每升一级,塔的伤害都会再次增加,所以 3 级 Archer 的攻击伤害为 12。客户端只会通过 Remotes.Upgrade 请求升级;服务器会检查塔和金币,扣除费用并设置新等级。
面板会显示服务器确定的等级:它会监听所选塔的 Level 属性,并在属性变化时重新绘制。将这个连接保存在一个变量中,并在选择另一座塔时断开旧连接:
levelChanged = tower:GetAttributeChangedSignal("Level"):Connect(showPanel)挑战
中等在面板中升级炮塔。
- 在
DefenseClient中,面板的Upgrade按钮会将chosen传给remotes.Upgrade,而choose会将炮塔的GetAttributeChangedSignal("Level")连接到showPanel。 - 在
remotes.Upgrade.OnServerEvent(player, tower)中:如果目标不在towers中,或价格太高,就拒绝操作;否则扣除费用,并将Level加 1。 - 在
shoot中,将伤害乘以炮塔的Level。
按“开始”,然后升级一个 Archer:面板会显示 Archer 等级 2。接着按“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local enemies = workspace.Enemies
local towers = workspace.Towers
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
enemy.Humanoid.Died:Connect(function()
-- 每个玩家都获得奖励
for _, player in Players:GetPlayers() do
player.leaderstats.Money.Value += info.Reward
end
enemy:Destroy()
end)
task.spawn(walkPath, enemy)
return enemy
end
-- 距离指定位置最近、在指定范围内且仍存活的敌人,如果没有则返回 nil
local function nearestEnemy(position, range)
local best, bestDistance = nil, range
for _, enemy in enemies:GetChildren() do
local root = enemy:FindFirstChild("HumanoidRootPart")
if root and enemy.Humanoid.Health > 0 then
local distance = (root.Position - position).Magnitude
if distance <= bestDistance then
best, bestDistance = enemy, distance
end
end
end
return best
end
-- 从防御塔射向目标的一道闪光
local function beam(from, to)
local ray = Instance.new("Part")
ray.Anchored = true
ray.CanCollide = false
ray.CanTouch = false
ray.Material = Enum.Material.Neon
ray.Color = Color3.fromRGB(255, 240, 150)
ray.Size = Vector3.new(0.2, 0.2, (to - from).Magnitude)
ray.CFrame = CFrame.lookAt((from + to) / 2, to)
ray.Parent = workspace
Debris:AddItem(ray, 0.1)
end
-- 射击:对目标造成伤害
local function shoot(tower, target, info)
local damage = info.Damage
local at = target.HumanoidRootPart.Position
target.Humanoid:TakeDamage(damage)
beam(tower.Head.Position, at)
end
-- 防御塔的工作流程:瞄准范围内最近的敌人,射击,然后等待冷却结束
local function runTower(tower)
local info = Settings.Towers[tower.Name]
while tower.Parent do
local target = nearestEnemy(tower.Head.Position, info.Range)
if target then
tower.Head.CFrame = CFrame.lookAt(tower.Head.Position, target.HumanoidRootPart.Position)
shoot(tower, target, info)
task.wait(info.Cooldown)
else
task.wait(0.1)
end
end
end
-- 在建造台上建造防御塔
local function placeTower(pad, kind)
local tower = ServerStorage.Towers[kind]:Clone()
tower:PivotTo(pad.CFrame + Vector3.new(0, pad.Size.Y / 2 + 1, 0))
tower:SetAttribute("Level", 1)
tower.Parent = towers
pad:SetAttribute("Used", true)
task.spawn(runTower, tower)
end
-- 客户端请求在建造台上建造防御塔。检查所有条件:建造台是否真实存在且未被占用,
-- 防御塔是否真实存在,以及钱是否足够支付。
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
if typeof(pad) ~= "Instance" or pad.Parent ~= workspace.Pads or pad:GetAttribute("Used") then
return
end
local info = typeof(kind) == "string" and Settings.Towers[kind]
if not info then
return
end
local money = player.leaderstats.Money
if money.Value < info.Price then
return
end
money.Value -= info.Price
placeTower(pad, kind)
end)
-- 第 14 课:在这里添加升级功能
-- 一波敌人:逐个生成敌人,然后等待它们全部消失
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 在线编辑器