Menu
CoddyTech

砸地

Coddy Roblox 游戏:竞技场对战 课程的第 10 课,共 25 课。

第二个技能按 E 键释放:重击地面,击中你周围的所有人。workspace:GetPartBoundsInRadius(position, radius, params)会查找距离某个点一定范围内的部件,就像挥击的方框,不过范围是圆形的:

for _, part in workspace:GetPartBoundsInRadius(root.Position, 6, params) do

现在游戏中有两种伤害他人的方式,之后还会有更多。每次攻击都会调用一个函数 hurt(humanoid, amount),因此伤害规则(很快会加入格挡)只需编写一次。它也会放过已经被击败的 Humanoid。

challenge icon

挑战

中等

在 E 键上添加一次地面猛击。

  1. 在 swing 上方,编写 hurt(humanoid, amount):除非 Health 为 0,否则调用 TakeDamage(amount)。Swing 调用它,而不是调用 TakeDamage 和 print。
  2. 添加 abilities.Slam(player, character, root):在 GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) 范围内的每个 Humanoid(每个只处理一次,不包括你自己)都会受到 hurt(humanoid, Settings.SlamDamage) 的伤害。
  3. 在 BattleClient 中,将 "Slam" 绑定到 E 键,并将它添加到 shown 中。

按“开始”,然后在假人旁按 E 键:它的血条会下降。然后按“检查”。

亲手试试

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player → 上次挥剑的时间

-- Roblox 自带的剑术动画
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- 挥剑:先播放动画,再击中你面前判定框内的所有目标
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- 你面前的判定框,不包括你自己的身体
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- 每次挥剑,每个 humanoid 只命中一次
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and not hit[humanoid] then
			hit[humanoid] = true
			humanoid:TakeDamage(Settings.SwordDamage)
			print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
		end
	end
end

-- 按名称列出各项技能及其作用
local abilities = {}

-- 玩家的设备负责移动角色,所以冲刺也在设备上执行
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- 第 10 课:在这里加入 Ground Slam

-- 客户端按名称请求使用技能。服务器会检查:技能是否存在、
-- 玩家是否存活,以及冷却时间是否结束。技能下次可以使用的时间
-- 是玩家的一个属性,这样屏幕上就能显示出来。
remotes.Ability.OnServerEvent:Connect(function(player, name)
	if typeof(name) ~= "string" or not abilities[name] then
		return
	end
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = workspace:GetServerTimeNow()
	if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
		return
	end
	player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])
	abilities[name](player, character, root)
end)

-- 每位玩家每次重生都会获得一把剑
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

-- 训练假人的血条会随生命值变化。被击败后,
-- 它会在片刻后以全新的复制体在原地重新出现。
local function setupDummy(dummy)
	local humanoid = dummy.Humanoid
	humanoid.HealthChanged:Connect(function(health)
		dummy.HealthBar.Back.Fill.Size = UDim2.fromScale(health / humanoid.MaxHealth, 1)
	end)
	humanoid.Died:Connect(function()
		local spot = dummy:GetPivot()
		task.wait(0.5)
		dummy:Destroy()
		task.wait(Settings.DummyRespawn)
		local fresh = ServerStorage.Dummy:Clone()
		fresh:PivotTo(spot)
		fresh.Parent = workspace
		setupDummy(fresh)
	end)
end
setupDummy(workspace.Dummy)

Roblox 游戏:竞技场对战中的所有课

自己练习: Roblox 在线编辑器