Laser collision fixes and more precise turret line of sight checks
Projectiles using BaseLaserbeamProjectile no longer use a janky vanilla function, and turrets use a more precise can hit in line, because the vanilla function is a straight up lie
This commit is contained in:
1 parent
a4667e0cbf
commit
03765ab878
9 files changed
+161
-14
No files matched your search
@@ -110,12 +110,9 @@ namespace CalamityMod.Projectiles.BaseProjectiles
|
||||
/// <summary>
|
||||
/// Calculates the laser length while taking tiles into account.
|
||||
/// </summary>
|
||||
/// <param name="samplePointCount">The amount of samples the ray uses. The higher this is, the more precision, but also more calculations done.</param>
|
||||
public float DetermineLaserLength_CollideWithTiles(int samplePointCount)
|
||||
public float DetermineLaserLength_CollideWithTiles()
|
||||
{
|
||||
float[] laserLengthSamplePoints = new float[samplePointCount];
|
||||
Collision.LaserScan(Projectile.Center, Projectile.velocity, Projectile.scale, MaxLaserLength, laserLengthSamplePoints);
|
||||
return laserLengthSamplePoints.Average();
|
||||
return CalamityUtils.PreciseDistanceToTileCollisionHit(Projectile.Center, Projectile.velocity.ToRotation(), MaxLaserLength);
|
||||
}
|
||||
|
||||
protected internal void DrawBeamWithColor(Color beamColor, float scale, int startFrame = 0, int middleFrame = 0, int endFrame = 0)
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace CalamityMod.Projectiles.DraedonsArsenal
|
||||
if (Projectile.timeLeft == 12)
|
||||
{
|
||||
Vector2 beamVector = Projectile.velocity;
|
||||
float beamLength = DetermineLaserLength_CollideWithTiles(12);
|
||||
float beamLength = DetermineLaserLength_CollideWithTiles();
|
||||
|
||||
//Rapid dust
|
||||
int dustCount = Main.rand.Next(10, 30);
|
||||
@@ -101,7 +101,7 @@ namespace CalamityMod.Projectiles.DraedonsArsenal
|
||||
|
||||
public override void DetermineScale() => Projectile.scale = 1f;
|
||||
|
||||
public override float DetermineLaserLength() => DetermineLaserLength_CollideWithTiles(5);
|
||||
public override float DetermineLaserLength() => DetermineLaserLength_CollideWithTiles();
|
||||
|
||||
public override bool ShouldUpdatePosition() => false;
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace CalamityMod.Projectiles.Melee
|
||||
|
||||
public override float DetermineLaserLength()
|
||||
{
|
||||
return DetermineLaserLength_CollideWithTiles(5);
|
||||
return DetermineLaserLength_CollideWithTiles();
|
||||
}
|
||||
|
||||
public override bool PreAI()
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace CalamityMod.Projectiles.Ranged
|
||||
public override float DetermineLaserLength()
|
||||
{
|
||||
if (Projectile.penetrate == 100)
|
||||
return DetermineLaserLength_CollideWithTiles(8);
|
||||
return DetermineLaserLength_CollideWithTiles();
|
||||
return LaserLength;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace CalamityMod.Projectiles.Ranged
|
||||
{
|
||||
|
||||
Vector2 beamVector = Projectile.velocity;
|
||||
float beamLength = DetermineLaserLength_CollideWithTiles(12);
|
||||
float beamLength = DetermineLaserLength_CollideWithTiles();
|
||||
|
||||
//Rapid dust
|
||||
int dustCount = Main.rand.Next(10, 30);
|
||||
@@ -77,7 +77,7 @@ namespace CalamityMod.Projectiles.Ranged
|
||||
|
||||
public override void DetermineScale() => Projectile.scale = Projectile.timeLeft / Lifetime * MaxScale;
|
||||
|
||||
public override float DetermineLaserLength() => DetermineLaserLength_CollideWithTiles(5);
|
||||
public override float DetermineLaserLength() => DetermineLaserLength_CollideWithTiles();
|
||||
|
||||
public override bool ShouldUpdatePosition() => false;
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace CalamityMod.Projectiles.Turret
|
||||
|
||||
public override float DetermineLaserLength()
|
||||
{
|
||||
return DetermineLaserLength_CollideWithTiles(8);
|
||||
return DetermineLaserLength_CollideWithTiles();
|
||||
}
|
||||
|
||||
public override bool PreDraw(ref Color lightColor)
|
||||
|
||||
@@ -125,9 +125,10 @@ namespace CalamityMod.TileEntities
|
||||
// If the turret is too far off its mark or hasn't been around for long enough, don't fire at all.
|
||||
float deltaAngle = MathHelper.WrapAngle(Angle - targetAngle);
|
||||
bool angleCloseEnough = Math.Abs(deltaAngle) <= MaxTargetAngleDeviance;
|
||||
if (!angleCloseEnough || FiringTime < FiringStartupDelay || !Collision.CanHitLine(turretPos, 1, 1, targetPos, 1, 1))
|
||||
if (!angleCloseEnough || FiringTime < FiringStartupDelay || !CalamityUtils.PreciseCanHitInLine(turretPos, (targetPos - turretPos).ToRotation(), (targetPos - turretPos).Length()))
|
||||
return;
|
||||
|
||||
|
||||
// Don't shoot every frame, but sync up the firing cadence to the startup delay.
|
||||
if ((FiringTime - FiringStartupDelay) % FiringUseTime == 0)
|
||||
Shoot(turretPos);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Terraria;
|
||||
using Terraria.ID;
|
||||
|
||||
namespace CalamityMod
|
||||
{
|
||||
@@ -91,5 +92,153 @@ namespace CalamityMod
|
||||
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines the distance required before a ray in a given direction from a given starting position hits solid tiles, taking slopes into account.
|
||||
/// </summary>
|
||||
/// <param name="start">The point to check from.</param>
|
||||
/// <param name="rotation">The direction in which tiles are checked.</param>
|
||||
/// <param name="length">How far in the direction that will be checked.</param>
|
||||
/// <param name="step">How many units moved forward each loop. Greater = less precise.</param>
|
||||
/// <returns>The length until the first collision detected. Returns input length if no collision occurs.</returns>
|
||||
public static float PreciseDistanceToTileCollisionHit(Vector2 start, float rotation, float length, float step = 1)
|
||||
{
|
||||
Vector2 unitVect = rotation.ToRotationVector2();
|
||||
Vector2 end = unitVect * length;
|
||||
|
||||
if (length < 1f)
|
||||
{
|
||||
Point endWorldPos = end.ToTileCoordinates();
|
||||
return ParanoidTileRetrieval(endWorldPos.X, endWorldPos.Y).IsTileSolid() ? 0 : length;
|
||||
}
|
||||
|
||||
Vector2 currentPos = start;
|
||||
Point lastAirPos = new Point(-1, -1);
|
||||
for (float i = 0; i < length; i += step)
|
||||
{
|
||||
currentPos += unitVect * step;
|
||||
|
||||
Point tilePos = currentPos.ToTileCoordinates();
|
||||
|
||||
if (tilePos == lastAirPos)
|
||||
continue;
|
||||
|
||||
if (!WorldGen.InWorld(tilePos.X, tilePos.Y))
|
||||
continue;
|
||||
|
||||
Tile tile = Main.tile[tilePos.X, tilePos.Y];
|
||||
if (!tile.IsTileSolid())
|
||||
{
|
||||
lastAirPos = tilePos;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tile.Slope == SlopeType.Solid && !tile.IsHalfBlock)
|
||||
return (currentPos - start).Length();
|
||||
|
||||
Vector2 tileWorldPos = new Vector2(tilePos.X * 16, tilePos.Y * 16);
|
||||
Vector2 currentPosInTile = currentPos - tileWorldPos;
|
||||
if (tile.IsHalfBlock)
|
||||
{
|
||||
if (currentPosInTile.Y >= 8f)
|
||||
return (currentPos - start).Length();
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeDownLeft)
|
||||
{
|
||||
if (currentPosInTile.X <= currentPosInTile.Y)
|
||||
return (currentPos - start).Length();
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeDownRight)
|
||||
{
|
||||
if ((16 - currentPosInTile.X) <= currentPosInTile.Y)
|
||||
return (currentPos - start).Length();
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeUpLeft)
|
||||
{
|
||||
if (currentPosInTile.X <= (16 - currentPosInTile.Y))
|
||||
return (currentPos - start).Length();
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeUpRight)
|
||||
{
|
||||
if (currentPosInTile.X >= currentPosInTile.Y)
|
||||
return (currentPos - start).Length();
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines the distance required before a ray in a given direction from a given starting position hits solid tiles, taking slopes into account.
|
||||
/// </summary>
|
||||
/// <param name="start">The point to check from.</param>
|
||||
/// <param name="rotation">The direction in which tiles are checked.</param>
|
||||
/// <param name="length">How far in the direction that will be checked.</param>
|
||||
/// <param name="step">How many units moved forward each loop. Greater = less precise.</param>
|
||||
/// <returns>False if there is a tile collision before checking the full length of the line.</returns>
|
||||
public static bool PreciseCanHitInLine(Vector2 start, float rotation, float length, float step = 1)
|
||||
{
|
||||
Vector2 unitVect = rotation.ToRotationVector2();
|
||||
Vector2 end = unitVect * length;
|
||||
|
||||
if (length < 1f)
|
||||
{
|
||||
Point endWorldPos = end.ToTileCoordinates();
|
||||
return !ParanoidTileRetrieval(endWorldPos.X, endWorldPos.Y).IsTileSolid();
|
||||
}
|
||||
|
||||
Vector2 currentPos = start;
|
||||
Point lastAirPos = new Point(-1, -1);
|
||||
for (float i = 0; i < length; i += step)
|
||||
{
|
||||
currentPos += unitVect * step;
|
||||
|
||||
Point tilePos = currentPos.ToTileCoordinates();
|
||||
|
||||
if (tilePos == lastAirPos)
|
||||
continue;
|
||||
|
||||
if (!WorldGen.InWorld(tilePos.X, tilePos.Y))
|
||||
continue;
|
||||
|
||||
Tile tile = Main.tile[tilePos.X, tilePos.Y];
|
||||
if (!tile.IsTileSolid())
|
||||
{
|
||||
lastAirPos = tilePos;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tile.Slope == SlopeType.Solid && !tile.IsHalfBlock)
|
||||
return false;
|
||||
|
||||
Vector2 tileWorldPos = new Vector2(tilePos.X * 16, tilePos.Y * 16);
|
||||
Vector2 currentPosInTile = currentPos - tileWorldPos;
|
||||
if (tile.IsHalfBlock)
|
||||
{
|
||||
if (currentPosInTile.Y >= 8f)
|
||||
return false;
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeDownLeft)
|
||||
{
|
||||
if (currentPosInTile.X <= currentPosInTile.Y)
|
||||
return false;
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeDownRight)
|
||||
{
|
||||
if ((16 - currentPosInTile.X) <= currentPosInTile.Y)
|
||||
return false;
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeUpLeft)
|
||||
{
|
||||
if (currentPosInTile.X <= (16 - currentPosInTile.Y))
|
||||
return false;
|
||||
}
|
||||
else if (tile.Slope == SlopeType.SlopeUpRight)
|
||||
{
|
||||
if (currentPosInTile.X >= currentPosInTile.Y)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -702,7 +702,7 @@ namespace CalamityMod
|
||||
/// Determines if a tile is solid based on whether it's active and not actuated or if the tile is solid. This will not count platforms and other non-solid ground tiles
|
||||
/// </summary>
|
||||
/// <param name="tile">The tile to check.</param>
|
||||
public static bool IsTileSolid(this Tile tile) => tile != null && tile.HasUnactuatedTile && Main.tileSolid[tile.TileType] && !TileID.Sets.Platforms[tile.TileType];
|
||||
public static bool IsTileSolid(this Tile tile) => tile != null && tile.HasUnactuatedTile && Main.tileSolid[tile.TileType] && !Main.tileSolidTop[tile.TileType];
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a tile is "full" based on if the tile is solid. This will count platforms and actuated tiles but no other non-solid ground tiles.
|
||||
|
||||
Reference in new issue
Block a user