diff --git a/Projectiles/Boss/AresTeslaOrb.cs b/Projectiles/Boss/AresTeslaOrb.cs index 894c2d2314..8103fff1b5 100644 --- a/Projectiles/Boss/AresTeslaOrb.cs +++ b/Projectiles/Boss/AresTeslaOrb.cs @@ -1,5 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; using System.IO; using Terraria; using Terraria.ID; @@ -7,27 +9,29 @@ using Terraria.ModLoader; namespace CalamityMod.Projectiles.Boss { - public class AresTeslaOrb : ModProjectile - { + public class AresTeslaOrb : ModProjectile + { public ref float Identity => ref projectile.ai[0]; + public PrimitiveTrail LightningDrawer; + public PrimitiveTrail LightningBackgroundDrawer; private const int timeLeft = 480; public override void SetStaticDefaults() - { - DisplayName.SetDefault("Tesla Orb"); - Main.projFrames[projectile.type] = 4; - ProjectileID.Sets.TrailCacheLength[projectile.type] = 4; - ProjectileID.Sets.TrailingMode[projectile.type] = 0; - } + { + DisplayName.SetDefault("Tesla Orb"); + Main.projFrames[projectile.type] = 4; + ProjectileID.Sets.TrailCacheLength[projectile.type] = 4; + ProjectileID.Sets.TrailingMode[projectile.type] = 0; + } - public override void SetDefaults() - { + public override void SetDefaults() + { projectile.width = 32; - projectile.height = 32; - projectile.hostile = true; - projectile.ignoreWater = true; + projectile.height = 32; + projectile.hostile = true; + projectile.ignoreWater = true; projectile.tileCollide = false; - projectile.penetrate = -1; + projectile.penetrate = -1; projectile.Opacity = 0f; cooldownSlot = 1; projectile.timeLeft = timeLeft; @@ -45,7 +49,7 @@ namespace CalamityMod.Projectiles.Boss } public override void AI() - { + { int fadeOutTime = 15; int fadeInTime = 3; if (projectile.timeLeft < fadeOutTime) @@ -56,13 +60,13 @@ namespace CalamityMod.Projectiles.Boss Lighting.AddLight(projectile.Center, 0.1f * projectile.Opacity, 0.25f * projectile.Opacity, 0.25f * projectile.Opacity); projectile.frameCounter++; - if (projectile.frameCounter > 4) - { - projectile.frame++; - projectile.frameCounter = 0; - } - if (projectile.frame > 3) - projectile.frame = 0; + if (projectile.frameCounter > 4) + { + projectile.frame++; + projectile.frameCounter = 0; + } + if (projectile.frame > 3) + projectile.frame = 0; if (projectile.localAI[0] == 0f) { @@ -119,7 +123,7 @@ namespace CalamityMod.Projectiles.Boss dust = Main.dust[num56]; } } - } + } public override bool CanHitPlayer(Player target) => projectile.Opacity == 1f; @@ -131,16 +135,108 @@ namespace CalamityMod.Projectiles.Boss target.AddBuff(BuffID.Electrified, 240); } - public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) - { + public Projectile GetOrbToAttachTo() + { + for (int i = 0; i < Main.maxProjectiles; i++) + { + if (Main.projectile[i].type != projectile.type || Main.projectile[i].ai[0] != Identity + 1f || !Main.projectile[i].active) + continue; + + return Main.projectile[i]; + } + return null; + } + + public List DetermineElectricArcPoints(Vector2 start, Vector2 end, int seed) + { + List points = new List(); + + // Determine the base points based on a linear path from the start the end end point. + for (int i = 0; i <= 75; i++) + points.Add(Vector2.Lerp(start, end, i / 73.5f)); + + // Then, add continuous randomness to the positions of various points. + for (int i = 0; i < points.Count; i++) + { + float completionRatio = i / (float)points.Count; + + // Noise offsets should taper off at the ends of the line. + float offsetMuffleFactor = Utils.InverseLerp(0.12f, 0.25f, completionRatio, true) * Utils.InverseLerp(0.88f, 0.75f, completionRatio, true); + + // Give a sense of time for the noise on the vertical axis. This is achieved via a 0-1 constricted sinusoid. + float noiseY = (float)Math.Cos(completionRatio * 17.2f + Main.GlobalTime * 10.7f) * 0.5f + 0.5f; + + float noise = CalamityUtils.PerlinNoise2D(completionRatio, noiseY, 2, seed); + + // Now that the noise value has been computed, convert it to a direction by treating the noise as an angle + // and then converting it into a unit vector. + Vector2 offsetDirection = (noise * MathHelper.Pi * 0.7f).ToRotationVector2(); + + // Then, determine the factor of the offset. This is based on the initial direction (but squashed) and the muffle factor from above. + Vector2 offset = offsetDirection * (float)Math.Pow(offsetDirection.Y, 2D) * offsetMuffleFactor * 25f; + + points[i] += offset; + } + + return points; + } + + internal float WidthFunction(float completionRatio) + { + return MathHelper.Lerp(0.75f, 1.85f, (float)Math.Sin(MathHelper.Pi * completionRatio)) * projectile.scale; + } + + internal Color ColorFunction(float completionRatio) + { + float fadeToWhite = MathHelper.Lerp(0f, 0.65f, (float)Math.Sin(MathHelper.TwoPi * completionRatio + Main.GlobalTime * 4f) * 0.5f + 0.5f); + Color baseColor = Color.Lerp(Color.Cyan, Color.White, fadeToWhite); + return Color.Lerp(baseColor, Color.LightBlue, ((float)Math.Sin(MathHelper.Pi * completionRatio + Main.GlobalTime * 4f) * 0.5f + 0.5f) * 0.8f) * projectile.Opacity; + } + + internal float BackgroundWidthFunction(float completionRatio) => WidthFunction(completionRatio) * 4f; + + internal Color BackgroundColorFunction(float completionRatio) + { + Color color = Color.CornflowerBlue * projectile.Opacity * 0.4f; + return color; + } + + public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) + { + if (LightningDrawer is null) + LightningDrawer = new PrimitiveTrail(WidthFunction, ColorFunction, PrimitiveTrail.RigidPointRetreivalFunction); + if (LightningBackgroundDrawer is null) + LightningBackgroundDrawer = new PrimitiveTrail(BackgroundWidthFunction, BackgroundColorFunction, PrimitiveTrail.RigidPointRetreivalFunction); + + Projectile orbToAttachTo = GetOrbToAttachTo(); + if (orbToAttachTo != null) + { + List arcPoints = DetermineElectricArcPoints(projectile.Center, orbToAttachTo.Center, 117); + LightningBackgroundDrawer.Draw(arcPoints, -Main.screenPosition, 90); + LightningDrawer.Draw(arcPoints, -Main.screenPosition, 90); + } + lightColor.R = (byte)(255 * projectile.Opacity); lightColor.G = (byte)(255 * projectile.Opacity); lightColor.B = (byte)(255 * projectile.Opacity); CalamityUtils.DrawAfterimagesCentered(projectile, ProjectileID.Sets.TrailingMode[projectile.type], lightColor, 1); - return false; + return false; + } + + public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) + { + if (projHitbox.Intersects(targetHitbox)) + return true; + + float _ = 0f; + Projectile orbToAttachTo = GetOrbToAttachTo(); + if (orbToAttachTo != null && Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), projectile.Center, orbToAttachTo.Center, 8f, ref _)) + return true; + + return false; } - public override void ModifyHitPlayer(Player target, ref int damage, ref bool crit) + public override void ModifyHitPlayer(Player target, ref int damage, ref bool crit) { target.Calamity().lastProjectileHit = projectile; } diff --git a/Utilities/MathematicalUtils.cs b/Utilities/MathematicalUtils.cs index 797a623508..bee488ebe1 100644 --- a/Utilities/MathematicalUtils.cs +++ b/Utilities/MathematicalUtils.cs @@ -1,11 +1,71 @@ using Microsoft.Xna.Framework; using System; +using System.Collections.Generic; using Terraria; namespace CalamityMod { public static partial class CalamityUtils { + + internal static readonly List Directions = new List() + { + new Vector2(-1f, -1f), + new Vector2(1f, -1f), + new Vector2(-1f, 1f), + new Vector2(1f, 1f), + new Vector2(0f, -1f), + new Vector2(-1f, 0f), + new Vector2(0f, 1f), + new Vector2(1f, 0f), + }; + + /// + /// Computes 2-dimensional Perlin Noise, which gives "random" but continuous values. + /// + /// The X position on the map. + /// The Y position on the map. + /// A metric of "instability" of the noise. The higher this is, the more unstable. Lower of bounds of 2-3 are preferable. + /// The seed for the noise. + /// + public static float PerlinNoise2D(float x, float y, int octaves, int seed) + { + float SmoothFunction(float n) => 3f * n * n - 2f * n * n * n; + float NoiseGradient(int s, int noiseX, int noiseY, float xd, float yd) + { + int hash = s; + hash ^= 1619 * noiseX; + hash ^= 31337 * noiseY; + + hash = hash * hash * hash * 60493; + hash = (hash >> 13) ^ hash; + + Vector2 g = Directions[hash & 7]; + + return xd * g.X + yd * g.Y; + } + + int frequency = (int)Math.Pow(2D, octaves); + x *= frequency; + y *= frequency; + + int flooredX = (int)x; + int flooredY = (int)y; + int ceilingX = flooredX + 1; + int ceilingY = flooredY + 1; + float interpolatedX = x - flooredX; + float interpolatedY = y - flooredY; + float interpolatedX2 = interpolatedX - 1; + float interpolatedY2 = interpolatedY - 1; + + float fadeX = SmoothFunction(interpolatedX); + float fadeY = SmoothFunction(interpolatedY); + + float smoothX = MathHelper.Lerp(NoiseGradient(seed, flooredX, flooredY, interpolatedX, interpolatedY), NoiseGradient(seed, ceilingX, flooredY, interpolatedX2, interpolatedY), fadeX); + float smoothY = MathHelper.Lerp(NoiseGradient(seed, flooredX, ceilingY, interpolatedX, interpolatedY2), NoiseGradient(seed, ceilingX, ceilingY, interpolatedX2, interpolatedY2), fadeX); + return MathHelper.Lerp(smoothX, smoothY, fadeY); + } + /// /// Computes the Manhattan Distance between two points. This is typically used as a cheaper alternative to Euclidean Distance. ///