Menu
CoddyTech

חיות מחמד חופרות מהר יותר

שיעור 12 מתוך 18 בקורס משחק Roblox: סימולטור חיות מחמד של Coddy.

בסימולטור חיות מחמד, חיות המחמד חופרות: כל חיית מחמד שנמצאת בחוץ מוסיפה את Power שלה לכל פגיעה. שועל (עוצמה 4) גורם לפגיעה להפחית 5 במקום 1.

חברו את העוצמה של חיות המחמד שנמצאות בחוץ, והפחיתו מהבריאות של הערימה 1 ועוד את הסכום הזה. חיות מחמד שהוחזרו לא עוזרות.

challenge icon

אתגר

בינוני
  1. כתבו את petPower(player): סכום ה־Power של חיות המחמד של השחקן שנמצאות בחוץ.
  2. בתוך hitPile, הפחיתו מהבריאות 1 + petPower(player) במקום 1.

לערימות בשיעור הזה יש 8 נקודות בריאות. לחצו על Play, ואז על Check.

נסו בעצמכם

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

-- חיית מחמד: StringValue בתיקיית Pets של השחקן, שמכיל את שמה, עם
-- רמת הנדירות והכוח שלה כמאפיינים
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

-- כל שחקן מקבל 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)
			elseif model then
				model:Destroy()
				petModels[pet] = nil
			end
		end
	end
end)
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה משחק Roblox: סימולטור חיות מחמד