forked from StarCoreSE/Orrery-Combat-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
40 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...Framework - Heart Module/Data/Scripts/HeartModule/Projectiles/ParallelProjectileThread.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Heart_Module.Data.Scripts.HeartModule.ExceptionHandler; | ||
using ParallelTasks; | ||
using Sandbox.ModAPI; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Heart_Module.Data.Scripts.HeartModule.Projectiles | ||
{ | ||
internal class ParallelProjectileThread | ||
{ | ||
#region variables | ||
|
||
Task thisTask; | ||
/// <summary> | ||
/// Thread-safe buffer array for active projectiles; cannot be written to while the thread is active. | ||
/// </summary> | ||
Projectile[] ActiveProjectiles = new Projectile[0]; | ||
/// <summary> | ||
/// Thread safe buffer list for projectiles to close. | ||
/// </summary> | ||
List<Projectile> ProjectilesToClose = new List<Projectile>(); | ||
|
||
public float DeltaTick = 0; | ||
|
||
#endregion | ||
|
||
public ParallelProjectileThread() | ||
{ | ||
thisTask = MyAPIGateway.Parallel.StartBackground(DoWork); | ||
HeartLog.Log("Started ParallelProjectileThread!"); | ||
} | ||
|
||
#region methods | ||
|
||
public void Update() | ||
{ | ||
DeltaTick += ProjectileManager.DeltaTick; | ||
|
||
if (thisTask.IsComplete) | ||
{ | ||
// Update thread-safe buffer lists | ||
ActiveProjectiles = ProjectileManager.I.ActiveProjectiles.Values.ToArray(); | ||
ProjectileManager.I.QueuedCloseProjectiles.AddRange(ProjectilesToClose); | ||
ProjectilesToClose.Clear(); | ||
thisTask = MyAPIGateway.Parallel.StartBackground(DoWork); | ||
} | ||
} | ||
|
||
public void Close() | ||
{ | ||
HeartLog.Log("-------------------------------------------"); | ||
HeartLog.Log(" Closing ParallelProjectileThread..."); | ||
if (!thisTask.IsComplete) | ||
thisTask.Wait(true); | ||
HeartLog.Log(" Closed ParallelProjectileThread."); | ||
HeartLog.Log("-------------------------------------------"); | ||
} | ||
|
||
void DoWork() | ||
{ | ||
MyAPIGateway.Parallel.ForEach(ActiveProjectiles, UpdateSingleProjectile); | ||
DeltaTick = 0; | ||
} | ||
|
||
void UpdateSingleProjectile(Projectile projectile) | ||
{ | ||
projectile.TickUpdate(DeltaTick); | ||
|
||
if (projectile.QueuedDispose) | ||
ProjectilesToClose.Add(projectile); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters