Menu
CoddyTech

Lave montante

Leçon 10 sur 18 du cours Jeu Roblox : tour sans fin de Coddy.

Attendre en bas ne devrait pas être sans danger. Une nappe de lave démarre sous la tour et, après un court délai, monte régulièrement jusqu’à se trouver à 2 studs du sommet, si bien que tout le monde doit continuer à grimper.

Un tween peut attendre avant de démarrer : le sixième argument de TweenInfo.new est un délai en secondes. Pour une ascension régulière, la durée du tween correspond à la distance divisée par la vitesse :

local seconds = top / Settings.LavaSpeed
local info = TweenInfo.new(seconds, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, Settings.LavaDelay)

La lave n’est pas solide (CanCollide false), donc personne ne peut se tenir dessus, et son événement Touched utilise killOnTouch, la même fonction que les briques mortelles. Elle démarre à une hauteur de -2 et monte jusqu’à top - 2, soit exactement top studs.

challenge icon

Défi

Moyen
  1. Près du début : local lava = nil. Fais en sorte que buildTower renvoie top.
  2. Écris startLava(top) : crée une Part ancrée nommée Lava, avec CanCollide désactivé, de dimensions 30 × 2 × 30, à Vector3.new(0, -2, 0), de couleur orange et de matériau Neon (Color3.fromRGB(255, 90, 20)), dans workspace, et connecte Touched à killOnTouch. Anime sa Position jusqu’à Vector3.new(0, top - 2, 0) sur une durée de top / Settings.LavaSpeed secondes, avec une interpolation Linear, après un délai de Settings.LavaDelay.
  3. À la fin : local top = buildTower(), puis startLava(top).

La lave de cette leçon va vite, donc la vérification est rapide. Appuie sur Play, puis sur Check.

Essayez vous-même

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Settings = require(game.ReplicatedStorage.Settings)

local stages = ServerStorage.Stages
local tower = nil

print("Each tower has " .. Settings.StageCount .. " stages")

-- Chaque joueur reçoit Wins dans le classement
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- Tout élément portant le tag Kill met fin à une vie
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Une marche escamotable s’efface peu après qu’on a atterri dessus, puis réapparaît
local function makeVanish(part)
	part.Touched:Connect(function(hit)
		if part:GetAttribute("Gone") or not hit.Parent:FindFirstChild("Humanoid") then
			return
		end
		part:SetAttribute("Gone", true)
		task.wait(Settings.VanishDelay)
		part.Transparency = 0.8
		part.CanCollide = false
		task.wait(Settings.VanishTime)
		part.Transparency = 0
		part.CanCollide = true
		part:SetAttribute("Gone", nil)
	end)
end

-- Un laser glisse jusqu’au décalage Slide, puis revient, indéfiniment
local function makeSlide(part)
	local info = TweenInfo.new(Settings.SlideTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
	local goal = { Position = part.Position + part:GetAttribute("Slide") }
	TweenService:Create(part, info, goal):Play()
end

-- Un tremplin propulse dans les airs quiconque atterrit dessus
local function bounce(hit)
	local root = hit.Parent:FindFirstChild("HumanoidRootPart")
	if root then
		root.AssemblyLinearVelocity = Vector3.new(0, Settings.BouncePower, 0)
	end
end

-- Les éléments Spinner tournent un peu à chaque image
RunService.Heartbeat:Connect(function(dt)
	for _, spinner in CollectionService:GetTagged("Spinner") do
		if spinner:IsDescendantOf(workspace) then
			spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0))
		end
	end
end)

-- Active chaque obstacle d’une étape selon ses tags
local function setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		end
		if part:HasTag("Vanish") then
			makeVanish(part)
		end
		if part:HasTag("Conveyor") then
			part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed
		end
		if part:HasTag("Slider") then
			makeSlide(part)
		end
		if part:HasTag("Bounce") then
			part.Touched:Connect(bounce)
		end
	end
end

-- Une étape choisie au hasard
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- Place une copie d’un modèle au sommet de la tour ; renvoie la copie et la nouvelle hauteur du sommet
local function stack(template, bottom)
	local copy = template:Clone()
	local box, size = copy:GetBoundingBox()
	local lift = bottom - (box.Position.Y - size.Y / 2)
	copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
	copy.Parent = tower
	return copy, bottom + size.Y
end

-- Construit une nouvelle tour aléatoire, de bas en haut
local function buildTower()
	tower = Instance.new("Model")
	tower.Name = "Tower"
	tower.Parent = workspace
	local top = 0
	for i = 1, Settings.StageCount do
		local stage, newTop = stack(pickStage(), top)
		top = newTop
		setupStage(stage)
	end
	stack(ServerStorage.Finish, top)
end

buildTower()
quiz iconTestez-vous

Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.

Toutes les leçons de Jeu Roblox : tour sans fin