开火
Coddy Roblox 游戏:塔防 课程的第 10 课,共 22 课。
面对敌人只是任务的一半。shoot 会通过 Humanoid:TakeDamage 对目标造成伤害,并在头部和目标之间画出一道短暂的闪光。下面是这道闪光的代码,可以粘贴到 runTower 上方:一个细长的霓虹部件,Debris 会在十分之一秒后将其移除(在顶部添加 local Debris = game:GetService("Debris"))。
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射击后,塔会等待其 Cooldown 冷却时间结束后才能再次射击,这就是弓箭手射速快而加农炮射速慢的原因。
当 Humanoid 的生命值降到 0 时,它的 Died 事件会触发。被击败的敌人应该立即离开游戏,因此 spawnEnemy 会将 Died 连接到销毁操作:这样 walkPath 就会发现敌人已经消失,并停止行动。
挑战
中等让防御塔开火。
- 粘贴上面的
beam。编写shoot(tower, target, info):对目标的 Humanoid 调用TakeDamage(info.Damage),然后调用beam(tower.Head.Position, target.HumanoidRootPart.Position)。 - 在
runTower中,转向后开火,然后等待info.Cooldown(没有目标时保持 0.1)。 - 在
spawnEnemy中,在敌人的 Humanoid 的Died事件触发时销毁敌人。
按下 “开始”,在起点附近建造一个 Archer:它会向 Zombies 闪光射击,敌人随后倒下。然后按下 “检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
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
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
-- 第 10 课:在这里添加开火功能
-- 防御塔的日常:瞄准射程内最近的敌人
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)
task.wait(0.1)
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.Parent = towers
task.spawn(runTower, tower)
end
-- 客户端请求在平台上建造一座防御塔
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
placeTower(pad, kind)
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 在线编辑器