Menu
CoddyTech

奖励

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

建造防御塔需要钱,而钱可以通过击败敌人获得。每种敌人在设置中都有一个 Reward:僵尸是 6,暴徒是 15。

在塔防游戏中,所有玩家都在同一个服务器中一起游戏,所以敌人死亡时,每位玩家都会获得奖励。Players:GetPlayers() 会列出游戏中的所有玩家:

for _, player in Players:GetPlayers() do
	player.leaderstats.Money.Value += info.Reward
end

奖励写在 Died 处理程序中,因此,抵达基地的敌人不会提供奖励。

challenge icon

挑战

中等

每当防御塔击败一个敌人,就给玩家发放奖励。

  1. 在 spawnEnemy 的 Died 处理程序中,销毁敌人之前,遍历 Players:GetPlayers()。
  2. 将 info.Reward 加到每位玩家的 leaderstats.Money.Value 中。

按“开始”并建造一个 Archer:每击败一个 Zombie,Money 就增加 6。然后按“检查”。

亲手试试

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()
		-- 第 11 课:在这里添加奖励
		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.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 在线编辑器