기지에 도달하기
Coddy의 Roblox 게임: 타워 디펜스 코스 레슨. 22개 중 4번째.
경로 끝에 도달한 적은 기지에 피해를 줍니다. 기지의 체력은 서버에 있어야 하지만 모든 플레이어의 화면에 표시되므로, 작업 공간의 속성으로 저장하세요. 서버에서 설정한 속성은 모든 클라이언트에서 볼 수 있습니다.
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
print(workspace:GetAttribute("BaseHealth")) --> 20damageBase(amount)는 체력을 0 아래로 내려가지 않게(math.max) 줄이고 출력하므로, HUD에 표시될 때까지 출력 창에서 기지 체력을 확인할 수 있습니다. 마지막 웨이포인트를 지나면 적은 이 함수를 호출하고 Destroy로 게임에서 나갑니다. 그렇지 않으면 웨이브가 끝나지 않습니다.
챌린지
쉬움적이 기지에 도달하면 기지에 피해를 줍니다.
- 웨이포인트를 정렬한 후,
BaseHealth워크스페이스 특성을Settings.BaseHealth로 설정합니다. damageBase(amount)를 작성합니다.BaseHealth를amount만큼 낮추되, 0보다 낮아지지 않게 하고print("Base: " .. health)를 실행합니다.walkPath의 끝에서damageBase(1)을 호출하고 적을 제거합니다.
‘플레이’를 누릅니다. Zombie가 기지로 걸어와 사라지고, 출력 창에 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")
-- 순서대로 정렬된 웨이포인트(이름은 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()
-- 첫 번째 웨이포인트 바로 위에 배치됨
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
enemy.Parent = enemies
task.spawn(walkPath, enemy)
return enemy
end
-- 일단은 적 한 명만
spawnEnemy("Zombie")
Roblox 게임: 타워 디펜스의 모든 레슨
직접 연습해 보세요: Roblox 플레이그라운드