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

190 lines
8.5 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 Context = require(Runtime.Context)
local Rules = require(Runtime.FlagRules)
local HotPoint = require(Runtime.Parent.HotPoint)
type Fighter = Types.Fighter
type Flag = {
marker: Common.Marker, stars: ParticleEmitter, mist: ParticleEmitter, light: PointLight, banner: BasePart,
carrier: Player?, tether: WeldConstraint?, droppedUntil: number,
}
local function makeFlag(parent: Instance, roof: Vector3): Flag
local mark = Common.marker(parent, "CitadelFlag", Strings.flagRoofLabel, roof, Common.gold, 2.6, true)
local body = mark.body
body.Transparency = 1
body.CFrame = CFrame.new(body.Position)
local light = assert(body:FindFirstChildOfClass("PointLight"))
light.Range, light.Brightness = 21, 1.6
local function attach(name: string, size: Vector3, offset: Vector3, tint: Color3): Part
local part = Instance.new("Part")
part.Name, part.Size, part.Color = name, size, tint
part.CFrame = body.CFrame + offset
part.Material = Enum.Material.Neon
part.Massless, part.CanCollide, part.CanTouch, part.CanQuery = true, false, false, false
part.Parent = body
local weld = Instance.new("WeldConstraint")
weld.Part0, weld.Part1, weld.Parent = body, part, part
return part
end
attach("Pole", Vector3.new(.17, 4.5, .17), Vector3.zero, Color3.fromRGB(235, 235, 255))
local banner = attach("Banner", Vector3.new(2.65, 1.48, .15), Vector3.new(1.35, 1.45, 0), Common.gold)
attach("CrownLight", Vector3.new(.33, .33, .33), Vector3.yAxis * 2.34, Common.neutral).Shape = Enum.PartType.Ball
local aura = Instance.new("Attachment")
aura.Name = "FlagAura"
aura.Position = -Vector3.yAxis * 1.75
aura.Parent = body
local stars = Instance.new("ParticleEmitter")
stars.Name = "Stars"
stars.Texture = "rbxasset://textures/particles/sparkles_main.dds"
stars.Color = ColorSequence.new(Common.neutral, Common.gold)
stars.LightEmission, stars.LightInfluence = 1, 0
stars.Rate, stars.Speed, stars.Lifetime = 24, NumberRange.new(3, 6), NumberRange.new(.7, 1.4)
stars.SpreadAngle = Vector2.new(65, 65)
stars.Rotation, stars.RotSpeed = NumberRange.new(-180, 180), NumberRange.new(-90, 90)
stars.Size = NumberSequence.new({NumberSequenceKeypoint.new(0, .22), NumberSequenceKeypoint.new(.4, .52), NumberSequenceKeypoint.new(1, 0)})
stars.Parent = aura
local mist = Instance.new("ParticleEmitter")
mist.Name = "ColoredMist"
mist.Texture = "rbxasset://textures/particles/smoke_main.dds"
mist.Color = ColorSequence.new(Common.neutral, Common.gold)
mist.LightEmission, mist.LightInfluence = .55, 0
mist.Rate, mist.Speed, mist.Lifetime = 9, NumberRange.new(1, 4), NumberRange.new(.7, 1.2)
mist.SpreadAngle = Vector2.new(110, 110)
mist.Size = NumberSequence.new(1.4, 4)
mist.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0, .72), NumberSequenceKeypoint.new(.45, .79), NumberSequenceKeypoint.new(1, 1)})
mist.Parent = aura
return {marker = mark, stars = stars, mist = mist, light = light, banner = banner,
carrier = nil, tether = nil, droppedUntil = 0}
end
local function start(ctx: Types.ModeContext): Types.ModeInstance
local map, geometry = assert(ctx.map), ctx.geometry
local flag = makeFlag(ctx.folder, map.roof.position)
Common.bases(ctx.folder, geometry, map)
local state = ctx.state
local lastSeconds = -1
state:SetAttribute("FlagPosition", flag.marker.origin)
state:SetAttribute("FlagBase_Blue", map.blue)
state:SetAttribute("FlagBase_Orange", map.orange)
local function putFlag(object: Flag, at: Vector3, droppedUntil: number)
if object.carrier then object.carrier:SetAttribute("ArenaFlag", nil) end
if object.tether then object.tether:Destroy() end
object.carrier, object.tether, object.droppedUntil = nil, nil, droppedUntil
state:SetAttribute("FlagCarrierSide", nil)
state:SetAttribute("FlagPosition", at)
state:SetAttribute("FlagReturnAt", if droppedUntil > 0 then droppedUntil else nil)
lastSeconds = -1
local mark = object.marker
mark.body.Anchored = true
mark.body.CFrame = CFrame.new(at + Vector3.yAxis * 2.4)
mark.label.Text = if droppedUntil > 0 then Strings.flagDropLabel else Strings.flagRoofLabel
mark.label.TextColor3, object.banner.Color = Common.gold, Common.gold
object.stars.Enabled, object.mist.Enabled, object.light.Enabled = true, true, true
object.stars:Emit(16)
end
local function pickUpFlag(object: Flag, fighter: Fighter)
local body = object.marker.body
body.CFrame = fighter.root.CFrame * CFrame.new(0, 3.15, .65)
body.Massless = true
local tether = Instance.new("WeldConstraint")
tether.Part0, tether.Part1 = body, fighter.root
tether.Parent = body
body.Anchored = false
object.carrier, object.tether, object.droppedUntil = fighter.player, tether, 0
fighter.player:SetAttribute("ArenaFlag", true)
state:SetAttribute("FlagCarrierSide", fighter.side)
state:SetAttribute("FlagReturnAt", nil)
state:SetAttribute("FlagPosition", fighter.root.Position)
local tint = Common.color(fighter.side)
object.marker.label.Text = Strings.flagCarrierLabel(fighter.player.DisplayName)
object.marker.label.TextColor3, object.banner.Color = tint, tint
object.stars.Enabled, object.mist.Enabled, object.light.Enabled = false, false, false
object.stars:Clear()
object.mist:Clear()
end
local function dropCarried(player: Player, at: Vector3?, now: number, fighters: {Fighter}?)
if flag.carrier == player then
if at then
local floor = workspace:Raycast(at + Vector3.yAxis * .3, -Vector3.yAxis * 50, geometry)
if floor then
putFlag(flag, floor.Position, now + Rules.RETURN_SECONDS)
for _, fighter in (fighters or Context.collect(ctx.members())) do ctx.legacy(fighter.player, "FlagDropped") end
ctx.events.fire(Kinds.FlagDropped, {at = floor.Position})
else
putFlag(flag, flag.marker.origin, 0)
ctx.events.fire(Kinds.FlagReturned, {at = flag.marker.origin})
end
else
putFlag(flag, flag.marker.origin, 0)
ctx.events.fire(Kinds.FlagReturned, {at = flag.marker.origin})
end
end
end
local function runFlag(fighters: {Fighter}, now: number)
if flag.carrier then
for _, fighter in fighters do
if fighter.player ~= flag.carrier then continue end
state:SetAttribute("FlagPosition", fighter.root.Position)
state:SetAttribute("FlagCarrierSide", fighter.side)
local base = if fighter.side == "Blue" then map.blue else map.orange
if Common.boundsAt(fighter.root.Position, base, HotPoint.Radius, 7) then
ctx.scoring.add(fighter.side, 1, Kinds.FlagScored, fighter.player)
ctx.contribute(fighter.player, "deliveries", 1)
ctx.events.fire(Kinds.FlagScored, {side = fighter.side, userId = fighter.player.UserId})
ctx.cue(fighters, true, nil)
putFlag(flag, flag.marker.origin, 0)
ctx.events.fire(Kinds.FlagReturned, {at = flag.marker.origin})
ctx.status(Strings.flagReturned)
end
return
end
local carrier = flag.carrier
local root = carrier.Character and carrier.Character.PrimaryPart
dropCarried(carrier, root and root.Position, now, fighters)
return
elseif flag.droppedUntil > 0 and now >= flag.droppedUntil then
putFlag(flag, flag.marker.origin, 0)
ctx.events.fire(Kinds.FlagReturned, {at = flag.marker.origin})
end
if flag.droppedUntil > 0 then
local seconds = Rules.remaining(flag.droppedUntil, now)
if seconds ~= lastSeconds then
lastSeconds = seconds
flag.marker.label.Text = string.format(Strings.flagHints.dropLabel, seconds)
end
end
local at = flag.marker.body.Position
for _, fighter in fighters do
if Common.boundsAt(fighter.root.Position, at, 5.5, 6.5) and Common.visibleAt(fighter, at, geometry) then
pickUpFlag(flag, fighter)
ctx.contribute(fighter.player, "carries", 1)
ctx.events.fire(Kinds.FlagPicked, {side = fighter.side, userId = fighter.player.UserId, text = fighter.player.DisplayName})
ctx.cue(fighters, false, fighter.side)
ctx.status(Strings.flagCarrier(fighter.player.DisplayName))
return
end
end
ctx.status(if flag.droppedUntil > 0 then Strings.flagOnGround else Strings.flagOnRoof)
end
return Common.instance(ctx, {
step = function(now, _, fighters) runFlag(fighters, now); return false end,
death = dropCarried, leaving = dropCarried,
destroy = function()
if flag.carrier then flag.carrier:SetAttribute("ArenaFlag", nil) end
for _, name in {"FlagPosition","FlagCarrierSide","FlagReturnAt","FlagBase_Blue","FlagBase_Orange"} do state:SetAttribute(name,nil) end
end,
})
end
return {id = "CaptureFlag", usesMap = true, start = start}