抽取稀有度
Coddy Roblox 游戏:宠物模拟器 课程的第 10 课,共 20 课。
孵化出的宠物不一定总是猫。Settings.Pets 列出了每种宠物,它们的 Chance 是 100 分制的概率(猫 45、狗 30、兔子 15、狐狸 8、龙 2),还包含稀有度和力量。
加权抽取:先从 0 到 100 中选一个数字,然后沿着列表依次减去每种宠物的概率。减到 0 或更低时,对应的宠物就会被选中。因此,概率为 45 的宠物平均每 100 次会被选中 45 次:
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:以 Name 为键的Settings.Pets。 - 在
givePet中,从petInfo[name]设置Rarity和Power属性。 - 编写加权抽取函数
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")
-- 宠物:玩家的 Pets 文件夹中的一个 StringValue,保存着宠物的名字
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 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器