ランダムなタワー
Coddyの「Robloxゲーム:エンドレスタワー」コースのレッスン 3/18。
同じステージが5回続く塔では面白くありません。ServerStorage.Stagesには、Steps、KillBricks、Conveyor、VanishingSteps、Bounce、Spinner、Lasersの7種類があります。各階にランダムに1つ選びましょう。
local all = stages:GetChildren()
local stage = all[math.random(1, #all)]math.random(1, #all)は、ステージの数までの1からの整数なので、どのステージも同じ確率で選ばれます。ほとんどのステージはまだ何もしません。スピナー、ベルト、レーザーが動き出すのは次の章です。
最後のステージの上にはServerStorage.Finishを置きます。これは登りの終点となる金色のパッドです。stackはすでにどんなModelも持ち上げるので、Finishも持ち上げます。
チャレンジ
簡単pickStage()を作成します。stagesの子要素をランダムに1つ返します。buildTowerで、stages.Stepsの代わりにpickStage()を積み重ねます。- ループの後、塔の最上部に
ServerStorage.Finishを積み重ねます。
Playを数回押してみましょう。塔は毎回異なるものになります。その後、Checkを押します。
自分で試してみよう
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local stages = ServerStorage.Stages
local tower = nil
print("Each tower has " .. Settings.StageCount .. " stages")
-- すべてのプレイヤーのリーダーボードに Wins を追加する
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)
-- モデルのコピーをタワーの頂上に配置し、そのコピーと新しい頂上の高さを返す
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
-- Steps ステージを積み重ねてタワーを作る
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(stages.Steps, top)
top = newTop
end
end
buildTower()
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。