Menu
CoddyTech

连击

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

连续挥击 3 次,每次挥击都在上一次挥击后的 Settings.ComboWindow 时间内,就会形成一套连击:伤害依次为 10、10,然后是一次猛冲,造成 25 点伤害(Settings.ComboDamage)。暂停后,连击会重新开始。

服务器会为每位玩家记录连续挥击的次数,以及上一次挥击的时间:

local c = combo[player]
if c and now - c.last <= Settings.ComboWindow then
	c.count = c.count % 3 + 1
else
	c = { count = 1 }
	combo[player] = c
end
c.last = now

c.count % 3 + 1 的结果依次为 1、2、3、1、2、3:猛冲之后,新的一套连击就会开始。第三次攻击会播放 Roblox 的猛冲动画 rbxassetid://522638767。

challenge icon

挑战

中等

快速挥砍三次即可打出连击:10、10,然后一次造成 25 伤害的突刺。

  1. 添加 local combo = {} 和 lunge Animation,ID 为 rbxassetid://522638767。
  2. 在 swing 中,冷却检查通过后,按照代码所示记录挥砍次数,最后设置为 c.last = now。
  3. 当 c.count == 3 时播放突刺动画,否则播放斩击动画;使用 Settings.ComboDamage[c.count] 造成伤害。

按下“开始”,快速挥砍三次:第三次会使出突刺。然后按“检查”。

亲手试试

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
	-- 格挡会抵消大部分伤害
	if character:GetAttribute("Blocking") then
		amount *= Settings.BlockMultiplier
	end
	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
	-- 第 13 课:在这里添加连击
	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 = {}

-- 玩家的设备负责移动自己的角色,所以冲刺也在设备上执行
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

function abilities.Block(player, character)
	character:SetAttribute("Blocking", true)
	task.delay(Settings.BlockTime, function()
		if character.Parent then
			character:SetAttribute("Blocking", nil)
		end
	end)
end

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