--!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) 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 destroyed = false local match: Match match = { step = function(now) if destroyed then return false end local elapsed = math.max(0, now - lastStep) lastStep = now return instance.step(now, math.min(elapsed, .5), Context.collect(ctx.members()), elapsed) 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) if not ok then warn(issue) end end, } active[config.arena] = match return match end return Objectives