Menu
CoddyTech

瞄准

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

炮塔会守卫其Range范围内的地面。每个炮塔都在自己的线程中运行独立的循环:找到范围内最近的存活敌人,并转动它的Head朝向该敌人。

寻找最近的敌人是一个经典的循环:一开始不选任何敌人,并把范围设为当前最短距离;然后,只要发现存活的敌人(它有HumanoidRootPart,且Health > 0)比当前记录的距离更近,就保留它。

local best, bestDistance = nil, range
for _, enemy in enemies:GetChildren() do
	-- 比目前最近的还近?那就选它。
end
return best

CFrame.lookAt(from, at)是一个位于from并朝向at的 CFrame,因此可以在不移动头部的情况下转动它:

tower.Head.CFrame = CFrame.lookAt(tower.Head.Position, target.HumanoidRootPart.Position)

无限循环的每一轮都必须等待,否则游戏会卡住:无论是否找到目标,都等待 0.1 秒。

challenge icon

挑战

中等

让一座塔转向最近的敌人。

  1. 编写 nearestEnemy(position, range):找出距离 position 最近且位于 range 范围内的存活敌人;如果没有,则返回 nil。
  2. 编写 runTower(tower):只要塔还在游戏中,就让它的 Head 转向 Settings.Towers[tower.Name].Range 范围内最近的敌人,然后执行 task.wait(0.1)。
  3. 在 placeTower 的末尾添加 task.spawn(runTower, tower)。

按“开始”,在起点附近建造一座 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

-- 第 9 课:在这里添加瞄准功能

-- 在底座上建造一座塔
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
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 在线编辑器