Files
Hunter/scripts/ModeRuntime/Modes/TrophyHunt.luau
T

96 lines
3.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)
type Fighter = Types.Fighter
local HotPoint = require(Runtime.Parent.HotPoint)
local Rules = require(Runtime.TrophyRules)
local Diagnostics = require(Runtime.ZoneDiagnostics)
type Drop = {marker: Common.Marker, amount: number, expires: number}
local function start(ctx: Types.ModeContext): Types.ModeInstance
local map, geometry, folder = assert(ctx.map), ctx.geometry, ctx.folder
Common.bases(folder, geometry, map)
local tokens: {Drop} = {}
local carried: {[Player]: number} = {}
local function floorAt(at: Vector3): Vector3
local hit = Diagnostics.raycast(at + Vector3.yAxis * .3, -Vector3.yAxis * 50, geometry)
return if hit then hit.Position else map.ground.position
end
local function tokenDrop(at: Vector3, amount: number, now: number)
local floor = floorAt(at)
ctx.events.fire(Kinds.TokenDropped, {at = floor, value = amount})
for _, existing in tokens do
if Rules.canMerge(existing.marker.origin, floor) then
existing.amount += amount
existing.expires = now + Rules.LIFETIME
existing.marker.body:SetAttribute("TokenExpiresAt", existing.expires)
existing.marker.label.Text = Strings.tokens(existing.amount)
return
end
end
if #tokens >= Rules.MAX_DROPS then
local old = table.remove(tokens, 1)
old.marker.body:Destroy()
end
local mark = Common.marker(folder, "Tokens", Strings.tokens(amount), floor, Common.gold, 1.55, true)
mark.body:SetAttribute("TokenExpiresAt", now + Rules.LIFETIME)
mark.body:SetAttribute("TokenFlashSeconds", Rules.FLASH_SECONDS)
table.insert(tokens, {marker = mark, amount = amount, expires = now + Rules.LIFETIME})
end
local function runTrophies(fighters: {Fighter}, now: number)
for i = #tokens, 1, -1 do
local token = tokens[i]
local collector: Fighter? = nil
if now < token.expires then
for _, f in fighters do
if Common.boundsAt(f.root.Position, token.marker.origin, 5, 6) and Common.visibleAt(f, token.marker.body.Position, geometry) then collector = f; break end
end
if not collector then continue end
local p = collector.player
if not carried[p] then ctx.events.fire(Kinds.TokenPicked, {userId = p.UserId}, {p}) end
carried[p] = (carried[p] or 0) + token.amount
p:SetAttribute("ArenaTokens", carried[p])
end
token.marker.body:Destroy()
table.remove(tokens, i)
end
for _, f in fighters do
local amount = carried[f.player]
if amount and Common.boundsAt(f.root.Position, if f.side == "Blue" then map.blue else map.orange, HotPoint.Radius, 7) then
ctx.scoring.add(f.side, amount, Kinds.TokensBanked, f.player)
ctx.contribute(f.player, "tokensBanked", amount)
ctx.events.fire(Kinds.TokensBanked, {side = f.side, userId = f.player.UserId, value = amount, at = if f.side == "Blue" then map.blue else map.orange})
ctx.cue(fighters, true, nil)
carried[f.player] = nil
f.player:SetAttribute("ArenaTokens", nil)
end
end
ctx.status(Strings.trophyStatus)
end
local function drop(player: Player, at: Vector3?, now: number)
local amount = carried[player]
if not amount then return end
carried[player] = nil
player:SetAttribute("ArenaTokens", nil)
if at then
tokenDrop(at, amount, now)
ctx.events.fire(Kinds.TokensLost, {userId=player.UserId,value=amount})
end
end
return Common.instance(ctx, {
step = function(now, _, fighters) runTrophies(fighters, now); return false end,
kill = function(player, _, at, now)
if Common.sideOf(player) then tokenDrop(at, 1, now) end
end,
death = drop, leaving = drop,
destroy = function()
for p in carried do p:SetAttribute("ArenaTokens", nil) end
end,
})
end
return {id = "TrophyHunt", usesMap = true, start = start}