217 lines
8.2 KiB
Luau
217 lines
8.2 KiB
Luau
-- Пушки на турели: выстрелы считает сервер (темп, перегрев, сектор обстрела, урон), клиенты рисуют трассеры
|
|
-- и снаряды. Параметры — у пушки из слота «Оружие» (Config.Garage.Parts → gun): автоматы попадают сразу,
|
|
-- пушка и ракетница стреляют летящими снарядами со взрывом.
|
|
-- Бочки на карте взрываются от попаданий, взрыв поджигает соседние бочки.
|
|
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
local Config = require(ReplicatedStorage.Shared.Config)
|
|
local BuggyStats = require(ReplicatedStorage.Shared.BuggyStats)
|
|
local GunMath = require(ReplicatedStorage.Shared.GunMath)
|
|
local Buggy = require(script.Parent.Buggy)
|
|
local World = require(script.Parent.World)
|
|
|
|
local G = Config.Gun
|
|
local Weapons = {}
|
|
|
|
local Tracer: RemoteEvent
|
|
local rng = Random.new()
|
|
|
|
local function humanoidOf(instance: Instance): Humanoid?
|
|
local model = instance:FindFirstAncestorOfClass("Model")
|
|
return model and model:FindFirstChildOfClass("Humanoid")
|
|
end
|
|
|
|
local function explode(barrel: BasePart)
|
|
if not barrel.Parent then
|
|
return
|
|
end
|
|
local respawn = barrel:Clone()
|
|
local parent = barrel.Parent
|
|
local position = barrel.Position
|
|
barrel:Destroy()
|
|
|
|
local explosion = Instance.new("Explosion")
|
|
explosion.Position = position
|
|
explosion.BlastRadius = G.ExplosionRadius
|
|
explosion.BlastPressure = 150000 -- подбрасывает багги, но не разваливает их
|
|
explosion.DestroyJointRadiusPercent = 0
|
|
explosion.ExplosionType = Enum.ExplosionType.NoCraters -- свои воронки фиксированного размера (World.Crater)
|
|
World.Crater(position)
|
|
local damaged = {}
|
|
explosion.Hit:Connect(function(part, distance)
|
|
local humanoid = humanoidOf(part)
|
|
if humanoid and not damaged[humanoid] then
|
|
damaged[humanoid] = true
|
|
humanoid:TakeDamage(G.ExplosionDamage * math.clamp(1 - distance / G.ExplosionRadius, 0.2, 1))
|
|
end
|
|
if part:GetAttribute("Explosive") then
|
|
task.delay(0.15, explode, part) -- цепная реакция
|
|
end
|
|
end)
|
|
explosion.Parent = workspace
|
|
|
|
task.delay(G.BarrelRespawn, function()
|
|
respawn:SetAttribute("Health", G.BarrelHealth)
|
|
respawn.Parent = parent
|
|
end)
|
|
end
|
|
|
|
-- Пуля попала в instance.
|
|
local function hit(instance: BasePart, shooter: Player, damage: number)
|
|
if instance:GetAttribute("Explosive") then
|
|
local health = (instance:GetAttribute("Health") or 1) - 1
|
|
instance:SetAttribute("Health", health)
|
|
if health <= 0 then
|
|
explode(instance)
|
|
end
|
|
return
|
|
end
|
|
local humanoid = humanoidOf(instance)
|
|
if humanoid and humanoid.Parent ~= shooter.Character and humanoid.Health > 0 then
|
|
humanoid:SetAttribute("LastHitBy", shooter.UserId) -- кому награда за убитого зомби (Zombies)
|
|
humanoid:TakeDamage(damage)
|
|
return
|
|
end
|
|
-- Бронелист (детали из гаража с armor) держит пулю.
|
|
if instance:GetAttribute("Armor") then
|
|
return
|
|
end
|
|
-- Попали в чужую багги — достаётся водителю.
|
|
local model = instance:FindFirstAncestorOfClass("Model")
|
|
local driver = model and Buggy.Driver(model)
|
|
local driverHumanoid = driver and driver.Character and driver.Character:FindFirstChildOfClass("Humanoid")
|
|
if driverHumanoid and driver ~= shooter then
|
|
driverHumanoid:TakeDamage(damage * 0.5)
|
|
end
|
|
end
|
|
|
|
-- Взрыв снаряда пушки или ракеты: урон спадает от центра к краю, бочки рядом детонируют.
|
|
-- Стрелка не задевает. Багги в радиусе подбрасывает, водителю достаётся половина (если не прикрыт бронёй целиком).
|
|
local function blast(position: Vector3, spec, shooter: Player, damage: number)
|
|
local explosion = Instance.new("Explosion")
|
|
explosion.Position = position
|
|
explosion.BlastRadius = spec.blastRadius
|
|
explosion.BlastPressure = 90000
|
|
explosion.DestroyJointRadiusPercent = 0
|
|
explosion.ExplosionType = Enum.ExplosionType.NoCraters
|
|
World.Crater(position)
|
|
local damaged = {}
|
|
explosion.Hit:Connect(function(part, distance)
|
|
local falloff = math.clamp(1 - distance / spec.blastRadius, 0.2, 1)
|
|
if part:GetAttribute("Explosive") then
|
|
task.delay(0.1, explode, part)
|
|
return
|
|
end
|
|
local humanoid = humanoidOf(part)
|
|
if not humanoid then
|
|
local model = part:FindFirstAncestorOfClass("Model")
|
|
local driver = model and Buggy.Driver(model)
|
|
humanoid = driver and driver.Character and driver.Character:FindFirstChildOfClass("Humanoid")
|
|
falloff *= 0.5
|
|
end
|
|
if humanoid and not damaged[humanoid] and humanoid.Parent ~= shooter.Character then
|
|
damaged[humanoid] = true
|
|
humanoid:SetAttribute("LastHitBy", shooter.UserId)
|
|
humanoid:TakeDamage(damage * falloff)
|
|
end
|
|
end)
|
|
explosion.Parent = workspace
|
|
end
|
|
|
|
local function aimTurret(data, yaw: number, pitch: number)
|
|
data.gun.motor.C0 = GunMath.Mount * GunMath.Rotation(yaw, pitch)
|
|
end
|
|
|
|
local function isAngle(v): boolean
|
|
return typeof(v) == "number" and v == v and math.abs(v) < 4
|
|
end
|
|
|
|
function Weapons.Init(remotes: Folder)
|
|
local FireGun = Instance.new("RemoteEvent")
|
|
FireGun.Name = "FireGun"
|
|
FireGun.Parent = remotes
|
|
|
|
local AimGun = Instance.new("UnreliableRemoteEvent")
|
|
AimGun.Name = "AimGun"
|
|
AimGun.Parent = remotes
|
|
|
|
Tracer = Instance.new("RemoteEvent")
|
|
Tracer.Name = "Tracer"
|
|
Tracer.Parent = remotes
|
|
|
|
-- Поворот турели, чтобы другие игроки видели, куда целится водитель.
|
|
AimGun.OnServerEvent:Connect(function(player, yaw, pitch)
|
|
local model = Buggy.FindDrivenBy(player)
|
|
if model and isAngle(yaw) and isAngle(pitch) then
|
|
aimTurret(Buggy.Get(model),
|
|
math.clamp(yaw, -math.rad(G.YawLimit), math.rad(G.YawLimit)),
|
|
math.clamp(pitch, math.rad(G.PitchMin), math.rad(G.PitchMax)))
|
|
end
|
|
end)
|
|
|
|
FireGun.OnServerEvent:Connect(function(player, target)
|
|
if typeof(target) ~= "Vector3" then
|
|
return
|
|
end
|
|
local model = Buggy.FindDrivenBy(player)
|
|
if not model then
|
|
return
|
|
end
|
|
local data = Buggy.Get(model)
|
|
if data.locked then
|
|
return -- раунд ещё не начался
|
|
end
|
|
local gun = data.gun
|
|
local spec = gun.spec
|
|
local now = os.clock()
|
|
|
|
-- Остывание и перегрев (темп с запасом 20% на сетевые задержки)
|
|
gun.heat = math.max(gun.heat - (now - gun.heatTime) * G.CoolRate, 0)
|
|
gun.heatTime = now
|
|
if gun.overheated and gun.heat <= G.CoolResume then
|
|
gun.overheated = false
|
|
end
|
|
-- Бусты из дропа: скорострельность и урон
|
|
local fireRate = spec.fireRate * BuggyStats.BoostMultiplier(player, "firerate")
|
|
local damage = spec.damage * BuggyStats.BoostMultiplier(player, "damage")
|
|
if gun.overheated or now - gun.lastShot < 0.8 / fireRate then
|
|
return
|
|
end
|
|
gun.lastShot = now
|
|
gun.heat += spec.heatPerShot
|
|
if gun.heat >= 1 then
|
|
gun.overheated = true
|
|
end
|
|
|
|
local chassisCFrame = data.chassis.CFrame
|
|
local mount = (chassisCFrame * GunMath.Mount).Position
|
|
local yaw, pitch = GunMath.Angles(chassisCFrame, target - mount)
|
|
aimTurret(data, yaw, pitch)
|
|
|
|
local spread = math.rad(spec.spread)
|
|
local turret = GunMath.Turret(chassisCFrame, yaw + rng:NextNumber(-spread, spread), pitch + rng:NextNumber(-spread, spread))
|
|
local origin = (turret * GunMath.Muzzle).Position
|
|
local direction = turret.LookVector * G.Range
|
|
|
|
local params = RaycastParams.new()
|
|
params.FilterType = Enum.RaycastFilterType.Exclude
|
|
params.FilterDescendantsInstances = { model, player.Character }
|
|
local result = workspace:Raycast(origin, direction, params)
|
|
local hitPosition = result and result.Position or origin + direction
|
|
|
|
if spec.kind == "bullet" then
|
|
if result then
|
|
hit(result.Instance, player, damage)
|
|
end
|
|
else
|
|
-- Снаряд летит до точки попадания (клиенты рисуют полёт), там взрыв. Промах — взрыв в конце дальности.
|
|
local travel = (hitPosition - origin).Magnitude / spec.speed
|
|
task.delay(travel, blast, hitPosition, spec, player, damage)
|
|
end
|
|
Tracer:FireAllClients(model, origin, hitPosition, player.UserId)
|
|
end)
|
|
end
|
|
|
|
return Weapons
|