Menu
CoddyTech

지면 강타

Coddy의 Roblox 게임: 배틀그라운드 코스 레슨. 25개 중 10번째.

두 번째 능력은 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와 출력문 대신 이 함수를 호출하세요.
  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 플레이그라운드