ペットはより速く掘る
Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 12/18。
ペットシミュレーターでは、ペットが採掘します。外に出ているペットはそれぞれ、自分のPowerを各攻撃に加えます。Fox(パワー4)がいると、1ではなく5のダメージを与えます。
外に出ているペットのパワーを合計し、その値に1を加えて、山の体力から差し引きます。しまってあるペットは役に立ちません。
チャレンジ
中級petPower(player)を記述します。これは、プレイヤーが出しているペットのPowerの合計です。hitPileでは、体力から1ではなく1 + petPower(player)を差し引きます。
このレッスンの山の体力は8です。プレイを押してから、チェックを押してください。
自分で試してみよう
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 = {} -- 各 pile -> その pile が置かれている spot
local lastHit = {} -- 各 player -> 最後に攻撃した時刻
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 名前をキーにした Settings.Pets
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- pet:player の 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
-- player が出している pet の数
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
-- すべての player にリーダーボード上の 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
-- その player が所有する pet
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- spot 上のコインの山: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 回攻撃すると pile の体力が 1 減る。
-- 体力が 0 になると pile が壊れ、その価値に相当するコインが支払われる
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 の各スポットに山を1つずつ配置する
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)
-- 重み付き抽選:各ペットの 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()
-- 枠に空きがあれば、すぐに連れ出す
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- 連れ出したペットは飼い主についていく。それぞれにワールド内のパーツがあり、毎フレーム
-- プレイヤーの後ろに一列に並ぶ
local petModels = {} -- 各pet -> ワールド内で対応するパーツ
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)
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。