Menu
CoddyTech

冰冻

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

冰霜塔造成的伤害很低:它的攻击会让敌人的速度降至原来的一小部分,也就是 info.Slow,持续 2 秒,这样其他防御塔就能多攻击它几次。

有两件事会导致减速效果出错。被减速的敌人再次受到攻击时,速度不能越来越慢,所以要给它标记一个属性,并在标记存在时跳过它。恢复的速度应该是敌人自身的速度,也就是设置中的速度,而不是它现在行走的速度。task.delay 会稍后运行一个函数,不会让防御塔停下来等待:

task.delay(2, function()
	-- 两秒后
end)
challenge icon

挑战

中等

让 Frost 炮塔减慢敌人的速度。

  1. 编写 slow(enemy, factor):如果敌人没有 Slowed 属性,就设置该属性,并将 WalkSpeed 设为 Settings 中的速度乘以 factor。
  2. 2 秒后(使用 task.delay),如果敌人仍有父级,就恢复速度并清除 Slowed。
  3. 当设置了 info.Slow 时,shoot 会减慢仍在游戏中的目标。

按“开始”并建造一座 Frost 炮塔:被它击中的僵尸会减速。然后按“检查”。

亲手试试

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
local towerPad = {} -- 每座塔 -> 它所在的平台

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

-- 第 17 课:在这里添加 Frost

-- 一次射击:对目标造成伤害(如果是 Cannon,还会伤害目标附近的所有敌人)
local function shoot(tower, target, info)
	local damage = info.Damage * tower:GetAttribute("Level")
	local at = target.HumanoidRootPart.Position
	if info.Splash then
		for _, enemy in enemies:GetChildren() do
			local root = enemy:FindFirstChild("HumanoidRootPart")
			if root and (root.Position - at).Magnitude <= info.Splash then
				enemy.Humanoid:TakeDamage(damage)
			end
		end
	else
		target.Humanoid:TakeDamage(damage)
	end
	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)
	towerPad[tower] = pad
	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)

-- 升级:下一级的费用越来越高,每升一级都会再增加一次
-- 防御塔的伤害值
remotes.Upgrade.OnServerEvent:Connect(function(player, tower)
	if typeof(tower) ~= "Instance" or tower.Parent ~= towers then
		return
	end
	local level = tower:GetAttribute("Level")
	local cost = math.floor(Settings.Towers[tower.Name].Price * Settings.UpgradeCost * level)
	local money = player.leaderstats.Money
	if money.Value < cost then
		return
	end
	money.Value -= cost
	tower:SetAttribute("Level", level + 1)
end)

-- 出售:返还一半价格,底座也重新空出来
remotes.Sell.OnServerEvent:Connect(function(player, tower)
	if typeof(tower) ~= "Instance" or tower.Parent ~= towers then
		return
	end
	player.leaderstats.Money.Value += math.floor(Settings.Towers[tower.Name].Price / 2)
	towerPad[tower]:SetAttribute("Used", nil)
	towerPad[tower] = nil
	tower:Destroy()
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 在线编辑器