発射
Coddyの「Robloxゲーム:タワーディフェンス」コースのレッスン 8/18。
敵に向き合うことは、仕事の半分です。shootはHumanoid:TakeDamageで対象にダメージを与え、頭から対象まで短い光の閃光を描きます。これは細いネオンパーツで、10分の1秒後にDebrisが削除します。
Debris:AddItem(ray, 0.1) -- 0.1秒後に破棄されるショットの後、タワーは再び撃てるようになるまでCooldownの時間だけ待ちます。これによってArcherは速く、Cannonは遅くなります。
Humanoidの体力が0になると、Diedイベントが発生します。倒された敵はすぐにゲームから退出する必要があるため、spawnEnemyはDiedを敵の破棄に接続します。するとwalkPathは敵がいなくなったことを認識し、停止します。
チャレンジ
中級Debrisサービスを取得します。beam(from, to)を記述します。固定されたネオンのパーツを作成し、サイズを0.2×0.2、長さを距離に設定し、CFrame.lookAt((from + to) / 2, to)で設定して、0.1秒後にDebrisで削除します。shoot(tower, target, info)を記述します。ターゲットのHumanoidに対してTakeDamage(info.Damage)を実行し、その後、頭からターゲットまでビームを出します。runTowerでは、向きを変えてから射撃し、0.1秒ではなくinfo.Cooldownの時間だけ待機します(ターゲットがいない場合は0.1秒のままにします)。spawnEnemyでは、Humanoidが死亡したときに敵を破棄します。
Playを押してから、Checkを押します。
自分で試してみよう
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local enemies = workspace.Enemies
local towers = workspace.Towers
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
-- 指定地点から範囲内にいる最も近い生存中の敵。いなければ nil
local function nearestEnemy(position, range)
local best, bestDistance = nil, range
for _, enemy in enemies:GetChildren() do
local root = enemy:FindFirstChild("HumanoidRootPart")
if root and enemy.Humanoid.Health > 0 then
local distance = (root.Position - position).Magnitude
if distance <= bestDistance then
best, bestDistance = enemy, distance
end
end
end
return best
end
-- タワーの動作:範囲内で最も近い敵に狙いを定める
local function runTower(tower)
local info = Settings.Towers[tower.Name]
while tower.Parent do
local target = nearestEnemy(tower.Head.Position, info.Range)
if target then
tower.Head.CFrame = CFrame.lookAt(tower.Head.Position, target.HumanoidRootPart.Position)
task.wait(0.1)
else
task.wait(0.1)
end
end
end
-- パッドの上にタワーを建てる
local function placeTower(pad, kind)
local tower = ServerStorage.Towers[kind]:Clone()
tower:PivotTo(pad.CFrame + Vector3.new(0, pad.Size.Y / 2 + 1, 0))
tower.Parent = towers
task.spawn(runTower, tower)
end
-- クライアントがパッドへのタワーの設置を要求する
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
placeTower(pad, kind)
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
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。