ランダムなタワー
Coddyの「Robloxゲーム:エンドレスタワー」コースのレッスン 4/21。
同じステージが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を積み重ねます。
何回か「プレイ」を押します。塔の形は毎回異なり、頂上には金色のパッドがあります。その後、「確認」を押します。
自分で試してみよう
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)
-- レッスン4: ここにランダムなタワーを配置する
-- モデルのコピーをタワーの上に配置し、そのコピーと新しい頂点の高さを返す
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()
Robloxゲーム:エンドレスタワーのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド