Pets Escavam Mais Rápido
Lição 12 de 18 do curso Jogo Roblox: Simulador de Pets da Coddy.
Em um simulador de animais de estimação, os pets cavam: cada pet que está fora adiciona sua Power a cada golpe. Uma raposa (poder 4) faz um golpe causar 5 em vez de 1.
Some all power of the pets that are out, and subtract 1 plus that from the pile's health. Pets that are put away do not help.
Desafio
Médio- Escreva
petPower(player): a soma do Power dos pets do jogador que estão em campo. - Em
hitPile, subtraia1 + petPower(player)da vida em vez de 1.
As pilhas desta lição têm 8 de vida. Pressione Play e, em seguida, Check.
Experimente você mesmo
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 = {} -- cada pilha -> o local onde ela fica
local lastHit = {} -- cada jogador -> quando deu o último golpe
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- Settings.Pets por nome
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- Um pet: um StringValue na pasta Pets do jogador que armazena seu nome, com
-- sua raridade e poder como atributos
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
-- quantos pets um jogador tem em campo
local function equippedCount(player)
local count = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
count += 1
end
end
return count
end
-- Todo jogador recebe Coins no placar
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
-- os pets que ele possui
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- Uma pilha de moedas em um local: uma cópia de ServerStorage.CoinPile com sua vida
-- e seu valor como atributos
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
-- Um golpe reduz em 1 a vida de uma pilha;
-- ao chegar a 0, a pilha se quebra e concede seu valor
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()
-- uma nova pilha no mesmo lugar, alguns segundos depois
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- Uma pilha em cada local do Meadow
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- O cliente pede para atingir uma pilha. Nunca confie no cliente: qualquer pessoa pode disparar
-- este remote com qualquer dado, de qualquer lugar, tão rápido quanto quiser.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- uma pilha de verdade
if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
return
end
-- ao alcance
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
-- não rápido demais
local now = time()
if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
return
end
lastHit[player] = now
hitPile(player, pile)
end)
-- Um sorteio ponderado: a Chance de cada pet (de um total de 100) é sua parte da faixa
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
-- Incubação: o prompt do ovo, pago com moedas
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()
-- já fica em uso, se houver espaço
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- Os pets em uso seguem seu dono: cada um tem uma peça no mundo, mantida em uma
-- fileira atrás do jogador a cada quadro
local petModels = {} -- cada pet -> sua peça no mundo
RunService.Heartbeat:Connect(function()
for _, player in Players:GetPlayers() do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
local pets = player:FindFirstChild("Pets")
local slot = 0
for _, pet in pets and pets:GetChildren() or {} do
local model = petModels[pet]
if pet:GetAttribute("Equipped") and root then
slot += 1
if not model then
model = ServerStorage.PetModels[pet.Value]:Clone()
model.Parent = petParts
petModels[pet] = model
end
model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
elseif model then
model:Destroy()
petModels[pet] = nil
end
end
end
end)
Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.
Todas as lições de Jogo Roblox: Simulador de Pets
3Ovos e Pets
Chocar um Ovo