Skip to content

Commit

Permalink
Async Main Projectile Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 9, 2024
1 parent 70ccd9d commit 982b3ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,20 @@ public Projectile(int DefinitionId, Vector3D Position, Vector3D Direction, IMyCu
Definition.LiveMethods.OnSpawn?.Invoke(Id, (MyEntity)MyAPIGateway.Entities.GetEntityById(Firer));
}

public void TickUpdate(float delta)
public void AVTickUpdate(float delta)
{
if (QueuedDispose)
return;

if (MyAPIGateway.Session.IsServer)
UpdateAudio();
}

public void AsyncTickUpdate(float delta)
{
if (QueuedDispose)
return;

if ((Definition.PhysicalProjectile.MaxTrajectory != -1 && Definition.PhysicalProjectile.MaxTrajectory < DistanceTravelled) || (Definition.PhysicalProjectile.MaxLifetime != -1 && Definition.PhysicalProjectile.MaxLifetime < Age))
QueueDispose();

Expand Down Expand Up @@ -180,8 +192,6 @@ public void TickUpdate(float delta)
MaxBeamLength = Definition.PhysicalProjectile.MaxTrajectory;
}
}
if (MyAPIGateway.Session.IsServer)
UpdateAudio();
}

public Vector3D NextMoveStep = Vector3D.Zero;
Expand All @@ -204,7 +214,7 @@ public void UpdateFromSerializable(n_SerializableProjectile projectile)
InheritedVelocity = projectile.InheritedVelocity.Value;
if (projectile.Firer.HasValue)
Firer = projectile.Firer.Value;
TickUpdate(delta);
AsyncTickUpdate(delta);
}

public void UpdateHitscan(Vector3D newPosition, Vector3D newDirection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,11 @@ public ProjectileManager()

public void UnloadData()
{
I = null;
isActive = false;
I = null;
DamageHandler.Unload();
}

private void ProjectileTick(Projectile projectile)
{
projectile?.UpdateBoundingBoxCheck(allValidEntities);
}

HashSet<BoundingSphere> allValidEntities = new HashSet<BoundingSphere>();

public void UpdateAfterSimulation()
Expand All @@ -73,7 +68,7 @@ public void UpdateAfterSimulation()
// Tick projectiles
foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run
{
projectile.TickUpdate(deltaTick);
projectile.AVTickUpdate(deltaTick);
if (projectile.QueuedDispose)
QueuedCloseProjectiles.Add(projectile);
}
Expand Down Expand Up @@ -119,21 +114,24 @@ public void UpdateAfterSimulation()
}
}

bool isActive = false;
bool isActive = true;
int ticksReady = 0;
/// <summary>
/// Updates parallel at MAX 60tps, but can run at under that without lagging the game.
/// </summary>
public void UpdateProjectilesParallel()
{
while (isActive)
while (I.isActive)
{
if (ticksReady <= 0)
continue;

float delta = ticksReady / 60f;

foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run
{
projectile.UpdateBoundingBoxCheck(allValidEntities);
projectile.UpdateBoundingBoxCheck(I.allValidEntities);
projectile.AsyncTickUpdate(delta);
}

ticksReady = 0;
Expand Down

0 comments on commit 982b3ad

Please sign in to comment.