Files
Hunter/scripts/HotPoint.luau
T

310 lines
12 KiB
Luau
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--!strict
local TweenService = game:GetService("TweenService")
local Sites = require(script.Parent:WaitForChild("ArenaObjectiveSites") :: ModuleScript)
type Side = "Blue" | "Orange"
export type Fighter = {player: Player, side: Side, root: BasePart}
export type Marker = {body: Part, label: TextLabel, origin: Vector3}
type Config = {
state: Instance, geometry: RaycastParams, sites: {Sites.Site}, startedAt: number,
marker: (Sites.Site) -> Marker, score: (Side, number) -> (), status: (string) -> (),
contribute: ((Player, string, number) -> ())?,
}
type Capture = {
step: ({Fighter}, number, number) -> (), hit: (Player, number, boolean) -> (),
death: (Player) -> (), destroy: () -> (),
}
local HotPoint = {Radius = 18}
local CAPTURE_SECONDS = 10
local ROTATION_SECONDS = 40
local COLLAPSE_SECONDS = .55
local CAPTURE_NOTICE_SECONDS = 2
local NEXT_POINT_WARNING_SECONDS = 5
local TEAM_SPEED = 1.5
local HIT_WINDOW = 1.2
local HIT_DECAY = .8
local neutral = Color3.fromRGB(183, 132, 255)
local contested = Color3.fromRGB(255, 211, 124)
local teamColor: {[Side]: Color3} = {Blue = Color3.fromRGB(76, 228, 255), Orange = Color3.fromRGB(255, 81, 105)}
function HotPoint.attachRing(mark: Marker, radius: number?): (Color3) -> ()
local r = radius or HotPoint.Radius
local body = mark.body
body.Transparency = .92
local beams: {Beam} = {}
local sparks: {ParticleEmitter} = {}
local function anchor(angle: number, height: number): Attachment
local radial = Vector3.new(math.cos(angle), 0, math.sin(angle))
local tangent = Vector3.new(-radial.Z, 0, radial.X)
local item = Instance.new("Attachment")
local world = CFrame.fromMatrix(mark.origin + radial * r + Vector3.yAxis * height, tangent, Vector3.yAxis)
item.CFrame = body.CFrame:ToObjectSpace(world)
item.Parent = body
return item
end
local function halo(name: string, height: number, width: number, transparency: number)
local corners: {Attachment} = {}
for index = 0, 3 do
table.insert(corners, anchor(index * math.pi / 2, height))
end
local handle = 4 * (math.sqrt(2) - 1) / 3 * r
for index, start in corners do
local beam = Instance.new("Beam")
beam.Name = name
beam.Attachment0, beam.Attachment1 = start, corners[index % #corners + 1]
beam.CurveSize0, beam.CurveSize1 = handle, handle
beam.Segments, beam.FaceCamera = 18, true
beam.Width0, beam.Width1 = width, width
beam.Transparency = NumberSequence.new(transparency)
beam.LightEmission, beam.LightInfluence, beam.Brightness = 1, 0, 2
beam.Parent = body
table.insert(beams, beam)
end
end
halo("FloorRing", .29, .32, .1)
for index = 0, 19 do
local angle = index * math.pi / 10
local source = anchor(angle, .28)
if index % 2 == 0 then
local veil = Instance.new("Beam")
veil.Name = "RisingVeil"
veil.Attachment0, veil.Attachment1 = source, anchor(angle, 7.4)
veil.FaceCamera, veil.Segments = true, 8
veil.Width0, veil.Width1 = .78, .3
veil.LightEmission, veil.LightInfluence, veil.Brightness = 1, 0, 1.4
veil.Transparency = NumberSequence.new({
NumberSequenceKeypoint.new(0, .42), NumberSequenceKeypoint.new(.38, .74), NumberSequenceKeypoint.new(1, 1),
})
veil.Parent = body
table.insert(beams, veil)
end
local emitter = Instance.new("ParticleEmitter")
emitter.Name = "RisingSparks"
emitter.Texture = "rbxasset://textures/particles/sparkles_main.dds"
emitter.EmissionDirection = Enum.NormalId.Top
emitter.Rate, emitter.Speed = 12, NumberRange.new(16.5, 27)
emitter.Lifetime, emitter.SpreadAngle = NumberRange.new(.6, 1.08), Vector2.new(8, 8)
emitter.Rotation, emitter.RotSpeed = NumberRange.new(-180, 180), NumberRange.new(-95, 95)
emitter.LightEmission, emitter.LightInfluence, emitter.Brightness = 1, 0, 2
emitter.Size = NumberSequence.new({
NumberSequenceKeypoint.new(0, .16), NumberSequenceKeypoint.new(.25, .55), NumberSequenceKeypoint.new(1, .08),
})
emitter.Transparency = NumberSequence.new({
NumberSequenceKeypoint.new(0, 1), NumberSequenceKeypoint.new(.22, .12), NumberSequenceKeypoint.new(1, 1),
})
emitter.Parent = source
table.insert(sparks, emitter)
end
return function(tint: Color3)
body.Color, mark.label.TextColor3 = tint, tint
local light = body:FindFirstChildOfClass("PointLight")
if light then light.Color, light.Range = tint, 18 end
local spectrum = ColorSequence.new({
ColorSequenceKeypoint.new(0, tint), ColorSequenceKeypoint.new(.5, tint:Lerp(Color3.new(1, 1, 1), .38)),
ColorSequenceKeypoint.new(1, tint),
})
for _, beam in beams do beam.Color = spectrum end
for _, emitter in sparks do emitter.Color = spectrum end
end
end
local function collapse(mark: Marker)
local body = mark.body
local motion = TweenInfo.new(COLLAPSE_SECONDS, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local center = body.CFrame:PointToObjectSpace(mark.origin + Vector3.yAxis * .29)
body:SetAttribute("ObjectiveArrowHidden", true)
mark.label.Visible = false
TweenService:Create(body, motion, {Size = Vector3.new(body.Size.X, .1, .1), Transparency = 1}):Play()
for _, item in body:GetDescendants() do
if item:IsA("Attachment") then
TweenService:Create(item, motion, {Position = center}):Play()
elseif item:IsA("Beam") then
TweenService:Create(item, motion, {Width0 = 0, Width1 = 0, CurveSize0 = 0, CurveSize1 = 0}):Play()
elseif item:IsA("ParticleEmitter") then
item.Enabled = false
elseif item:IsA("PointLight") then
TweenService:Create(item, motion, {Brightness = 0, Range = 0}):Play()
end
end
local source = Instance.new("Attachment")
source.CFrame = body.CFrame:ToObjectSpace(CFrame.new(mark.origin + Vector3.yAxis * .65))
source.Parent = body
local smoke = Instance.new("ParticleEmitter")
smoke.Name = "CaptureSmoke"
smoke.Texture = "rbxasset://textures/particles/smoke_main.dds"
smoke.Rate = 0
smoke.Lifetime = NumberRange.new(.8, 1.45)
smoke.Speed = NumberRange.new(7, 16)
smoke.SpreadAngle = Vector2.new(68, 68)
smoke.Rotation = NumberRange.new(-180, 180)
smoke.Acceleration = Vector3.yAxis * 6
smoke.Color = ColorSequence.new(body.Color:Lerp(Color3.fromRGB(215, 222, 236), .65))
smoke.Size = NumberSequence.new({
NumberSequenceKeypoint.new(0, 2), NumberSequenceKeypoint.new(.5, 6), NumberSequenceKeypoint.new(1, 10),
})
smoke.Transparency = NumberSequence.new({
NumberSequenceKeypoint.new(0, .28), NumberSequenceKeypoint.new(.42, .46), NumberSequenceKeypoint.new(1, 1),
})
smoke.Parent = source
smoke:Emit(32)
end
function HotPoint.start(config: Config): Capture
local state = config.state
local index, credit, hitSince, hitUntil = 0, 0, 0, 0
local movesAt = config.startedAt
local capturedAt = 0
local capturedSide: Side? = nil
local claiming: Side? = nil
local capturers: {[Player]: boolean} = {}
local mark: Marker? = nil
local paint: ((Color3) -> ())? = nil
local lastTint: Color3? = nil
local function publish(side: Side?, percent: number, phase: string?)
if state:GetAttribute("HotPointTeam") ~= side then state:SetAttribute("HotPointTeam", side) end
if state:GetAttribute("HotPointProgress") ~= percent then state:SetAttribute("HotPointProgress", percent) end
if state:GetAttribute("HotPointPhase") ~= phase then state:SetAttribute("HotPointPhase", phase) end
end
local function clearCapture()
claiming, credit, hitSince, hitUntil = nil, 0, 0, 0
table.clear(capturers)
end
local function paintRing(tint: Color3)
if paint and lastTint ~= tint then
lastTint = tint
paint(tint)
end
end
local function isInside(fighter: Fighter, origin: Vector3): boolean
local delta = fighter.root.Position - origin
if delta.Y < -.5 or delta.Y > 60 or delta.X * delta.X + delta.Z * delta.Z > HotPoint.Radius ^ 2 then return false end
if delta.Y > 8 then
-- A jump over the point still counts when the first floor below is the capture floor.
local floor = workspace:Raycast(fighter.root.Position, -Vector3.yAxis * (delta.Y + .8), config.geometry)
return floor ~= nil and math.abs(floor.Position.Y - origin.Y) < .65
end
local to = origin + Vector3.yAxis * 2.7 - fighter.root.Position
return workspace:Raycast(fighter.root.Position, to, config.geometry) == nil
end
local function step(fighters: {Fighter}, now: number, dt: number)
if index == 0 or now >= movesAt then
if mark then mark.body:Destroy() end
index = index % #config.sites + 1
local activeSite = config.sites[index]
local fresh = config.marker(activeSite)
mark, paint, lastTint = fresh, HotPoint.attachRing(fresh), nil
capturedSide = nil
movesAt = now + ROTATION_SECONDS
clearCapture()
publish(nil, 0, nil)
state:SetAttribute("HotPointPosition", activeSite.position)
state:SetAttribute("HotPointNextPosition", config.sites[index % #config.sites + 1].position)
state:SetAttribute("HotPointMovesAt", movesAt)
elseif capturedSide then
local since = now - capturedAt
if since >= COLLAPSE_SECONDS + CAPTURE_NOTICE_SECONDS then
publish(nil, 0, nil)
config.status("Скоро появится новая точка")
elseif since >= COLLAPSE_SECONDS then
publish(capturedSide, 100, "Captured")
config.status("Точка захвачена")
end
return
end
local site = config.sites[index]
local blue, orange = 0, 0
local inside: {Fighter} = {}
for _, fighter in fighters do
if not isInside(fighter, site.position) then continue end
table.insert(inside, fighter)
if config.contribute then config.contribute(fighter.player, "holdSeconds", dt) end
if fighter.side == "Blue" then blue += 1 else orange += 1 end
end
local remaining = math.ceil(movesAt - now)
config.status(site.name .. " · Смена точки через " .. remaining .. " с")
local soleSide: Side? = if blue > 0 and orange == 0 then "Blue" elseif orange > 0 and blue == 0 then "Orange" else nil
local isContested = blue > 0 and orange > 0
if soleSide then
if claiming ~= soleSide then
clearCapture()
claiming = soleSide
dt = 0
end
elseif not isContested or not claiming then
clearCapture()
end
local side = claiming
if side then
table.clear(capturers)
for _, fighter in inside do
if fighter.side == side then capturers[fighter.player] = true end
end
local underFire = now < hitUntil
local damageTime = math.max(0, math.min(now, hitUntil) - math.max(now - dt, hitSince))
if damageTime > 0 then credit = math.max(0, credit - damageTime * HIT_DECAY) end
if not isContested then
local count = if side == "Blue" then blue else orange
credit += (dt - damageTime) * (if count >= 2 then TEAM_SPEED else 1)
end
if credit >= CAPTURE_SECONDS then
capturedSide, capturedAt = side, now
movesAt = now + COLLAPSE_SECONDS + CAPTURE_NOTICE_SECONDS + NEXT_POINT_WARNING_SECONDS
config.score(side, 1)
if config.contribute then
for player in capturers do config.contribute(player, "captures", 1) end
end
clearCapture()
publish(nil, 0, nil)
config.status("")
state:SetAttribute("HotPointPosition", nil)
state:SetAttribute("HotPointMovesAt", movesAt)
paintRing(teamColor[side])
if mark then collapse(mark) end
else
local phase = if isContested then "Contested" elseif underFire then "UnderFire" else "Capturing"
publish(side, math.floor(100 * credit / CAPTURE_SECONDS), phase)
paintRing(if isContested then contested else teamColor[side])
end
elseif isContested then
publish(nil, 0, "Contested")
paintRing(contested)
else
publish(nil, 0, nil)
paintRing(neutral)
end
end
local function death(player: Player)
if not capturers[player] then return end
clearCapture()
publish(nil, 0, nil)
end
return {
step = step,
hit = function(player: Player, now: number, killed: boolean)
if killed then death(player)
elseif capturers[player] then
if now >= hitUntil then hitSince = now end
hitUntil = now + HIT_WINDOW
end
end,
death = death,
destroy = function()
if mark then mark.body:Destroy() end
publish(nil, 0, nil)
state:SetAttribute("HotPointPosition", nil)
state:SetAttribute("HotPointNextPosition", nil)
state:SetAttribute("HotPointMovesAt", nil)
end,
}
end
return table.freeze(HotPoint)