From 08bddf36c30ac01e3cfa4f2a5fefa90ca04695f8 Mon Sep 17 00:00:00 2001 From: Dreitone Date: Mon, 22 Dec 2025 21:36:02 -0600 Subject: [PATCH] small cleanup pr --- .../Mods.CalamityMod.Projectiles.Magic.hjson | 3 +- Utilities/CollisionUtils.cs | 24 +++++-------- Utilities/EntityUtils.cs | 35 +++---------------- Utilities/ItemUtils.cs | 12 +------ Utilities/MiscUtils.cs | 8 ++--- 5 files changed, 19 insertions(+), 63 deletions(-) diff --git a/Localization/en-US/Mods.CalamityMod.Projectiles.Magic.hjson b/Localization/en-US/Mods.CalamityMod.Projectiles.Magic.hjson index f14db8b5fa..51c9ca8cb7 100644 --- a/Localization/en-US/Mods.CalamityMod.Projectiles.Magic.hjson +++ b/Localization/en-US/Mods.CalamityMod.Projectiles.Magic.hjson @@ -59,7 +59,7 @@ CosmicTentacle.DisplayName: Dark Tentacle CryoBlast.DisplayName: Cryo Blast DarkSparkBeam.DisplayName: Dark Spark Beam DarkSparkPrism.DisplayName: Dark Crystal -HyperdeathRiftScepterBeam.DisplayName: Hypderdeath Rift Ripper +HyperdeathRiftScepterBeam.DisplayName: Hyperdeath Rift Ripper DeathValleyDusterProjectile.DisplayName: Death Valley Duster DeathhailBeam.DisplayName: Deathhail Beam EelDrop.DisplayName: Acid Eel Drop @@ -101,7 +101,6 @@ HellfireExplosionFriendly.DisplayName: Explosion HellwingBat.DisplayName: Ashen Hellwing HolyFlame.DisplayName: Holy Flame HolyLaser.DisplayName: Holy Laser -HyperdeathRiftScepterBeam.DisplayName: Hyperdeath Rift Scepter Beam IceBarrageMain.DisplayName: Ice Barrage IceBlock.DisplayName: Ice Barrage Block IceBlockIcicle.DisplayName: Ice Barrage Icicle diff --git a/Utilities/CollisionUtils.cs b/Utilities/CollisionUtils.cs index 1ffaa9d7da..5788363cb0 100644 --- a/Utilities/CollisionUtils.cs +++ b/Utilities/CollisionUtils.cs @@ -41,25 +41,17 @@ namespace CalamityMod /// The hitbox of the target to check. public static bool CircularHitboxCollision(Vector2 centerCheckPosition, float radius, Rectangle targetHitbox) { - // If the center intersects the hitbox, return true immediately - Rectangle center = new Rectangle((int)centerCheckPosition.X, (int)centerCheckPosition.Y, 1, 1); - if (center.Intersects(targetHitbox)) - return true; - float topLeftDistance = Vector2.Distance(centerCheckPosition, targetHitbox.TopLeft()); - float topRightDistance = Vector2.Distance(centerCheckPosition, targetHitbox.TopRight()); - float bottomLeftDistance = Vector2.Distance(centerCheckPosition, targetHitbox.BottomLeft()); - float bottomRightDistance = Vector2.Distance(centerCheckPosition, targetHitbox.BottomRight()); + if (radius <= 0f) + return false; - float distanceToClosestPoint = topLeftDistance; - if (topRightDistance < distanceToClosestPoint) - distanceToClosestPoint = topRightDistance; - if (bottomLeftDistance < distanceToClosestPoint) - distanceToClosestPoint = bottomLeftDistance; - if (bottomRightDistance < distanceToClosestPoint) - distanceToClosestPoint = bottomRightDistance; + float closestX = MathHelper.Clamp(centerCheckPosition.X, targetHitbox.Left, targetHitbox.Right); + float closestY = MathHelper.Clamp(centerCheckPosition.Y, targetHitbox.Top, targetHitbox.Bottom); - return distanceToClosestPoint <= radius; + float dx = centerCheckPosition.X - closestX; + float dy = centerCheckPosition.Y - closestY; + + return (dx * dx + dy * dy) <= (radius * radius); } /// diff --git a/Utilities/EntityUtils.cs b/Utilities/EntityUtils.cs index ae032240f7..6d02c92f68 100644 --- a/Utilities/EntityUtils.cs +++ b/Utilities/EntityUtils.cs @@ -69,60 +69,35 @@ namespace CalamityMod /// /// whoAmI index to check /// true if index is in valid range [0 <= index < Main.maxNPCs] - public static bool IndexInRange(this NPC[] _, int index) - { - if (index >= 0) - return index < Main.maxNPCs; - return false; - } + public static bool IndexInRange(this NPC[] _, int index) => (uint)index < Main.maxNPCs; /// /// Fallback method for Main.player.IndexInRange which provide accurate range check (0 <= index < Main.maxPlayers) /// /// whoAmI index to check /// true if index is in valid range [0 <= index < Main.maxPlayers] - public static bool IndexInRange(this Player[] _, int index) - { - if (index >= 0) - return index < Main.maxPlayers; - return false; - } + public static bool IndexInRange(this Player[] _, int index) => (uint)index < Main.maxPlayers; /// /// Fallback method for Main.projectile.IndexInRange which provide accurate range check (0 <= index < Main.maxProjectiles) /// /// whoAmI index to check /// true if index is in valid range [0 <= index < Main.maxProjectiles] - public static bool IndexInRange(this Projectile[] _, int index) - { - if (index >= 0) - return index < Main.maxProjectiles; - return false; - } + public static bool IndexInRange(this Projectile[] _, int index) => (uint)index < Main.maxProjectiles; /// /// Fallback method for Main.gore.IndexInRange which provide accurate range check (0 <= index < Main.maxGore) /// /// whoAmI index to check /// true if index is in valid range [0 <= index < Main.maxGore] - public static bool IndexInRange(this Gore[] _, int index) - { - if (index >= 0) - return index < Main.maxGore; - return false; - } + public static bool IndexInRange(this Gore[] _, int index) => (uint)index < Main.maxGore; /// /// Fallback method for Main.npc.IndexInRange which provide accurate range check (0 <= index < Main.maxDust) /// /// whoAmI index to check /// true if index is in valid range [0 <= index < Main.maxDust] - public static bool IndexInRange(this Dust[] _, int index) - { - if (index >= 0) - return index < Main.maxDust; - return false; - } + public static bool IndexInRange(this Dust[] _, int index) => (uint)index < Main.maxDust; #endregion } } diff --git a/Utilities/ItemUtils.cs b/Utilities/ItemUtils.cs index 5cf69ae0cc..3c29818724 100644 --- a/Utilities/ItemUtils.cs +++ b/Utilities/ItemUtils.cs @@ -90,17 +90,7 @@ namespace CalamityMod public static void FindAndReplaceAll(this List tooltips, string replacedKey, string newKey) { foreach (TooltipLine line in tooltips) - { - if (!line.Text.Contains(replacedKey)) - continue; - //loops only 100 times at max per tooltip line to prevent any infinite loops - for (var i = 0; i < 100; i++) - { - line.Text = line.Text.Replace(replacedKey, newKey); - if (!line.Text.Contains(replacedKey)) - break; - } - } + line.Text = line.Text.Replace(replacedKey, newKey); } /// diff --git a/Utilities/MiscUtils.cs b/Utilities/MiscUtils.cs index e9829beb6f..d1eab75448 100644 --- a/Utilities/MiscUtils.cs +++ b/Utilities/MiscUtils.cs @@ -140,7 +140,7 @@ namespace CalamityMod /// The base type of the collection. /// The collection. /// The type to search for. - public static bool ContainsType(this IEnumerable collection, Type type) => collection.Any(entry => entry.GetType() == type.GetType()); + public static bool ContainsType(this IEnumerable collection, Type type) => collection.Any(entry => entry.GetType() == type); /// /// Calculates the sound volume and panning for a sound which is played at the specified location in the game world.
@@ -156,7 +156,7 @@ namespace CalamityMod if (soundPos.X == -1f || soundPos.Y == -1f) volume = 1f; - else if (WorldGen.gen || Main.dedServ || Main.dedServ) + else if (WorldGen.gen || Main.dedServ) volume = 0f; else { @@ -273,12 +273,12 @@ namespace CalamityMod for (int i = 0; i < originalText.Length; i++) { for (int j = 0; j < 37; j++) - { + { if (TextKeys[j] == originalText[i]) { morseText += MorseKeys[j]; break; - } + } } }