Menu
CoddyTech

宠物挖得更快

Coddy Roblox 游戏:宠物模拟器 课程的第 13 课,共 20 课。

在宠物模拟器中,宠物会挖掘:每只出战的宠物都会为每次攻击增加 Power。一只狐狸(力量为 4)会让一次攻击造成 5 点伤害,而不是 1 点。

把出战宠物的力量加起来,然后从矿堆的生命值中减去这个总和加 1。收起来的宠物不会提供帮助。

challenge icon

挑战

中等

让每只已出战的宠物都和你一起挖掘。

  1. 编写 petPower(player):计算玩家已装备宠物的 Power 属性之和。
  2. 在 hitPile 中,从生命值中减去 1 + petPower(player),而不是 1。

按“开始”,孵化一只宠物并攻击一堆金币:它上面的数字会减少 2 或更多。然后按“检查”。

亲手试试

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

-- 玩家放出了多少只宠物
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

-- 第 13 课:宠物挖得更快的代码写在这里

-- 每个玩家的排行榜上都有 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()
	-- 如果还有空位,孵化后立即让宠物出场
	local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
	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")
		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)
			end
		end
	end
end)

Roblox 游戏:宠物模拟器中的所有课

自己练习: Roblox 在线编辑器