Menu
CoddyTech

웨이브

Coddy의 Roblox 게임: 타워 디펜스 코스 레슨. 22개 중 5번째.

좀비 한 마리로는 게임이라고 할 수 없습니다. Settings.Waves에는 각 웨이브가 적 이름 목록으로 나열되어 있습니다:

Waves = {
	{ "Zombie", "Zombie", "Zombie" },
	{ "Zombie", "Runner" },
}

runWave(list)는 적을 하나씩 생성하며, 각 적 생성 간격은 Settings.SpawnGap초입니다. 그런 다음 workspace.Enemies가 비워질 때까지 기다리므로, 현재 웨이브가 끝난 뒤에만 다음 웨이브가 시작됩니다. 각 웨이브가 시작되기 전에 플레이어에게는 준비할 Settings.WaveBreak초가 주어집니다.

메인 루프는 웨이브를 순서대로 진행합니다: for i, list in Settings.Waves do.

challenge icon

챌린지

쉬움

적을 웨이브별로 보냅니다.

  1. runWave(list)를 작성합니다. 목록의 각 적을 생성하고, 각각 생성한 후 Settings.SpawnGap만큼 기다린 다음 enemies에 자식이 없어질 때까지 기다립니다.
  2. 단일 적 생성 코드를 for i, list in Settings.Waves do로 바꿉니다. Settings.WaveBreak만큼 기다린 다음 runWave(list)를 호출합니다.

‘플레이’를 누릅니다. 6초 후 좀비 5마리가 1초 간격으로 나타나고, 그 뒤에도 더 나타납니다. 그런 다음 ‘확인’을 누릅니다.

직접 해보기

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)
	print("Base: " .. 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(1)
	enemy:Destroy()
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

-- 레슨 5: Waves가 여기에 들어갑니다

-- 지금은 적 한 명만
spawnEnemy("Zombie")

Roblox 게임: 타워 디펜스의 모든 레슨

직접 연습해 보세요: Roblox 플레이그라운드