Menu
CoddyTech

溅射

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

加农炮速度较慢,但炮弹会爆炸:它会伤害距离命中目标 Splash 个 studs 以内的所有敌人。敌人排成一列行走,因此一发炮弹可以击中多个敌人。

只有设置中的加农炮这一行有 Splash,所以其他炮塔的 info.Splash 为 nil,而一个 if 就能选择射击类型:

if info.Splash then
	-- 目标周围 info.Splash 范围内的所有人
else
	target.Humanoid:TakeDamage(damage)
end
challenge icon

挑战

中等

让 Cannon 的炮弹爆炸。

  1. 在 shoot 中,当设置了 info.Splash 时,对距离 at 不超过 info.Splash studs 的每个敌人造成伤害,其中 HumanoidRootPart 位于该范围内。
  2. 否则,像之前一样,只对目标造成伤害。

按“开始”,在路径旁建造一个 Cannon:一发炮弹会对目标附近的每个 Zombie 造成伤害。然后按“检查”。

亲手试试

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

-- 开火:对目标造成伤害
local function shoot(tower, target, info)
	local damage = info.Damage * tower:GetAttribute("Level")
	local at = target.HumanoidRootPart.Position
	-- 第 16 课:在这里添加 Splash
	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)
	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 在线编辑器