同时带出三只宠物
Coddy Roblox 游戏:宠物模拟器 课程的第 12 课,共 20 课。
宠物应该排成一排,同时最多只能有 Settings.MaxEquipped(3)只宠物同时在外面。只有当外面的宠物少于这个数量时,才会放出一只新宠物,其余宠物留在 Pets 文件夹中。
通过遍历文件夹来统计外面的宠物数量:
local count = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
count += 1
end
end要让它们排成一排,在放置时给外面的宠物依次编号为 1、2、3,再把编号转换成侧向偏移量:使用 (slot - 2) * 5,slot 1 会向左移动 5 studs,slot 3 会向右移动 5 studs。
挑战
中等让最多三只宠物排成一排跟在你身后。
- 编写
equippedCount(player),用于统计玩家已装备的宠物数量。 - 使用
givePet(player, name, equippedCount(player) < Settings.MaxEquipped)来孵化宠物。 - 在 Heartbeat 中,为跟随在外的宠物依次将
slot计为 1、2、3,并把每只宠物放在root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)的位置。
按“开始”并孵化四只宠物:三只宠物会并排跟在你身后。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local pileSpot = {} -- 每个硬币堆对应它所在的位置
local lastHit = {} -- 每个玩家对应上次攻击的时间
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 按名称整理 Settings.Pets
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- 一只宠物:玩家的 Pets 文件夹中的一个 StringValue,保存宠物名称,
-- 并将稀有度和力量保存为属性
local function givePet(player, name, equipped)
local info = petInfo[name]
local pet = Instance.new("StringValue")
pet.Name = "Pet"
pet.Value = name
pet:SetAttribute("Rarity", info.Rarity)
pet:SetAttribute("Power", info.Power)
pet:SetAttribute("Equipped", equipped)
pet.Parent = player.Pets
return pet
end
-- 第 12 课:Three Pets Out 的代码写在这里
-- 每个玩家的排行榜上都有 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
-- 客户端请求敲击金币堆。绝不能信任客户端:任何人都能
-- 从任何地方,以任意速度,向这个 remote 发送任意内容。
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)
-- 按权重随机抽取:每只宠物的 Chance(以 100 为总值)决定它占的比例
local function rollPet()
local roll = math.random() * 100
for _, info in Settings.Pets do
roll -= info.Chance
if roll <= 0 then
return info.Name
end
end
return Settings.Pets[1].Name
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 = rollPet()
-- 立即出场,跟随主人
givePet(player, name, true)
print(player.Name .. " hatched a " .. name)
end)
-- 出场的宠物会跟随主人:每只宠物在场景中都有一个部件,
-- 每一帧都保持在玩家身后
local petModels = {} -- 每只宠物 -> 它在游戏世界中的部件
RunService.Heartbeat:Connect(function()
for _, player in Players:GetPlayers() do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
local pets = player:FindFirstChild("Pets")
for _, pet in pets and pets:GetChildren() or {} do
local model = petModels[pet]
if pet:GetAttribute("Equipped") and root then
if not model then
model = ServerStorage.PetModels[pet.Value]:Clone()
model.Parent = petParts
petModels[pet] = model
end
model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4)
end
end
end
end)
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器