Skip to content

Commit

Permalink
More collision check's
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDorob committed Oct 10, 2023
1 parent d209f82 commit 832706c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
27 changes: 26 additions & 1 deletion Source/CombatExtended/CombatExtended/Projectiles/ProjectileCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,28 @@ private bool CheckCellForCollision(IntVec3 cell)
// Check for collision
if (thing == intendedTargetThing || def.projectile.alwaysFreeIntercept || thing.Position.DistanceTo(OriginIV3) >= minCollisionDistance)
{
if (BlockerRegistry.CheckForCollisionBetweenCallback(this, LastPos, thing.TrueCenter()))
{
return true;
}
var lastPosIV3 = LastPos.ToIntVec3();
var newPosIV3 = thing.TrueCenter().ToIntVec3();
// Iterate through all cells between the last and the THING
// INCLUDING[!!!] THE LAST AND NEW POSITIONS!
var cells = GenSight.PointsOnLineOfSight(lastPosIV3, newPosIV3).Union(new[] { lastPosIV3, newPosIV3 }).Distinct().OrderBy(x => (x.ToVector3Shifted() - LastPos).MagnitudeHorizontalSquared());
foreach (var _cell in cells)
{
bool colided = false;
colided = BlockerRegistry.CheckCellForCollisionCallback(this, _cell, launcher);
if (Controller.settings.DebugDrawInterceptChecks)
{
Map.debugDrawer.FlashCell(_cell, 1, "o");
}
if (colided)
{
return true;
}
}
if (TryCollideWith(thing))
{
return true;
Expand Down Expand Up @@ -980,9 +1002,12 @@ private bool TryCollideWith(Thing thing)
}
}

if (BlockerRegistry.BeforeCollideWithCallback(this, thing))
{
return true;
}
ExactPosition = point;
landed = true;

if (Controller.settings.DebugDrawInterceptChecks)
{
MoteMakerCE.ThrowText(thing.Position.ToVector3Shifted(), thing.Map, "x", Color.red);
Expand Down
28 changes: 27 additions & 1 deletion Source/CombatExtended/Compatibility/BlockerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public static class BlockerRegistry
private static List<Func<ProjectileCE, Vector3, Vector3, bool>> checkForCollisionBetweenCallbacks;
private static List<Func<ProjectileCE, IntVec3, Thing, bool>> checkCellForCollisionCallbacks;
private static List<Func<ProjectileCE, Thing, bool>> impactSomethingCallbacks;
private static List<Func<ProjectileCE, Thing, bool>> beforeCollideWithCallbacks;

private static void Enable()
{
enabled = true;
checkForCollisionBetweenCallbacks = new List<Func<ProjectileCE, Vector3, Vector3, bool>>();
impactSomethingCallbacks = new List<Func<ProjectileCE, Thing, bool>>();
checkCellForCollisionCallbacks = new List<Func<ProjectileCE, IntVec3, Thing, bool>>();
beforeCollideWithCallbacks = new List<Func<ProjectileCE, Thing, bool>>();
}

public static void RegisterCheckForCollisionBetweenCallback(Func<ProjectileCE, Vector3, Vector3, bool> f)
Expand Down Expand Up @@ -50,7 +52,14 @@ public static void RegisterImpactSomethingCallback(Func<ProjectileCE, Thing, boo
}
impactSomethingCallbacks.Add(f);
}

public static void RegisterBeforeCollideWithCallback(Func<ProjectileCE, Thing, bool> f)
{
if (!enabled)
{
Enable();
}
beforeCollideWithCallbacks.Add(f);
}
public static bool CheckForCollisionBetweenCallback(ProjectileCE projectile, Vector3 from, Vector3 to)
{
if (!enabled)
Expand Down Expand Up @@ -99,6 +108,22 @@ public static bool ImpactSomethingCallback(ProjectileCE projectile, Thing launch
return false;
}

public static bool BeforeCollideWithCallback(ProjectileCE projectile, Thing collideWith)
{
if (!enabled)
{
return false;
}
foreach (var cb in beforeCollideWithCallbacks)
{
if (cb(projectile, collideWith))
{
return true;
}
}
return false;
}

public static Vector3 GetExactPosition(Vector3 origin, Vector3 curPosition, Vector3 shieldPosition, float radiusSq)
{
Vector3 velocity = curPosition - origin;
Expand All @@ -114,5 +139,6 @@ public static Vector3 GetExactPosition(Vector3 origin, Vector3 curPosition, Vect
float scalar = (float)(2 * c / (-b + Math.Sqrt(det)));
return velocity * scalar + origin;
}

}
}

0 comments on commit 832706c

Please sign in to comment.