احتمالات الندرة
الدرس 10 من 20 في دورة لعبة 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"]) العثور عليها بسرعة.
التحدي
متوسطحدّد الحيوان الأليف الذي يفقس، بحسب قيمة Chance لكل حيوان أليف.
- أنشئ
petInfo: اجعلSettings.Petsمفهرسة حسب Name. - في
givePet، عيّن السمتينRarityوPowerمنpetInfo[name]. - اكتب
rollPet()، وهي عملية اختيار موزونة، واجعل الفقس يستخدمrollPet()بدلًا من Cat؛ وتعرض الطباعة اسم ما فقس.
اضغط على «العب» وافقس عدة حيوانات: ستعرض نافذة Output أسماء حيوانات أليفة مختلفة من حين لآخر. ثم اضغط على «تحقق».
جرّب بنفسك
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)
-- الدرس 10: تُضاف هنا آلية تحديد الندرة عشوائيًا
-- الفقس: رسالة التفاعل الخاصة بالبيضة، ويُدفع ثمنها بالعملات
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البيض والحيوانات الأليفة
فقّس بيضةتدرّب بنفسك: Playground لـ Roblox