140 lines
5.4 KiB
Luau
140 lines
5.4 KiB
Luau
--!strict
|
|
local Players = game:GetService("Players")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local RunService = game:GetService("RunService")
|
|
|
|
local Catalog = require(ReplicatedStorage:WaitForChild("PowerUpCatalog") :: ModuleScript)
|
|
local Magnet = require(ReplicatedStorage:WaitForChild("LootMagnet") :: ModuleScript)
|
|
local Effects = require(script.Parent:WaitForChild("PowerUpEffects") :: ModuleScript)
|
|
local Modes = require(ReplicatedStorage:WaitForChild("ArenaModes") :: ModuleScript)
|
|
local round = ReplicatedStorage:WaitForChild("ArenaRound")
|
|
|
|
local LIFE, AIR_TIME = 60, .63
|
|
local container = Instance.new("Folder")
|
|
container.Name = "PowerUpDrops"
|
|
container:SetAttribute("Life", LIFE)
|
|
container:SetAttribute("AirTime", AIR_TIME)
|
|
local collected = Instance.new("RemoteEvent")
|
|
collected.Name = "Collected"
|
|
collected.Parent = container
|
|
container.Parent = ReplicatedStorage
|
|
|
|
type Drop = {
|
|
data: Configuration, key: string, mode: string, position: Vector3, ready: number, expires: number,
|
|
collector: Player?, character: Model?, launch: number?,
|
|
}
|
|
type Candidate = {player: Player, character: Model, position: Vector3}
|
|
local drops: {[Configuration]: Drop} = {}
|
|
local nextId = 0
|
|
local random = Random.new()
|
|
local function groundNear(origin: Vector3, geometry: RaycastParams): Vector3?
|
|
local floor = workspace:Raycast(origin + Vector3.yAxis * 2, -Vector3.yAxis * 155, geometry)
|
|
if not floor then return nil end
|
|
local center = floor.Position + Vector3.yAxis * 2.15
|
|
local angle = random:NextNumber(0, math.pi * 2)
|
|
for i = 1, 5 do
|
|
local offset = Vector3.new(math.cos(angle + i * 2.4), 0, math.sin(angle + i * 2.4)) * 5.8
|
|
if workspace:Raycast(center, offset, geometry) then continue end
|
|
local nearby = workspace:Raycast(center + offset + Vector3.yAxis, -Vector3.yAxis * 7, geometry)
|
|
if nearby then
|
|
local rest = nearby.Position + Vector3.yAxis * 2.15
|
|
if not workspace:Raycast(center, rest - center, geometry) then return rest end
|
|
end
|
|
end
|
|
return center
|
|
end
|
|
|
|
local PowerUpDrops = {}
|
|
function PowerUpDrops.spawn(origin: Vector3, requested: string?, arenaMode: string?)
|
|
local now = workspace:GetServerTimeNow()
|
|
local mode = arenaMode or Modes.Procedural
|
|
if not Modes.open(round, mode, now) then return end
|
|
local position = groundNear(origin, Modes.collision(mode))
|
|
if not position then return end
|
|
local definition = Catalog[random:NextInteger(1, #Catalog)]
|
|
if requested then
|
|
for _, spec in Catalog do
|
|
if spec.id == requested then definition = spec break end
|
|
end
|
|
end
|
|
nextId += 1
|
|
local data = Instance.new("Configuration")
|
|
data.Name = tostring(nextId)
|
|
data:SetAttribute("Kind", definition.id)
|
|
data:SetAttribute("ArenaMode", mode)
|
|
data:SetAttribute("Origin", origin)
|
|
data:SetAttribute("Rest", position)
|
|
data:SetAttribute("Born", now)
|
|
data:SetAttribute("Expires", now + LIFE)
|
|
drops[data] = {data = data, key = definition.id, mode = mode, position = position, ready = now + AIR_TIME + .18, expires = now + LIFE}
|
|
data.Parent = container
|
|
end
|
|
|
|
local function remove(drop: Drop)
|
|
drops[drop.data] = nil
|
|
drop.data:Destroy()
|
|
end
|
|
|
|
local function acquire(drop: Drop, candidates: {Candidate}, now: number)
|
|
local geometry = Modes.collision(drop.mode)
|
|
local best: Candidate? = nil
|
|
local distance = Magnet.Radius
|
|
for _, candidate in candidates do
|
|
local range = (candidate.position - drop.position).Magnitude
|
|
if range <= distance and Magnet.canReach(candidate.position, drop.position, distance, geometry) then
|
|
best, distance = candidate, range
|
|
end
|
|
end
|
|
if not best then return end
|
|
local launch = now + .05
|
|
drop.collector, drop.character, drop.launch = best.player, best.character, launch
|
|
drop.data:SetAttribute("Collector", best.player.UserId)
|
|
drop.data:SetAttribute("Launch", launch)
|
|
end
|
|
|
|
local function complete(drop: Drop, byPlayer: {[Player]: Candidate}, now: number)
|
|
local owner = drop.collector
|
|
local current = owner and byPlayer[owner]
|
|
if not owner or not current or current.character ~= drop.character or Modes.of(owner) ~= drop.mode
|
|
or not Magnet.canReach(current.position, drop.position, 80, Modes.collision(drop.mode)) then
|
|
drop.collector, drop.character, drop.launch = nil, nil, nil
|
|
drop.data:SetAttribute("Collector", nil)
|
|
drop.data:SetAttribute("Launch", nil)
|
|
return
|
|
end
|
|
Effects.grant(owner, drop.key, now)
|
|
collected:FireClient(owner, drop.key)
|
|
remove(drop)
|
|
end
|
|
|
|
local elapsed = 0
|
|
RunService.Heartbeat:Connect(function(dt)
|
|
elapsed += dt
|
|
if elapsed < .05 then return end
|
|
elapsed %= .05
|
|
if not next(drops) then return end
|
|
local now = workspace:GetServerTimeNow()
|
|
local candidates: {[string]: {Candidate}} = {[Modes.Procedural] = {}}
|
|
local byPlayer: {[Player]: Candidate} = {}
|
|
for _, p in Players:GetPlayers() do
|
|
local character = p.Character
|
|
local root = character and character.PrimaryPart
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
if p:GetAttribute("InArena") ~= true or not character or not root or not humanoid or humanoid.Health <= 0 then continue end
|
|
local candidate = {player = p, character = character, position = root.Position}
|
|
table.insert(candidates[Modes.of(p)], candidate)
|
|
byPlayer[p] = candidate
|
|
end
|
|
for _, drop in drops do
|
|
if now >= drop.expires or not Modes.open(round, drop.mode, now) then
|
|
remove(drop)
|
|
elseif drop.launch then
|
|
if now >= drop.launch + Magnet.Duration then complete(drop, byPlayer, now) end
|
|
elseif now >= drop.ready and now + .05 + Magnet.Duration < drop.expires then
|
|
acquire(drop, candidates[drop.mode], now)
|
|
end
|
|
end
|
|
end)
|
|
|
|
return PowerUpDrops
|