Menu
CoddyTech

格挡

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

按 F 键格挡:在 Settings.BlockTime 秒内,你每次受到攻击时只会承受部分伤害,比例由 Settings.BlockMultiplier 决定。角色是否正在格挡是一个属性,名为 Blocking,所以任何内容都可以读取它。

因为每次攻击都会经过 hurt,所以只需编写一次规则:

if character:GetAttribute("Blocking") then
	amount *= Settings.BlockMultiplier
end

第 4 章中,机器人会攻击你,这几行代码到时也会保护你。

challenge icon

挑战

中等

按 F 进行格挡:短时间内,受到的伤害变为五分之一。

  1. 添加 abilities.Block(player, character):将角色的 Blocking 属性设为 true,并在 Settings.BlockTime 之后清除该属性,前提是角色仍然存在。
  2. 在 hurt 中,当角色处于 Blocking 状态时,将 amount 乘以 Settings.BlockMultiplier。
  3. 在 BattleClient 中,将 "Block" 绑定到 F,并将其添加到 shown。

按 “开始”,然后按 F:格挡标签会开始倒计时。然后点击 “检查”。

亲手试试

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 hurt(humanoid, amount)
	if humanoid.Health <= 0 then
		return
	end
	local character = humanoid.Parent
	humanoid:TakeDamage(amount)
end

-- 挥剑:先播放动画,再击中你前方范围内的所有人
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
			hurt(humanoid, Settings.SwordDamage)
		end
	end
end

-- 按名称列出各个技能的作用
local abilities = {}

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

-- 你周围的所有人都会被击中并击退
function abilities.Slam(player, character, root)
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local hit = {}
	for _, part in workspace:GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		local theirRoot = part.Parent:FindFirstChild("HumanoidRootPart")
		if humanoid and theirRoot and not hit[humanoid] then
			hit[humanoid] = true
			hurt(humanoid, Settings.SlamDamage)
			local away = (theirRoot.Position - root.Position) * Vector3.new(1, 0, 1)
			if away.Magnitude > 0 then
				away = away.Unit
			end
			theirRoot:ApplyImpulse((away * Settings.SlamKnockback + Vector3.new(0, 40, 0)) * theirRoot.AssemblyMass)
		end
	end
end

-- 第 12 课:在这里添加 Block

-- 客户端按名称请求使用技能。服务器会检查:技能是否存在、
-- 玩家是否还活着,以及技能是否已结束冷却。技能再次就绪的时间
-- 是玩家的一个属性,这样就能在玩家的屏幕上显示。
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 在线编辑器