87 lines
2.8 KiB
Luau
87 lines
2.8 KiB
Luau
--!strict
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local Runtime = script.Parent:WaitForChild("ModeRuntime")
|
|
local Types = require(Runtime:WaitForChild("Types") :: ModuleScript)
|
|
local Context = require(Runtime:WaitForChild("Context") :: ModuleScript)
|
|
|
|
local Diagnostics = require(Runtime:WaitForChild("ZoneDiagnostics") :: ModuleScript)
|
|
|
|
export type Match = {
|
|
step: (number) -> boolean, kill: (Player, Player, Vector3, number) -> (),
|
|
hit: ((Player, number, boolean) -> ())?,
|
|
death: (Player, Vector3?, number) -> (), leaving: (Player, Vector3?, number) -> (),
|
|
winner: () -> string, destroy: () -> (),
|
|
}
|
|
|
|
local registry: {[string]: Types.Mode} = {}
|
|
for _, module in Runtime:WaitForChild("Modes"):GetChildren() do
|
|
if module:IsA("ModuleScript") then
|
|
local mode = require(module) :: Types.Mode
|
|
assert(not registry[mode.id], "Duplicate mode: " .. mode.id)
|
|
registry[mode.id] = mode
|
|
end
|
|
end
|
|
|
|
local cue = Instance.new("RemoteEvent")
|
|
cue.Name = "ArenaObjectiveCue"
|
|
cue.Parent = ReplicatedStorage
|
|
local active: {[string]: Match} = {}
|
|
local Objectives = {}
|
|
|
|
function Objectives.current(arena: string): Match?
|
|
return active[arena]
|
|
end
|
|
|
|
function Objectives.start(config: Types.Config): Match
|
|
local mode = assert(registry[config.rule], "Unknown mode: " .. config.rule)
|
|
local ctx = Context.create(config, mode, cue)
|
|
local success, result = pcall(mode.start, ctx)
|
|
if not success then
|
|
ctx.janitor.clean()
|
|
ctx.folder:Destroy()
|
|
error(result)
|
|
end
|
|
local instance = result :: Types.ModeInstance
|
|
local lastStep = ctx.liveAt
|
|
local profileLabel = "Mode." .. mode.id .. ".step"
|
|
local destroyed = false
|
|
local match: Match
|
|
match = {
|
|
step = function(now)
|
|
if destroyed then return false end
|
|
local elapsed = math.max(0, now - lastStep)
|
|
lastStep = now
|
|
debug.profilebegin(profileLabel)
|
|
Diagnostics.begin()
|
|
local fighters = Context.collect(ctx.members())
|
|
local ok, ended = pcall(instance.step, now, math.min(elapsed, .5), fighters, elapsed)
|
|
Diagnostics.finish(ctx.state, #fighters, now)
|
|
debug.profileend()
|
|
if not ok then error(ended, 0) end
|
|
return ended
|
|
end,
|
|
kill = instance.kill, hit = instance.hit, death = instance.death, leaving = instance.leaving,
|
|
winner = function()
|
|
local side, reason = instance.winner()
|
|
ctx.state:SetAttribute("ResultReason", reason)
|
|
return side
|
|
end,
|
|
destroy = function()
|
|
if destroyed then return end
|
|
destroyed = true
|
|
if active[config.arena] == match then active[config.arena] = nil end
|
|
local ok, issue = pcall(instance.destroy)
|
|
ctx.janitor.clean()
|
|
ctx.folder:Destroy()
|
|
ctx.status("")
|
|
ctx.state:SetAttribute("RoundStage", nil)
|
|
for _, attr in {"DebugRays", "DebugFighters", "DebugEvents"} do ctx.state:SetAttribute(attr, nil) end
|
|
if not ok then warn(issue) end
|
|
end,
|
|
}
|
|
active[config.arena] = match
|
|
return match
|
|
end
|
|
|
|
return Objectives
|