מכת קרקע
שיעור 9 מתוך 20 בקורס משחק Roblox: זירות קרב של Coddy.
יכולת שנייה, ב־E: חבטת קרקע שפוגעת בכל מי שסביבך ומעיפה אותם. workspace:GetPartBoundsInRadius(position, radius, params) מוצאת את החלקים שנמצאים בטווח מסוים, ו־ApplyImpulse על חלק השורש מעניקה לכל הגוף שלו דחיפה. דחף הוא שינוי במהירות כפול המסה, ולכן הכפלה ב־AssemblyMass מעיפה אסדות קלות וכבדות באותה צורה:
theirRoot:ApplyImpulse((away * Settings.SlamKnockback + Vector3.new(0, 40, 0)) * theirRoot.AssemblyMass)עכשיו יש במשחק שתי דרכים לפגוע במישהו, ודרכים נוספות בדרך. כל פגיעה עוברת דרך פונקציה אחת, hurt(humanoid, amount), כך שכלל לגבי נזק (חסימה, בשיעור הבא) נכתב פעם אחת.
אתגר
בינוני- כתבו את
hurt(humanoid, amount)מעלswing: החזירו פעולה אם ה-Humanoid כבר הובס, אחרת הפעילוTakeDamage(amount). ודאו ש-swingקוראת לה. - הוסיפו את
abilities.Slam: כל מי שנמצא בטווח שלSettings.SlamRadius(כל אחד פעם אחת, לא אתם) ושיש לו שורש, יספוג נזק באמצעותSettings.SlamDamageדרךhurt, ויידחף הרחק מכם בעוצמה שלSettings.SlamKnockbackו-40 כלפי מעלה. - ב-
BattleClient, קשרו את"Slam"למקש E והוסיפו אותו לתצוגת זמני ההמתנה.
לחצו על Play, ואז על Check.
נסו בעצמכם
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)
end
end
end
-- היכולות, לפי שם: מה כל אחת עושה
local abilities = {}
function abilities.Dash(player, character, root)
root.AssemblyLinearVelocity = root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)
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)
-- כל שחקן מקבל Kills בטבלת המובילים, וחרב בכל פעם שהוא מופיע מחדש
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
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)
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.