diff --git a/Effects/CalamityShaders.cs b/Effects/CalamityShaders.cs index 8f6ca869e6..854606b33d 100644 --- a/Effects/CalamityShaders.cs +++ b/Effects/CalamityShaders.cs @@ -39,6 +39,7 @@ namespace CalamityMod.Effects public static Effect ExobladeSlashShader; public static Effect ExobladePierceShader; public static Effect HeavenlyGaleTrailShader; + public static Effect HeavenlyGaleLightningShader; public static Effect BaseFusableParticleEdgeShader; public static Effect AdditiveFusableParticleEdgeShader; @@ -81,6 +82,7 @@ namespace CalamityMod.Effects ExobladeSlashShader = CalamityMod.Instance.Assets.Request("Effects/ExobladeSlashShader", AssetRequestMode.ImmediateLoad).Value; ExobladePierceShader = CalamityMod.Instance.Assets.Request("Effects/ExobladePierceShader", AssetRequestMode.ImmediateLoad).Value; HeavenlyGaleTrailShader = CalamityMod.Instance.Assets.Request("Effects/HeavenlyGaleTrailShader", AssetRequestMode.ImmediateLoad).Value; + HeavenlyGaleLightningShader = CalamityMod.Instance.Assets.Request("Effects/HeavenlyGaleLightningShader", AssetRequestMode.ImmediateLoad).Value; BaseFusableParticleEdgeShader = CalamityMod.Instance.Assets.Request("Effects/ParticleFusion/BaseFusableParticleEdgeShader", AssetRequestMode.ImmediateLoad).Value; AdditiveFusableParticleEdgeShader = CalamityMod.Instance.Assets.Request("Effects/ParticleFusion/AdditiveFusableParticleEdgeShader", AssetRequestMode.ImmediateLoad).Value; @@ -120,6 +122,7 @@ namespace CalamityMod.Effects GameShaders.Misc["CalamityMod:ExobladeSlash"] = new MiscShaderData(new Ref(ExobladeSlashShader), "TrailPass"); GameShaders.Misc["CalamityMod:ExobladePierce"] = new MiscShaderData(new Ref(ExobladePierceShader), "PiercePass"); GameShaders.Misc["CalamityMod:HeavenlyGaleTrail"] = new MiscShaderData(new Ref(HeavenlyGaleTrailShader), "PiercePass"); + GameShaders.Misc["CalamityMod:HeavenlyGaleLightningArc"] = new MiscShaderData(new Ref(HeavenlyGaleLightningShader), "TrailPass"); GameShaders.Misc["CalamityMod:BaseFusableParticleEdge"] = new MiscShaderData(new Ref(BaseFusableParticleEdgeShader), "ParticlePass"); GameShaders.Misc["CalamityMod:AdditiveFusableParticleEdge"] = new MiscShaderData(new Ref(AdditiveFusableParticleEdgeShader), "ParticlePass"); diff --git a/Effects/HeavenlyGaleLightningShader.fx b/Effects/HeavenlyGaleLightningShader.fx new file mode 100644 index 0000000000..449fe01b37 --- /dev/null +++ b/Effects/HeavenlyGaleLightningShader.fx @@ -0,0 +1,60 @@ +sampler uImage0 : register(s0); +sampler uImage1 : register(s1); +float3 uColor; +float3 uSecondaryColor; +float uOpacity; +float uSaturation; +float uRotation; +float uTime; +float4 uSourceRect; +float2 uWorldPosition; +float uDirection; +float3 uLightSource; +float2 uImageSize0; +float2 uImageSize1; +matrix uWorldViewProjection; +float4 uShaderSpecificData; + +struct VertexShaderInput +{ + float4 Position : POSITION0; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +struct VertexShaderOutput +{ + float4 Position : SV_POSITION; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +VertexShaderOutput VertexShaderFunction(in VertexShaderInput input) +{ + VertexShaderOutput output = (VertexShaderOutput) 0; + float4 pos = mul(input.Position, uWorldViewProjection); + output.Position = pos; + + output.Color = input.Color; + output.TextureCoordinates = input.TextureCoordinates; + + return output; +} + +float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 +{ + float4 color = input.Color; + float2 coords = input.TextureCoordinates; + float distortion = lerp(-1, 1, tex2D(uImage1, coords + float2(0, uTime * sign(coords.y > 0.5 ? -1 : 1) * 1.81)).r); + float opacity = pow(sin((coords.y + distortion * 0.15) * 3.141), distortion * 3.95 + 7); + return color * pow(opacity, 0.25) + opacity; +} + +technique Technique1 +{ + pass TrailPass + { + VertexShader = compile vs_2_0 VertexShaderFunction(); + PixelShader = compile ps_2_0 PixelShaderFunction(); + } +} \ No newline at end of file diff --git a/Effects/HeavenlyGaleLightningShader.xnb b/Effects/HeavenlyGaleLightningShader.xnb new file mode 100644 index 0000000000..fce4bd53d8 Binary files /dev/null and b/Effects/HeavenlyGaleLightningShader.xnb differ diff --git a/Items/Weapons/Ranged/HeavenlyGale.cs b/Items/Weapons/Ranged/HeavenlyGale.cs index 9d4333f2f2..359d9a45ef 100644 --- a/Items/Weapons/Ranged/HeavenlyGale.cs +++ b/Items/Weapons/Ranged/HeavenlyGale.cs @@ -25,8 +25,14 @@ namespace CalamityMod.Items.Weapons.Ranged public const float MaxChargeDamageBoost = 4.5f; + public const float LightningDamageFactor = 0.36f; + + public const float ChargeLightningCreationThreshold = 0.8f; + public static readonly SoundStyle FireSound = new("CalamityMod/Sounds/Item/HeavenlyGaleFire"); + public static readonly SoundStyle LightningStrikeSound = new("CalamityMod/Sounds/Custom/HeavenlyGaleLightningStrike"); + public override void SetStaticDefaults() { DisplayName.SetDefault("Heavenly Gale"); diff --git a/Projectiles/Ranged/ExoCrystalArrow.cs b/Projectiles/Ranged/ExoCrystalArrow.cs index 104480b18b..c74762a2fc 100644 --- a/Projectiles/Ranged/ExoCrystalArrow.cs +++ b/Projectiles/Ranged/ExoCrystalArrow.cs @@ -15,6 +15,8 @@ namespace CalamityMod.Projectiles.Ranged { public PrimitiveTrail PierceAfterimageDrawer = null; + public bool CreateLightning => Projectile.ai[0] == 1f; + public override void SetStaticDefaults() { DisplayName.SetDefault("Exo Crystal"); @@ -103,5 +105,22 @@ namespace CalamityMod.Projectiles.Ranged GeneralParticleHandler.SpawnParticle(exoEnergy); } } + + public override void OnHitNPC(NPC target, int damage, float knockback, bool crit) + { + // Create lightning from above if necessary. + if (!CreateLightning) + return; + + int lightningDamage = (int)(Projectile.damage * HeavenlyGale.LightningDamageFactor); + Vector2 lightningSpawnPosition = target.Center - Vector2.UnitY.RotatedByRandom(0.24f) * Main.rand.NextFloat(960f, 1020f); + Vector2 lightningShootVelocity = (target.Center - lightningSpawnPosition).SafeNormalize(Vector2.UnitY) * 13f; + int lightning = Projectile.NewProjectile(Projectile.GetSource_OnHit(target), lightningSpawnPosition, lightningShootVelocity, ModContent.ProjectileType(), lightningDamage, 0f, Projectile.owner); + if (Main.projectile.IndexInRange(lightning)) + { + Main.projectile[lightning].ai[0] = lightningShootVelocity.ToRotation(); + Main.projectile[lightning].ai[1] = Main.rand.Next(100); + } + } } } diff --git a/Projectiles/Ranged/ExoLightningBolt.cs b/Projectiles/Ranged/ExoLightningBolt.cs new file mode 100644 index 0000000000..f683254923 --- /dev/null +++ b/Projectiles/Ranged/ExoLightningBolt.cs @@ -0,0 +1,174 @@ +using CalamityMod.Items.Weapons.Ranged; +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Terraria; +using Terraria.Audio; +using Terraria.Graphics.Shaders; +using Terraria.ID; +using Terraria.ModLoader; +using Terraria.Utilities; + +namespace CalamityMod.Projectiles.Ranged +{ + public class ExoLightningBolt : ModProjectile + { + internal PrimitiveTrail LightningDrawer; + + public bool HasPlayedSound; + + public const int Lifetime = 45; + public ref float InitialVelocityAngle => ref Projectile.ai[0]; + + // Technically not a ratio, and more of a seed, but it is used in a 0-2pi squash + // later in the code to get an arbitrary unit vector (which is then checked). + public ref float BaseTurnAngleRatio => ref Projectile.ai[1]; + public ref float AccumulatedXMovementSpeeds => ref Projectile.localAI[0]; + public ref float BranchingIteration => ref Projectile.localAI[1]; + + public virtual float LightningTurnRandomnessFactor { get; } = 2f; + public override string Texture => "CalamityMod/Projectiles/LightningProj"; + public override void SetStaticDefaults() + { + DisplayName.SetDefault("Exo Teratesla Lightningbolt"); + ProjectileID.Sets.DrawScreenCheckFluff[Type] = 10000; + ProjectileID.Sets.TrailingMode[Projectile.type] = 1; + ProjectileID.Sets.TrailCacheLength[Projectile.type] = 50; + } + + public override void SetDefaults() + { + Projectile.width = 22; + Projectile.height = 22; + Projectile.alpha = 255; + Projectile.penetrate = -1; + Projectile.ignoreWater = true; + Projectile.tileCollide = false; + Projectile.friendly = true; + Projectile.DamageType = DamageClass.Ranged; + Projectile.MaxUpdates = 5; + Projectile.usesLocalNPCImmunity = true; + Projectile.localNPCHitCooldown = Projectile.MaxUpdates * 13; + Projectile.timeLeft = Projectile.MaxUpdates * Lifetime; + } + + public override void SendExtraAI(BinaryWriter writer) + { + writer.Write(AccumulatedXMovementSpeeds); + writer.Write(BranchingIteration); + } + + public override void ReceiveExtraAI(BinaryReader reader) + { + AccumulatedXMovementSpeeds = reader.ReadSingle(); + BranchingIteration = reader.ReadSingle(); + } + + public override void AI() + { + // FrameCounter in this context is really just an arbitrary timer + // which allows random turning to occur. + Projectile.frameCounter++; + Projectile.oldPos[1] = Projectile.oldPos[0]; + + // Adjust opacity and scale. + float adjustedTimeLife = Projectile.timeLeft / Projectile.MaxUpdates; + Projectile.Opacity = Utils.GetLerpValue(0f, 9f, adjustedTimeLife, true) * Utils.GetLerpValue(Lifetime, Lifetime - 3f, adjustedTimeLife, true); + Projectile.scale = Projectile.Opacity; + + // Play a strike sound on the first frame. + if (!HasPlayedSound) + { + SoundEngine.PlaySound(HeavenlyGale.LightningStrikeSound with { Volume = 0.4f }, Main.player[Projectile.owner].Center); + HasPlayedSound = true; + } + + Lighting.AddLight(Projectile.Center, Color.White.ToVector3()); + if (Projectile.frameCounter >= Projectile.extraUpdates * 2) + { + Projectile.frameCounter = 0; + + float originalSpeed = MathHelper.Min(15f, Projectile.velocity.Length()); + UnifiedRandom unifiedRandom = new((int)BaseTurnAngleRatio); + int turnTries = 0; + Vector2 newBaseDirection = -Vector2.UnitY; + Vector2 potentialBaseDirection; + + do + { + BaseTurnAngleRatio = unifiedRandom.Next() % 100; + potentialBaseDirection = (BaseTurnAngleRatio / 100f * MathHelper.TwoPi).ToRotationVector2(); + + // Ensure that the new potential direction base is always moving upwards (this is supposed to be somewhat similar to a -UnitY + RotatedBy). + potentialBaseDirection.Y = -Math.Abs(potentialBaseDirection.Y); + + bool canChangeLightningDirection = true; + + // Potential directions with very little Y speed should not be considered, because this + // consequentially means that the X speed would be quite large. + if (potentialBaseDirection.Y > -0.02f) + canChangeLightningDirection = false; + + // This mess of math basically encourages movement at the ends of an extraUpdate cycle, + // discourages super frequenent randomness as the accumulated X speed changes get larger, + // or if the original speed is quite large. + if (Math.Abs(potentialBaseDirection.X * (Projectile.extraUpdates + 1) * 2f * originalSpeed + AccumulatedXMovementSpeeds) > Projectile.MaxUpdates * LightningTurnRandomnessFactor) + canChangeLightningDirection = false; + + // If the above checks were all passed, redefine the base direction of the lightning. + if (canChangeLightningDirection) + newBaseDirection = potentialBaseDirection; + + turnTries++; + } + while (turnTries < 100); + + if (Projectile.velocity != Vector2.Zero) + { + AccumulatedXMovementSpeeds += newBaseDirection.X * (Projectile.extraUpdates + 1) * 2f * originalSpeed; + Projectile.velocity = newBaseDirection.RotatedBy(InitialVelocityAngle + MathHelper.PiOver2) * originalSpeed; + Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.PiOver2; + } + } + } + + public float PrimitiveWidthFunction(float completionRatio) => CalamityUtils.Convert01To010(completionRatio) * Projectile.scale * Projectile.width; + + public Color PrimitiveColorFunction(float completionRatio) + { + float colorInterpolant = (float)Math.Sin(Projectile.identity / 3f + completionRatio * 20f + Main.GlobalTimeWrappedHourly * 1.1f) * 0.5f + 0.5f; + Color color = CalamityUtils.MulticolorLerp(colorInterpolant, Color.GreenYellow, Color.Lime, Color.Cyan); + return color; + } + + public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) + { + List checkPoints = Projectile.oldPos.Where(oldPos => oldPos != Vector2.Zero).ToList(); + if (checkPoints.Count <= 2) + return false; + + for (int i = 0; i < checkPoints.Count - 1; i++) + { + float _ = 0f; + float width = PrimitiveWidthFunction(i / (float)checkPoints.Count); + if (Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), checkPoints[i], checkPoints[i + 1], width * 0.8f, ref _)) + return true; + } + return false; + } + + public override bool PreDraw(ref Color lightColor) + { + if (LightningDrawer is null) + LightningDrawer = new PrimitiveTrail(PrimitiveWidthFunction, PrimitiveColorFunction, PrimitiveTrail.RigidPointRetreivalFunction, GameShaders.Misc["CalamityMod:HeavenlyGaleLightningArc"]); + + GameShaders.Misc["CalamityMod:HeavenlyGaleLightningArc"].UseImage1("Images/Misc/Perlin"); + GameShaders.Misc["CalamityMod:HeavenlyGaleLightningArc"].Apply(); + + LightningDrawer.Draw(Projectile.oldPos, Projectile.Size * 0.5f - Main.screenPosition, 18); + return false; + } + } +} diff --git a/Projectiles/Ranged/HeavenlyGaleProj.cs b/Projectiles/Ranged/HeavenlyGaleProj.cs index dfafc79e20..4b28101b23 100644 --- a/Projectiles/Ranged/HeavenlyGaleProj.cs +++ b/Projectiles/Ranged/HeavenlyGaleProj.cs @@ -89,8 +89,9 @@ namespace CalamityMod.Projectiles.Ranged if (Main.myPlayer == Projectile.owner) { + bool createLightning = ChargeTimer / HeavenlyGale.MaxChargeTime >= HeavenlyGale.ChargeLightningCreationThreshold; Vector2 arrowVelocity = arrowDirection * 20f; - Projectile.NewProjectile(Projectile.GetSource_FromAI(), tipPosition, arrowVelocity, ModContent.ProjectileType(), (int)(Projectile.damage * damageFactor), Projectile.knockBack, Projectile.owner); + Projectile.NewProjectile(Projectile.GetSource_FromAI(), tipPosition, arrowVelocity, ModContent.ProjectileType(), (int)(Projectile.damage * damageFactor), Projectile.knockBack, Projectile.owner, createLightning.ToInt()); } } diff --git a/Sounds/Custom/HeavenlyGaleLightningStrike.ogg b/Sounds/Custom/HeavenlyGaleLightningStrike.ogg new file mode 100644 index 0000000000..0a164a5e2b Binary files /dev/null and b/Sounds/Custom/HeavenlyGaleLightningStrike.ogg differ