From 51412fcfae2adcfb22b2a211a5aacd2327fd49e8 Mon Sep 17 00:00:00 2001 From: DominicKarma <51094361+DominicKarma@users.noreply.github.com> Date: Mon, 1 Feb 2021 21:16:03 -0600 Subject: [PATCH] Moved a few utility methods --- NPCs/Providence/Providence.cs | 2 +- Projectiles/Boss/DoGTeleportRift.cs | 2 +- Projectiles/Boss/HolyAura.cs | 6 +-- Projectiles/Boss/HolyBurnOrb.cs | 2 +- Projectiles/Boss/HolyLight.cs | 4 +- Projectiles/Boss/HolySpear.cs | 2 +- Utilities/MathematicalUtils.cs | 11 +++++ Utilities/MiscUtils.cs | 74 ----------------------------- Utilities/NPCUtils.cs | 16 +++++++ Utilities/PlayerUtils.cs | 16 +++++++ 10 files changed, 52 insertions(+), 83 deletions(-) diff --git a/NPCs/Providence/Providence.cs b/NPCs/Providence/Providence.cs index 37dbebf18b..762093057b 100644 --- a/NPCs/Providence/Providence.cs +++ b/NPCs/Providence/Providence.cs @@ -690,7 +690,7 @@ namespace CalamityMod.NPCs.Providence int dustAmt = (int)MathHelper.Lerp(4f, 8f, calamityGlobalNPC.newAI[3] / spawnAnimationTime); for (int m = 0; m < dustAmt; m++) { - float fade = MathHelper.Lerp(1.3f, 0.7f, npc.Opacity) * CalamityUtils.GetLerpValue(0f, 120f, calamityGlobalNPC.newAI[3], clamped: true); + float fade = MathHelper.Lerp(1.3f, 0.7f, npc.Opacity) * Utils.InverseLerp(0f, 120f, calamityGlobalNPC.newAI[3], clamped: true); Color newColor = Main.hslToRgb(calamityGlobalNPC.newAI[3] / 180f, 1f, 0.5f); if (!nightTime) diff --git a/Projectiles/Boss/DoGTeleportRift.cs b/Projectiles/Boss/DoGTeleportRift.cs index 7acb0995d6..23b9e7e0af 100644 --- a/Projectiles/Boss/DoGTeleportRift.cs +++ b/Projectiles/Boss/DoGTeleportRift.cs @@ -57,7 +57,7 @@ namespace CalamityMod.Projectiles.Boss public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { - float lerpMult = CalamityUtils.GetLerpValue(15f, 30f, projectile.timeLeft, clamped: true) * CalamityUtils.GetLerpValue(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * (MathHelper.Pi * 2f) * 3f)) * 0.8f; + float lerpMult = Utils.InverseLerp(15f, 30f, projectile.timeLeft, clamped: true) * Utils.InverseLerp(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * (MathHelper.Pi * 2f) * 3f)) * 0.8f; Texture2D texture = Main.projectileTexture[projectile.type]; Vector2 drawPos = projectile.Center - Main.screenPosition + new Vector2(0f, projectile.gfxOffY); diff --git a/Projectiles/Boss/HolyAura.cs b/Projectiles/Boss/HolyAura.cs index 19c517dd69..4a7a81160d 100644 --- a/Projectiles/Boss/HolyAura.cs +++ b/Projectiles/Boss/HolyAura.cs @@ -39,9 +39,9 @@ namespace CalamityMod.Projectiles.Boss float[] hue = new float[drawnAmt]; float[] size = new float[drawnAmt]; int totalTime = 210; - float colorChangeAmt = CalamityUtils.GetLerpValue(0f, 60f, projectile.timeLeft, true) * CalamityUtils.GetLerpValue(totalTime, totalTime - 60, projectile.timeLeft, true); - float colorChangeAmt2 = CalamityUtils.GetLerpValue(0f, 60f, projectile.timeLeft, true) * CalamityUtils.GetLerpValue(totalTime, 90f, projectile.timeLeft, true); - colorChangeAmt2 = CalamityUtils.GetLerpValue(0.2f, 0.5f, colorChangeAmt2, true); + float colorChangeAmt = Utils.InverseLerp(0f, 60f, projectile.timeLeft, true) * Utils.InverseLerp(totalTime, totalTime - 60, projectile.timeLeft, true); + float colorChangeAmt2 = Utils.InverseLerp(0f, 60f, projectile.timeLeft, true) * Utils.InverseLerp(totalTime, 90f, projectile.timeLeft, true); + colorChangeAmt2 = Utils.InverseLerp(0.2f, 0.5f, colorChangeAmt2, true); float sizeScale = 0.8f; float sizeScalar = (1f - sizeScale) / drawnAmt; float yPosOffset = 60f; diff --git a/Projectiles/Boss/HolyBurnOrb.cs b/Projectiles/Boss/HolyBurnOrb.cs index f15acd18ba..b7f8e2842f 100644 --- a/Projectiles/Boss/HolyBurnOrb.cs +++ b/Projectiles/Boss/HolyBurnOrb.cs @@ -72,7 +72,7 @@ namespace CalamityMod.Projectiles.Boss public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { - float lerpMult = CalamityUtils.GetLerpValue(15f, 30f, projectile.timeLeft, clamped: true) * CalamityUtils.GetLerpValue(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * (MathHelper.Pi * 2f) * 3f)) * 0.8f; + float lerpMult = Utils.InverseLerp(15f, 30f, projectile.timeLeft, clamped: true) * Utils.InverseLerp(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * (MathHelper.Pi * 2f) * 3f)) * 0.8f; Texture2D texture = Main.projectileTexture[projectile.type]; Vector2 drawPos = projectile.Center - Main.screenPosition + new Vector2(0f, projectile.gfxOffY); diff --git a/Projectiles/Boss/HolyLight.cs b/Projectiles/Boss/HolyLight.cs index 8ecaed2769..a4d2aa4444 100644 --- a/Projectiles/Boss/HolyLight.cs +++ b/Projectiles/Boss/HolyLight.cs @@ -73,14 +73,14 @@ namespace CalamityMod.Projectiles.Boss Color color34 = color33; Vector2 origin5 = value.Size() / 2f; Color color35 = color33 * 0.5f; - float num162 = CalamityUtils.GetLerpValue(15f, 30f, projectile.timeLeft, clamped: true) * CalamityUtils.GetLerpValue(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * ((float)Math.PI * 2f) * 3f)) * 0.8f; + float num162 = Utils.InverseLerp(15f, 30f, projectile.timeLeft, clamped: true) * Utils.InverseLerp(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * ((float)Math.PI * 2f) * 3f)) * 0.8f; Vector2 vector29 = new Vector2(0.5f, 1f) * num162; Vector2 vector30 = new Vector2(0.5f, 1f) * num162; color34 *= num162; color35 *= num162; int num163 = 0; - Vector2 position3 = vector28 + projectile.velocity.SafeNormalize(Vector2.Zero) * CalamityUtils.GetLerpValue(0.5f, 1f, projectile.localAI[0] / 60f, clamped: true) * num163; + Vector2 position3 = vector28 + projectile.velocity.SafeNormalize(Vector2.Zero) * Utils.InverseLerp(0.5f, 1f, projectile.localAI[0] / 60f, clamped: true) * num163; SpriteEffects spriteEffects = SpriteEffects.None; if (projectile.spriteDirection == -1) diff --git a/Projectiles/Boss/HolySpear.cs b/Projectiles/Boss/HolySpear.cs index d3abcc4aa5..cfeb65ae50 100644 --- a/Projectiles/Boss/HolySpear.cs +++ b/Projectiles/Boss/HolySpear.cs @@ -137,7 +137,7 @@ namespace CalamityMod.Projectiles.Boss Color color34 = color33; Vector2 origin5 = value.Size() / 2f; Color color35 = color33 * 0.5f; - float num162 = CalamityUtils.GetLerpValue(15f, 30f, projectile.timeLeft, clamped: true) * CalamityUtils.GetLerpValue(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * ((float)Math.PI * 2f) * 3f)) * 0.8f; + float num162 = Utils.InverseLerp(15f, 30f, projectile.timeLeft, clamped: true) * Utils.InverseLerp(240f, 200f, projectile.timeLeft, clamped: true) * (1f + 0.2f * (float)Math.Cos(Main.GlobalTime % 30f / 0.5f * ((float)Math.PI * 2f) * 3f)) * 0.8f; Vector2 vector29 = new Vector2(1f, 1.5f) * num162; Vector2 vector30 = new Vector2(0.5f, 1f) * num162; color34 *= num162; diff --git a/Utilities/MathematicalUtils.cs b/Utilities/MathematicalUtils.cs index 0bc2590e81..884bbe3266 100644 --- a/Utilities/MathematicalUtils.cs +++ b/Utilities/MathematicalUtils.cs @@ -19,5 +19,16 @@ namespace CalamityMod /// The first vector. /// The second vector. public static float AngleBetween(this Vector2 v1, Vector2 v2) => (float)Math.Acos(Vector2.Dot(v1.SafeNormalize(Vector2.Zero), v2.SafeNormalize(Vector2.Zero))); + + // REMOVE THIS IN CALAMITY 1.4, it's a 1.4 World.cs function. + // Due to its temporary state, this method will not receive an XML documentation comment. + public static Rectangle ClampToWorld(Rectangle tileRectangle) + { + int num = Math.Max(0, Math.Min(tileRectangle.Left, Main.maxTilesX)); + int num2 = Math.Max(0, Math.Min(tileRectangle.Top, Main.maxTilesY)); + int num3 = Math.Max(0, Math.Min(tileRectangle.Right, Main.maxTilesX)); + int num4 = Math.Max(0, Math.Min(tileRectangle.Bottom, Main.maxTilesY)); + return new Rectangle(num, num2, num3 - num, num4 - num2); + } } } diff --git a/Utilities/MiscUtils.cs b/Utilities/MiscUtils.cs index d25f6039af..4f8815c2a0 100644 --- a/Utilities/MiscUtils.cs +++ b/Utilities/MiscUtils.cs @@ -213,38 +213,6 @@ namespace CalamityMod list.Add(type); } - public static void Inflict246DebuffsNPC(NPC target, int buff, float timeBase = 2f) - { - if (Main.rand.NextBool(4)) - { - target.AddBuff(buff, SecondsToFrames(timeBase * 3f), false); - } - else if (Main.rand.NextBool(2)) - { - target.AddBuff(buff, SecondsToFrames(timeBase * 2f), false); - } - else - { - target.AddBuff(buff, SecondsToFrames(timeBase), false); - } - } - - public static void Inflict246DebuffsPvp(Player target, int buff, float timeBase = 2f) - { - if (Main.rand.NextBool(4)) - { - target.AddBuff(buff, SecondsToFrames(timeBase * 3f), false); - } - else if (Main.rand.NextBool(2)) - { - target.AddBuff(buff, SecondsToFrames(timeBase * 2f), false); - } - else - { - target.AddBuff(buff, SecondsToFrames(timeBase), false); - } - } - public static int SecondsToFrames(int seconds) => seconds * 60; public static int SecondsToFrames(float seconds) => (int)(seconds * 60); @@ -289,38 +257,6 @@ namespace CalamityMod public static bool WithinBounds(this int index, int cap) => index >= 0 && index < cap; - // REMOVE THIS IN CALAMITY 1.4, it's a 1.4 Main.cs function - public static float GetLerpValue(float from, float to, float t, bool clamped = false) - { - if (clamped) - { - if (from < to) - { - if (t < from) - { - return 0f; - } - if (t > to) - { - return 1f; - } - } - else - { - if (t < to) - { - return 1f; - } - if (t > from) - { - return 0f; - } - } - } - return (t - from) / (to - from); - } - - /// /// Clamps the distance between vectors via normalization. /// /// The starting point. @@ -333,15 +269,5 @@ namespace CalamityMod end = start + Vector2.Normalize(end - start) * maxDistance; } } - - // REMOVE THIS IN CALAMITY 1.4, it's a 1.4 World.cs function - public static Rectangle ClampToWorld(Rectangle tileRectangle) - { - int num = Math.Max(0, Math.Min(tileRectangle.Left, Main.maxTilesX)); - int num2 = Math.Max(0, Math.Min(tileRectangle.Top, Main.maxTilesY)); - int num3 = Math.Max(0, Math.Min(tileRectangle.Right, Main.maxTilesX)); - int num4 = Math.Max(0, Math.Min(tileRectangle.Bottom, Main.maxTilesY)); - return new Rectangle(num, num2, num3 - num, num4 - num2); - } } } diff --git a/Utilities/NPCUtils.cs b/Utilities/NPCUtils.cs index b68ed56409..c797df598d 100644 --- a/Utilities/NPCUtils.cs +++ b/Utilities/NPCUtils.cs @@ -407,6 +407,22 @@ namespace CalamityMod } } + public static void Inflict246DebuffsNPC(NPC target, int buff, float timeBase = 2f) + { + if (Main.rand.NextBool(4)) + { + target.AddBuff(buff, SecondsToFrames(timeBase * 3f), false); + } + else if (Main.rand.NextBool(2)) + { + target.AddBuff(buff, SecondsToFrames(timeBase * 2f), false); + } + else + { + target.AddBuff(buff, SecondsToFrames(timeBase), false); + } + } + /// Inflict typical exo weapon debuffs. Duration multiplier optional. /// /// The NPC attacked. diff --git a/Utilities/PlayerUtils.cs b/Utilities/PlayerUtils.cs index 9ff3600935..1a81e2d7a4 100644 --- a/Utilities/PlayerUtils.cs +++ b/Utilities/PlayerUtils.cs @@ -118,6 +118,22 @@ namespace CalamityMod damageMult += mp.GetAdrenalineDamage(); } + public static void Inflict246DebuffsPvp(Player target, int buff, float timeBase = 2f) + { + if (Main.rand.NextBool(4)) + { + target.AddBuff(buff, SecondsToFrames(timeBase * 3f), false); + } + else if (Main.rand.NextBool(2)) + { + target.AddBuff(buff, SecondsToFrames(timeBase * 2f), false); + } + else + { + target.AddBuff(buff, SecondsToFrames(timeBase), false); + } + } + /// /// Inflict typical exo weapon debuffs in pvp. ///