基地に到達する
Coddyの「Robloxゲーム:タワーディフェンス」コースのレッスン 4/22。
経路の終点に到達した敵は、基地にダメージを与えます。基地の体力はサーバー側で管理しますが、すべてのプレイヤーの画面に表示するため、workspaceの属性にしましょう。サーバーで設定した属性は、すべてのクライアントから見えます。
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
print(workspace:GetAttribute("BaseHealth")) --> 20damageBase(amount)は体力を0未満にならないように減らし(math.max)、その値を出力します。HUDに表示されるまで、Outputで基地の体力を確認できます。最後のウェイポイントの後、敵はこの関数を呼び出し、Destroyでゲームから退出します。そうしないと、ウェーブがいつまでも終わりません。
チャレンジ
簡単敵が基地に到達したら、基地にダメージを与えます。
- ウェイポイントを並べ替えた後、ワークスペースの属性
BaseHealthをSettings.BaseHealthに設定します。 damageBase(amount)を記述します。BaseHealthをamountだけ減らし、0未満にならないようにして、print("Base: " .. health)を実行します。walkPathの最後でdamageBase(1)を呼び出し、敵を破棄します。
「プレイ」を押します。Zombieが基地に歩いて到達して消え、Outputに Base: 19 と表示されます。その後、「確認」を押します。
自分で試してみよう
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")
-- 順番に並べたwaypoints(名前は1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
-- すべてのプレイヤーに、リーダーボード上の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)
-- Lesson 4: Reach the Baseはここに入る
-- 敵を経路に沿って歩かせる。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
end
-- ゲートに現れ、経路を歩く新しい敵
local function spawnEnemy(kind)
local enemy = ServerStorage.Enemies[kind]:Clone()
-- 最初のwaypointのすぐ上に配置する
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
enemy.Parent = enemies
task.spawn(walkPath, enemy)
return enemy
end
-- 今のところ、敵は1体だけ
spawnEnemy("Zombie")
Robloxゲーム:タワーディフェンスのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド