随机灾难
Coddy Roblox 游戏:灾难生存 课程的第 14 课,共 20 课。
如果你已经知道接下来会发生什么,游戏就会变得乏味。每一轮都应该随机选择一种灾难,除非 Settings.Disaster 指定了一种灾难,这在测试新灾难时很方便。
可以用字符串形式的名称查找表中的函数:Disasters["Meteors"] 就是 Disasters.Meteors,所以 Disasters[name](map) 会触发 name 所对应的灾难。如果表中没有这个名称,就会得到 nil,而调用 nil 会导致错误,所以在信任这个设置之前,先检查 Disasters[Settings.Disaster]。
要随机选择,可以列出表中的所有键,再选取一个位置。for name in Disasters do 会按不固定的顺序遍历这些键,因此要先对列表排序:这样,同一个随机数总会选中同一种灾难,让游戏更容易测试。
local names = {}
for name in Disasters do
table.insert(names, name)
end
table.sort(names)
local pick = names[math.random(1, #names)]玩家应该知道是什么击中了他们,所以倒计时会显示灾难名称:Flood! Time left: 15。
挑战
中等每轮随机选择一种灾难。
- 编写
pickDisaster():如果Disasters[Settings.Disaster]存在,就返回Settings.Disaster;否则,从表的键排序后得到的列表中随机选择一个名称。 - 在每轮中:
local disaster = pickDisaster(),然后执行Disasters[disaster](map)。 - 在倒计时中显示
disaster .. "! Time left: " .. seconds。
按几轮“开始”:状态板会显示每种灾难的名称,例如 Flood! Time left: 15。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Settings = require(game.ReplicatedStorage.Settings)
local lobbySpawn = workspace.LobbySpawn
print("Rounds last " .. Settings.RoundLength .. " seconds")
-- 每位玩家的排行榜上都有 Wins
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
end)
-- 大厅里的状态板
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard
local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui
local function setStatus(text)
status.Text = text
end
local function teleport(player, target)
local character = player.Character
if character then
character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
end
end
local function defeat(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- 按名称列出的灾难
local Disasters = {}
function Disasters.Flood(map)
local base = map.Base
local water = Instance.new("Part")
water.Name = "Water"
water.Anchored = true
water.CanCollide = false
water.Transparency = 0.3
water.Color = Color3.fromRGB(40, 120, 255)
water.Size = Vector3.new(base.Size.X + 10, 1, base.Size.Z + 10)
water.Position = base.Position - Vector3.new(0, 2, 0)
water.Parent = map
water.Touched:Connect(defeat)
local rise = TweenInfo.new(Settings.RoundLength * 0.8, Enum.EasingStyle.Linear)
TweenService:Create(water, rise, { Position = water.Position + Vector3.new(0, 8, 0) }):Play()
end
function Disasters.Meteors(map)
local base = map.Base
task.spawn(function()
while map.Parent do
local meteor = Instance.new("Part")
meteor.Name = "Meteor"
meteor.Shape = Enum.PartType.Ball
meteor.Size = Vector3.new(3, 3, 3)
meteor.Color = Color3.fromRGB(255, 120, 30)
meteor.Material = Enum.Material.Neon
local x = base.Position.X + math.random(-15, 15)
local z = base.Position.Z + math.random(-15, 15)
meteor.Position = Vector3.new(x, 40, z)
meteor.Parent = map
meteor.Touched:Connect(defeat)
Debris:AddItem(meteor, 4)
task.wait(0.5)
end
end)
end
function Disasters.Shrink(map)
local base = map.Base
local shrink = TweenInfo.new(Settings.RoundLength * 0.7, Enum.EasingStyle.Linear)
TweenService:Create(base, shrink, { Size = Vector3.new(6, base.Size.Y, 6) }):Play()
end
-- 第 14 课:在这里添加随机灾难
-- 回合循环
while true do
for seconds = Settings.Intermission, 1, -1 do
setStatus("Intermission: " .. seconds)
task.wait(1)
end
local map = ServerStorage.Maps.Island:Clone()
map.Name = "Map"
map.Parent = workspace
local survivors = {}
for _, player in Players:GetPlayers() do
local character = player.Character
if character and character.Humanoid.Health > 0 then
teleport(player, map.Spawn)
survivors[player] = true
character.Humanoid.Died:Connect(function()
survivors[player] = nil
end)
end
end
Disasters.Shrink(map)
for seconds = Settings.RoundLength, 1, -1 do
setStatus("Time left: " .. seconds)
task.wait(1)
end
local names = {}
for player in survivors do
player.leaderstats.Wins.Value += 1
teleport(player, lobbySpawn)
table.insert(names, player.Name)
end
map:Destroy()
if #names > 0 then
setStatus("Winners: " .. table.concat(names, ", "))
else
setStatus("Nobody survived!")
end
task.wait(3)
end
Roblox 游戏:灾难生存中的所有课
自己练习: Roblox 在线编辑器