Advertisement
AlewAlow

Projectile Manager 3

May 3rd, 2024
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local RunService = game:GetService("RunService")
  3. local HttpService = game:GetService("HttpService")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local ServerScriptService = game:GetService("ServerScriptService")
  6.  
  7. local IS_SERVER = RunService:IsServer()
  8.  
  9. local Signal = require(ReplicatedStorage.Libs.Signal)
  10. local Trove = require(ReplicatedStorage.Libs.Trove)
  11.  
  12. local DamageManager = require(ReplicatedStorage.Modules.DamageManager)
  13.  
  14. local ProjectileTypes = require(script.ProjectileTypes)
  15. local ProjectileManager = {}
  16.  
  17. ProjectileManager.ProjectileAdded = Signal.new()
  18. ProjectileManager.Projectiles = {}
  19.  
  20. function ProjectileManager.CreateProjectileId()
  21.     return HttpService:GenerateGUID(false)
  22. end
  23.  
  24. function ProjectileManager.CreateProjectile(projectileProperties)
  25.     if not projectileProperties.Id then
  26.         projectileProperties.Id = ProjectileManager.CreateProjectileId()
  27.     end
  28.    
  29.     if ProjectileManager.Projectiles[projectileProperties.Id] then
  30.         return nil
  31.     end
  32.    
  33.     if not projectileProperties.StartTime then
  34.         projectileProperties.StartTime = workspace:GetServerTimeNow()
  35.     end
  36.    
  37.     if not projectileProperties.ShooterPlayer and projectileProperties.Shooter then
  38.         projectileProperties.ShooterPlayer = Players:GetPlayerFromCharacter(projectileProperties.Shooter)
  39.     end
  40.    
  41.     if not projectileProperties.Shooter and projectileProperties.ShooterPlayer then
  42.         projectileProperties.Shooter = projectileProperties.ShooterPlayer.Character
  43.     end
  44.    
  45.     local projectileClass = ProjectileTypes[projectileProperties.Type]
  46.     local projectile = projectileClass.new(projectileProperties)
  47.    
  48.     projectile.Destroyed:Connect(function()
  49.         ProjectileManager.Projectiles[projectileProperties.Id] = nil
  50.     end)
  51.    
  52.     ProjectileManager.ProjectileAdded:Fire(projectile)
  53.     ProjectileManager.Projectiles[projectileProperties.Id] = projectile
  54.    
  55.     return projectile
  56. end
  57.  
  58. if IS_SERVER then
  59.     ProjectileManager.ProjectileHits = {}
  60.     ProjectileManager.MaxHitTimeDifference = 0.3
  61.     ProjectileManager.MaxHitPositionDifference = 10
  62.    
  63.     ProjectileManager.ProjectileAdded:Connect(function(projectile)
  64.         script.ProjectileCreated:FireAllClients(projectile.Properties)
  65.        
  66.         projectile.Hit:Connect(function(hit)
  67.             if not projectile.Properties.ShooterPlayer then
  68.                 return
  69.             end
  70.            
  71.             ProjectileManager.ProjectileHits[projectile.Properties.Id] = {
  72.                 HitTime = workspace:GetServerTimeNow(),
  73.                 HitPosition = projectile.Position,
  74.                 Properties = projectile.Properties,
  75.             }
  76.         end)
  77.     end)
  78.    
  79.     script.GetProjectiles.OnServerInvoke = function(player)
  80.         local projectilesProperties = {}
  81.        
  82.         for _, projectile in ProjectileManager.Projectiles do
  83.             table.insert(projectilesProperties, projectile.Properties)
  84.         end
  85.        
  86.         return projectilesProperties
  87.     end
  88.    
  89.     script.ProjectileHit.OnServerEvent:Connect(function(player, id, hitPosition, hitTime, hit)
  90.         local hitData = ProjectileManager.ProjectileHits[id]
  91.         if not hitData or hitData.Properties.ShooterPlayer ~= player then
  92.             return
  93.         end
  94.        
  95.         local timeDifference = math.abs(hitData.HitTime - hitTime)
  96.         if timeDifference > ProjectileManager.MaxHitTimeDifference then
  97.             return
  98.         end
  99.        
  100.         local positionDifference = (hitPosition - hitData.HitPosition).Magnitude
  101.         if positionDifference > ProjectileManager.MaxHitPositionDifference then
  102.             return
  103.         end
  104.        
  105.         DamageManager.ApplyDamage(
  106.             hitData.Properties.Damage,
  107.             hit,
  108.             player
  109.         )
  110.     end)
  111. else
  112.     ProjectileManager.ProjectileAdded:Connect(function(projectile)
  113.         projectile.Hit:Connect(function(hit)
  114.             if projectile.Properties.ShooterPlayer ~= Players.LocalPlayer then
  115.                 return
  116.             end
  117.            
  118.             script.ProjectileHit:FireServer(
  119.                 projectile.Properties.Id,
  120.                 projectile.Position,
  121.                 workspace:GetServerTimeNow(),
  122.                 hit
  123.             )
  124.         end)   
  125.     end)
  126.    
  127.     script.ProjectileCreated.OnClientEvent:Connect(ProjectileManager.CreateProjectile)
  128.    
  129.     task.spawn(function()
  130.         local projectilesProperties = script.GetProjectiles:InvokeServer()
  131.         for _, projectileProperties in projectilesProperties do
  132.             ProjectileManager.CreateProjectile(projectileProperties)
  133.         end
  134.     end)
  135. end
  136.  
  137. RunService.Stepped:Connect(function(_, deltaTime)
  138.     for _, projectile in ProjectileManager.Projectiles do
  139.         projectile:Update(deltaTime)
  140.     end
  141. end)
  142.  
  143. return ProjectileManager
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement