タワーを作ろう
Coddyの「Robloxゲーム:タワーディフェンス」コースのレッスン 6/18。
次はプレイヤー側です。DefenseClientはHUD内のLocalScriptです。HUDは、Robloxが各プレイヤーの画面にコピーするScreenGuiです。HUDのShopフレームには、タワーごとにTextButtonがあり、タワーの名前が付けられています。ボタンのActivatedイベントは、クリックまたはタップすると発生します。
button.Activated:Connect(function()
selected = button.Name
end)その後、灰色のパッドのいずれかをクリックすると、選択したタワーがそこに建てられます。クライアントはマウスからクリックを読み取ります(me:GetMouse()、そのButton1DownイベントとTarget、つまりポインターの下にあるパーツ)。しかし、クライアント自身では何も建てられません。タワーはサーバー上にあるからです。クライアントはRemoteEventのRemotes.Placeを通じて要求します。
-- クライアント
remotes.Place:FireServer(pad, "Archer")
-- サーバー: Roblox は最初に player を追加します
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
end)タワーモデルはServerStorage.Towers内で待機しており、サーバーがそのうちの1つをパッド上に複製します。
チャレンジ
中級DefenseClient内で:
Players、Settings(WaitForChildを使用)、local remotes = game.ReplicatedStorage:WaitForChild("Remotes")、local me = Players.LocalPlayer、local hud = script.Parentを取得します。hud.Shop内の各TextButtonについて、テキストをタワーの名前と価格(Archer $60)に設定し、Activated時にそれをselectedに記憶します。- マウスの
Button1Down時に、ターゲットがworkspace.Pads内にあり、タワーが選択されている場合は、パッドとタワーの名前を指定してremotes.Placeを発火します。
DefenseServer内で: local remotesとlocal towers = workspace.Towersを追加し、placeTower(pad, kind)(クローンを作成し、パッドのCFrameをパッドの高さの半分に1を足した分だけ上げた位置にPivotToし、towersを親に設定)を記述して、remotes.Place.OnServerEventをこれに接続します。
Playを押し、タワーを選んでパッドをクリックします。その後、Checkを押します。
自分で試してみよう
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local enemies = workspace.Enemies
print("The base can take " .. Settings.BaseHealth .. " hits")
-- 順番に並んだウェイポイント(名前は 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
-- workspace の属性として設定された拠点の体力。すべてのクライアントが読み取れる
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
-- 各プレイヤーのリーダーボードに Money を追加する
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = Settings.StartMoney
money.Parent = leaderstats
end)
-- 敵が拠点に到達した
local function damageBase(amount)
local health = math.max(0, workspace:GetAttribute("BaseHealth") - amount)
workspace:SetAttribute("BaseHealth", health)
end
-- 敵を経路に沿って移動させる。MoveTo は 8 秒後に移動を諦め、そのことを通知する
-- (MoveToFinished が false を返す)。そのため、遅い敵には到着するまで再度移動を指示する。
local function walkPath(enemy)
local humanoid = enemy.Humanoid
for i = 2, #waypoints do
repeat
humanoid:MoveTo(waypoints[i].Position)
until humanoid.MoveToFinished:Wait() or not enemy.Parent
if not enemy.Parent then
return
end
end
-- 拠点に到達した
damageBase(Settings.Enemies[enemy.Name].Damage)
enemy:Destroy()
end
-- 門に現れ、経路を進む新しい敵
local function spawnEnemy(kind)
local enemy = ServerStorage.Enemies[kind]:Clone()
-- 最初のウェイポイントのすぐ上に配置する
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
local info = Settings.Enemies[kind]
enemy.Humanoid.MaxHealth = info.Health
enemy.Humanoid.Health = info.Health
enemy.Humanoid.WalkSpeed = info.Speed
enemy.Parent = enemies
task.spawn(walkPath, enemy)
return enemy
end
-- 1 つのウェーブ:敵を 1 体ずつ出し、すべていなくなるまで待つ
local function runWave(list)
for _, kind in list do
spawnEnemy(kind)
task.wait(Settings.SpawnGap)
end
while #enemies:GetChildren() > 0 do
task.wait(0.2)
end
end
-- 各ウェーブを、休憩を挟んで開始する
for i, list in Settings.Waves do
task.wait(Settings.WaveBreak)
runWave(list)
end
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。