Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local HttpService = game:GetService("HttpService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local ServerScriptService = game:GetService("ServerScriptService")
- local IS_SERVER = RunService:IsServer()
- local Signal = require(ReplicatedStorage.Libs.Signal)
- local Trove = require(ReplicatedStorage.Libs.Trove)
- local DamageManager = require(ReplicatedStorage.Modules.DamageManager)
- local ProjectileTypes = require(script.ProjectileTypes)
- local ProjectileManager = {}
- ProjectileManager.ProjectileAdded = Signal.new()
- ProjectileManager.Projectiles = {}
- function ProjectileManager.CreateProjectileId()
- return HttpService:GenerateGUID(false)
- end
- function ProjectileManager.CreateProjectile(projectileProperties)
- if not projectileProperties.Id then
- projectileProperties.Id = ProjectileManager.CreateProjectileId()
- end
- if ProjectileManager.Projectiles[projectileProperties.Id] then
- return nil
- end
- if not projectileProperties.StartTime then
- projectileProperties.StartTime = workspace:GetServerTimeNow()
- end
- if not projectileProperties.ShooterPlayer and projectileProperties.Shooter then
- projectileProperties.ShooterPlayer = Players:GetPlayerFromCharacter(projectileProperties.Shooter)
- end
- if not projectileProperties.Shooter and projectileProperties.ShooterPlayer then
- projectileProperties.Shooter = projectileProperties.ShooterPlayer.Character
- end
- local projectileClass = ProjectileTypes[projectileProperties.Type]
- local projectile = projectileClass.new(projectileProperties)
- projectile.Destroyed:Connect(function()
- ProjectileManager.Projectiles[projectileProperties.Id] = nil
- end)
- ProjectileManager.ProjectileAdded:Fire(projectile)
- ProjectileManager.Projectiles[projectileProperties.Id] = projectile
- return projectile
- end
- if IS_SERVER then
- ProjectileManager.ProjectileHits = {}
- ProjectileManager.MaxHitTimeDifference = 0.3
- ProjectileManager.MaxHitPositionDifference = 10
- ProjectileManager.ProjectileAdded:Connect(function(projectile)
- script.ProjectileCreated:FireAllClients(projectile.Properties)
- projectile.Hit:Connect(function(hit)
- if not projectile.Properties.ShooterPlayer then
- return
- end
- ProjectileManager.ProjectileHits[projectile.Properties.Id] = {
- HitTime = workspace:GetServerTimeNow(),
- HitPosition = projectile.Position,
- Properties = projectile.Properties,
- }
- end)
- end)
- script.GetProjectiles.OnServerInvoke = function(player)
- local projectilesProperties = {}
- for _, projectile in ProjectileManager.Projectiles do
- table.insert(projectilesProperties, projectile.Properties)
- end
- return projectilesProperties
- end
- script.ProjectileHit.OnServerEvent:Connect(function(player, id, hitPosition, hitTime, hit)
- local hitData = ProjectileManager.ProjectileHits[id]
- if not hitData or hitData.Properties.ShooterPlayer ~= player then
- return
- end
- local timeDifference = math.abs(hitData.HitTime - hitTime)
- if timeDifference > ProjectileManager.MaxHitTimeDifference then
- return
- end
- local positionDifference = (hitPosition - hitData.HitPosition).Magnitude
- if positionDifference > ProjectileManager.MaxHitPositionDifference then
- return
- end
- DamageManager.ApplyDamage(
- hitData.Properties.Damage,
- hit,
- player
- )
- end)
- else
- ProjectileManager.ProjectileAdded:Connect(function(projectile)
- projectile.Hit:Connect(function(hit)
- if projectile.Properties.ShooterPlayer ~= Players.LocalPlayer then
- return
- end
- script.ProjectileHit:FireServer(
- projectile.Properties.Id,
- projectile.Position,
- workspace:GetServerTimeNow(),
- hit
- )
- end)
- end)
- script.ProjectileCreated.OnClientEvent:Connect(ProjectileManager.CreateProjectile)
- task.spawn(function()
- local projectilesProperties = script.GetProjectiles:InvokeServer()
- for _, projectileProperties in projectilesProperties do
- ProjectileManager.CreateProjectile(projectileProperties)
- end
- end)
- end
- RunService.Stepped:Connect(function(_, deltaTime)
- for _, projectile in ProjectileManager.Projectiles do
- projectile:Update(deltaTime)
- end
- end)
- return ProjectileManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement