Files

69 lines
2.8 KiB
Luau

--!strict
local Runtime = script.Parent.Parent
local Types = require(Runtime.Types)
local Common = require(Runtime.Common)
local Kinds = require(game.ReplicatedStorage.Hunter.EventKinds)
local Strings = require(game.ReplicatedStorage.Hunter.Strings)
local Rules = require(Runtime.OneLifeRules)
type Fighter = Types.Fighter
local function start(ctx: Types.ModeContext): Types.ModeInstance
local lifeActive, miniNumber = false, 0
local lifeEnds, nextMini = math.huge, ctx.liveAt
local state = ctx.state
local function runOneLife(members: {Player}, fighters: {Fighter}, now: number): boolean
if lifeActive then
local b, o = 0, 0
for _, f in fighters do if f.side == "Blue" then b += 1 else o += 1 end end
state:SetAttribute("MiniAliveBlue", b)
state:SetAttribute("MiniAliveOrange", o)
if (b > 0 and o > 0) and now < lifeEnds then
ctx.status(Strings.lifeActive(miniNumber, b, o, math.ceil(lifeEnds - now)))
return false
end
local side = Rules.winner(b,o)
if side then ctx.scoring.add(side, 1, Kinds.MiniRoundEnded) end
state:SetAttribute("MiniRoundActive", false)
ctx.events.fire(Kinds.MiniRoundEnded, {side = side, index = miniNumber, blue = b, orange = o})
if Rules.finished(ctx.scoring.get("Blue"),ctx.scoring.get("Orange")) then return true end
for _, p in members do ctx.bench(p) end
lifeActive, nextMini = false, now + Rules.INTERMISSION
end
if now >= nextMini then ctx.balanceWaiting() end
local ready: {Player} = {}
local b, o = false, false
for _, p in members do
local c = p.Character
local h = c and c:FindFirstChildOfClass("Humanoid")
if not c or not c.PrimaryPart or not h or h.Health <= 0 then continue end
table.insert(ready, p)
if Common.sideOf(p) == "Blue" then b = true else o = true end
end
if b and o and now >= nextMini then
for _, p in ready do ctx.deploy(p) end
miniNumber += 1
lifeActive, lifeEnds = true, now + Rules.DURATION
state:SetAttribute("MiniRoundActive",true)
state:SetAttribute("MiniRoundNextAt",nil)
ctx.status(Strings.lifeStart(miniNumber))
ctx.events.fire(Kinds.MiniRoundStarted, {index = miniNumber, blue = ctx.scoring.get("Blue"), orange = ctx.scoring.get("Orange")})
elseif not b or not o then
state:SetAttribute("MiniRoundNextAt",nil)
ctx.status(Strings.lifeWaiting)
else
state:SetAttribute("MiniRoundNextAt",nextMini)
ctx.status(Strings.lifeNext(math.ceil(nextMini - now)))
end
return false
end
return Common.instance(ctx, {
step = function(now, _, fighters) return runOneLife(ctx.members(), fighters, now) end,
death = function(player) ctx.bench(player) end,
destroy = function()
for _, name in {"MiniRoundActive","MiniRoundNextAt","MiniAliveBlue","MiniAliveOrange"} do state:SetAttribute(name,nil) end
end,
})
end
return {id = "OneLife", usesMap = true, start = start}