احتمالات الندرة
الدرس 10 من 18 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.
لا ينبغي أن تعطي البيضة قطة دائمًا. تسرد Settings.Pets كل حيوان أليف مع Chance من 100 (Cat 45، Dog 30، Bunny 15، Fox 8، Dragon 2)، وندرة وقوة.
سحب موزون: اختر رقمًا من 0 إلى 100، ثم مرّ على القائمة واطرح احتمال كل حيوان أليف منه. يفوز الحيوان الأليف الذي يخفض الرقم إلى 0 أو أقل، لذا يفوز حيوان أليف احتماله 45 في 45 سحبًا من أصل 100 في المتوسط:
local roll = math.random() * 100
for _, info in Settings.Pets do
roll -= info.Chance
if roll <= 0 then
return info.Name
end
endيحمل الحيوان الأليف الذي يفقس أيضًا Rarity وPower كسمات. ويتيح جدول الحيوانات الأليفة حسب الاسم (petInfo["Fox"]) العثور عليها بسرعة.
التحدي
متوسط- أنشئ
petInfo، مع جعلSettings.Petsمفهرسًا حسب Name. - في
givePet، عيّن سمتيRarityوPowerللحيوان الأليف منpetInfo. - اكتب
rollPet()، وهي عملية السحب الموزون، واجعل الفقس يستخدمrollPet()بدلًا من قطة (تعرض print اسم ما فقس).
اضغط على تشغيل وفقّس بعض الحيوانات. ثم تحقّق: تغيّر عمليات التحقق الاحتمالات للتأكد من أن السحب يتبعها.
جرّب بنفسك
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local pileSpot = {} -- كل كومة -> الموضع الذي تقف عليه
local lastHit = {} -- كل لاعب -> وقت آخر ضربة وجّهها
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- حيوان أليف: قيمة من نوع StringValue في مجلد Pets الخاص باللاعب، تحمل اسمه
local function givePet(player, name)
local pet = Instance.new("StringValue")
pet.Name = "Pet"
pet.Value = name
pet.Parent = player.Pets
return pet
end
-- يحصل كل لاعب على Coins في لوحة المتصدرين
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
-- الحيوانات الأليفة التي يملكونها
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- كومة عملات في موضع: نسخة من ServerStorage.CoinPile مع صحتها
-- وقيمتها بوصفهما سمتين
local function spawnPile(spot)
local pile = ServerStorage.CoinPile:Clone()
pile.Position = spot.Position
pile:SetAttribute("MaxHealth", Settings.PileHealth)
pile:SetAttribute("Health", Settings.PileHealth)
pile:SetAttribute("Value", Settings.PileValue)
pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
pileSpot[pile] = spot
pile.Parent = piles
return pile
end
-- تُنقص الضربة 1 من صحة الكومة؛
-- عند 0، تتحطم الكومة وتمنح قيمتها
local function hitPile(player, pile)
local health = pile:GetAttribute("Health") - 1
if health > 0 then
pile:SetAttribute("Health", health)
pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
return
end
player.leaderstats.Coins.Value += pile:GetAttribute("Value")
local spot = pileSpot[pile]
pileSpot[pile] = nil
pile:Destroy()
-- كومة جديدة في الموضع نفسه بعد بضع ثوانٍ
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- كومة في كل موضع في Meadow
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- يطلب العميل ضرب كومة. لا تثق بالعميل أبدًا: يمكن لأي شخص إرسال
-- هذا الحدث البعيد بأي شيء، ومن أي مكان، وبالسرعة التي يريدونها.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- كومة حقيقية
if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
return
end
-- ضمن نطاق الوصول
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
return
end
-- ليس بسرعة كبيرة
local now = time()
if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
return
end
lastHit[player] = now
hitPile(player, pile)
end)
-- الفقس: موجه البيضة، ويُدفع ثمنه بالعملات المعدنية
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if coins.Value < Settings.EggPrice then
return
end
coins.Value -= Settings.EggPrice
local name = "Cat"
givePet(player, name)
print(player.Name .. " hatched a " .. name)
end)
يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.
جميع دروس لعبة Roblox: محاكي الحيوانات الأليفة
3البيض والحيوانات الأليفة
فقّس بيضة