using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.IO; using Terraria; using Terraria.Enums; using Terraria.GameContent.Shaders; using Terraria.Graphics.Effects; using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Projectiles.Magic { public class YharimsCrystalBeam : ModProjectile { private const float PiBeamDivisor = MathHelper.Pi / YharimsCrystalPrism.NumBeams; private const float MaxDamageMultiplier = 3f; private const float BeamPosOffset = 16f; private const float MaxBeamScale = 1.8f; private const float MaxBeamLength = 2400f; private const float BeamTileCollisionWidth = 1f; private const float BeamHitboxCollisionWidth = 22f; private const int NumSamplePoints = 3; private const float BeamLengthChangeFactor = 0.75f; private const float VisualEffectThreshold = 0.1f; private const float OuterBeamOpacityMultiplier = 0.75f; private const float InnerBeamOpacityMultiplier = 0.1f; private const float BeamLightBrightness = 0.75f; private const float MainDustBeamEndOffset = 14.5f; private const float SidewaysDustBeamEndOffset = 4f; private const float BeamRenderTileOffset = 10.5f; private const float BeamLengthReductionFactor = 14.5f; public override void SetStaticDefaults() { DisplayName.SetDefault("Yharim's Beam"); } public override void SetDefaults() { projectile.width = 18; projectile.height = 18; projectile.magic = true; projectile.penetrate = -1; projectile.alpha = 255; // The beam itself still stops on tiles, but its invisible "source" projectile ignores them. projectile.tileCollide = false; projectile.usesLocalNPCImmunity = true; projectile.localNPCHitCooldown = 10; projectile.Calamity().PierceResistHarshness = 0.06f; projectile.Calamity().PierceResistCap = 0.4f; } public override void SendExtraAI(BinaryWriter writer) => writer.Write(projectile.localAI[1]); public override void ReceiveExtraAI(BinaryReader reader) => projectile.localAI[1] = reader.ReadSingle(); // projectile.ai[0] = 0 through N-1, chosen at spawn = Which beam ID this is (they all have different rotations and colors) // projectile.ai[1] = Index of the crystal projectile "hosting" this beam // projectile.localAI[1] = Length of the beam (dynamically calculated every frame) // The beam projectile also makes heavy use of its host crystal's charge level. public override void AI() { // If something has gone wrong with either the beam or the host crystal, destroy the beam. Projectile hostCrystal = Main.projectile[(int)projectile.ai[1]]; if (projectile.type != ModContent.ProjectileType() || !hostCrystal.active || hostCrystal.type != ModContent.ProjectileType()) { projectile.Kill(); return; } Vector2 hostCrystalDir = Vector2.Normalize(hostCrystal.velocity); float chargeRatio = MathHelper.Clamp(hostCrystal.ai[0] / YharimsCrystalPrism.MaxCharge, 0f, 1f); // Update the beam's damage based on the host crystal's current damage value, which accounts for Mana Sickness. // Yharim's Crystal smoothly scales up its damage instead of suddenly jumping to a higher damage output. projectile.damage = (int)(hostCrystal.damage * GetDamageMultiplier(chargeRatio)); // The beam cannot strike enemies until at a certain charge level. projectile.friendly = hostCrystal.ai[0] > YharimsCrystalPrism.DamageStart; // This offset is used to make the beams orient differently. float beamIdOffset = (int)projectile.ai[0] - YharimsCrystalPrism.NumBeams / 2f + 0.5f; float beamSpread; float spinRate; float beamStartSidewaysOffset; float beamStartForwardsOffset; // Variables scale smoothly while the crystal is charging up, but are fixed once it is at max charge. if (chargeRatio < 1f) { projectile.scale = MathHelper.Lerp(0f, MaxBeamScale, chargeRatio); beamSpread = MathHelper.Lerp(1.22f, 0f, chargeRatio); beamStartSidewaysOffset = MathHelper.Lerp(20f, 6f, chargeRatio); beamStartForwardsOffset = MathHelper.Lerp(-17f, -13f, chargeRatio); // For the first 2/3 of charge time, the opacity scales up from 0% to 40%. // Spin rate increases slowly during this time. if (chargeRatio <= 0.66f) { float phaseRatio = chargeRatio * 1.5f; projectile.Opacity = MathHelper.Lerp(0f, 0.4f, phaseRatio); spinRate = MathHelper.Lerp(20f, 16f, phaseRatio); } // For the last 1/3 of charge time, the opacity scales up from 40% to 100%. // Spin rate increases dramatically during this time. else { float phaseRatio = (chargeRatio - 0.66f) * 3f; projectile.Opacity = MathHelper.Lerp(0.4f, 1f, phaseRatio); spinRate = MathHelper.Lerp(16f, 6f, phaseRatio); } } else { projectile.scale = MaxBeamScale; projectile.Opacity = 1f; beamSpread = 0f; spinRate = 6f; beamStartSidewaysOffset = 6f; beamStartForwardsOffset = -13f; } // The amount to which the angle changes reduces over time so that the beams look like they are focusing. float deviationAngle = (hostCrystal.ai[0] + beamIdOffset * spinRate) / (spinRate * YharimsCrystalPrism.NumBeams) * MathHelper.TwoPi; Vector2 unitRot = Vector2.UnitY.RotatedBy(deviationAngle); float sinusoidYOffset = unitRot.Y * PiBeamDivisor * beamSpread; float hostCrystalAngle = hostCrystal.velocity.ToRotation(); Vector2 yVec = new Vector2(4f, beamStartSidewaysOffset); Vector2 beamSpanVector = (unitRot * yVec).RotatedBy(hostCrystalAngle); // Calculate the beam's emanating position. Start with the crystal center. projectile.Center = hostCrystal.Center; // Add a fixed offset to align with the crystal's spritesheet (?) projectile.position += hostCrystalDir * BeamPosOffset + new Vector2(0f, -hostCrystal.gfxOffY); // Add the forwards offset, measured in pixels. projectile.position += hostCrystalDir * beamStartForwardsOffset; // Add the sideways offset vector, which is calculated for the current angle of the beam and scales with the beam's sideways offset. projectile.position += beamSpanVector; // Set the beam's velocity to point towards its current spread direction and sanity check it. It should have magnitude 1. projectile.velocity = hostCrystalDir.RotatedBy(sinusoidYOffset); if (projectile.velocity.HasNaNs() || projectile.velocity == Vector2.Zero) projectile.velocity = -Vector2.UnitY; projectile.rotation = projectile.velocity.ToRotation(); // By default, the interpolation starts at the projectile's center. // If the host crystal is fully charged, the interpolation starts at the host crystal's center instead. // Overriding that, if the player shoves the crystal into or through a wall, the interpolation starts at the player's center. Vector2 samplingPoint = projectile.Center; if(hostCrystal.ai[0] >= YharimsCrystalPrism.MaxCharge) samplingPoint = hostCrystal.Center; if (!Collision.CanHitLine(Main.player[projectile.owner].Center, 0, 0, hostCrystal.Center, 0, 0)) samplingPoint = Main.player[projectile.owner].Center; // Perform a laser scan to calculate the correct length of the beam. // Alternatively, if the beam ignores tiles, just set it to be the max beam length with the following line. // projectile.localAI[1] = MaxBeamLength; float[] laserScanResults = new float[NumSamplePoints]; Collision.LaserScan(samplingPoint, projectile.velocity, BeamTileCollisionWidth * projectile.scale, MaxBeamLength, laserScanResults); float avg = 0f; for (int i = 0; i < laserScanResults.Length; ++i) avg += laserScanResults[i]; avg /= NumSamplePoints; projectile.localAI[1] = MathHelper.Lerp(projectile.localAI[1], avg, BeamLengthChangeFactor); // X = beam length. Y = beam width. Vector2 beamDims = new Vector2(projectile.velocity.Length() * projectile.localAI[1], projectile.width * projectile.scale); // Only produce dust and cause water ripples if the beam is above a certain charge level. Color beamColor = GetBeamColor(); if (chargeRatio >= VisualEffectThreshold) { ProduceBeamDust(beamColor); // If the game is rendering (i.e. isn't a dedicated server), make the beam disturb water. if (Main.netMode != NetmodeID.Server) { WaterShaderData wsd = (WaterShaderData)Filters.Scene["WaterDistortion"].GetShader(); // A universal time-based sinusoid which updates extremely rapidly. GlobalTime is 0 to 3600, measured in seconds. float waveSine = 0.1f * (float)Math.Sin(Main.GlobalTime * 20f); Vector2 ripplePos = projectile.position + new Vector2(beamDims.X * 0.5f, 0f).RotatedBy(projectile.rotation); // WaveData is encoded as a Color. Not sure why, considering Vector3 exists. Color waveData = new Color(0.5f, 0.1f * Math.Sign(waveSine) + 0.5f, 0f, 1f) * Math.Abs(waveSine); wsd.QueueRipple(ripplePos, waveData, beamDims, RippleShape.Square, projectile.rotation); } } // Make the beam cast light along its length. The brightness of the light scales with the charge. // v3_1 is an unnamed decompiled variable which is the color of the light cast by DelegateMethods.CastLight DelegateMethods.v3_1 = beamColor.ToVector3() * BeamLightBrightness * chargeRatio; Utils.PlotTileLine(projectile.Center, projectile.Center + projectile.velocity * projectile.localAI[1], beamDims.Y, new Utils.PerLinePoint(DelegateMethods.CastLight)); } // Uses a simple polynomial (x^3) to get sudden but smooth damage increase near the end of the charge-up period. private float GetDamageMultiplier(float chargeRatio) { float f = chargeRatio * chargeRatio * chargeRatio; return MathHelper.Lerp(1f, MaxDamageMultiplier, f); } // Determines whether the specified target hitbox is intersecting with the beam. public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) { // If the target is touching the beam's hitbox (which is a small rectangle vaguely overlapping the host crystal), that's good enough. if (projHitbox.Intersects(targetHitbox)) return true; // Otherwise, perform an AABB line collision check to check the whole beam. float _ = float.NaN; return Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), projectile.Center, projectile.Center + projectile.velocity * projectile.localAI[1], BeamHitboxCollisionWidth * projectile.scale, ref _); } public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { // If the beam doesn't have a defined direction, don't draw anything. if (projectile.velocity == Vector2.Zero) return false; Texture2D tex = Main.projectileTexture[projectile.type]; float beamLength = projectile.localAI[1]; Vector2 centerFloored = projectile.Center.Floor() + projectile.velocity * projectile.scale * BeamRenderTileOffset; Vector2 scaleVec = new Vector2(projectile.scale); // Reduce the beam length proportional to its square area to reduce block penetration. beamLength -= BeamLengthReductionFactor * projectile.scale * projectile.scale; DelegateMethods.f_1 = 1f; // f_1 is an unnamed decompiled variable whose function is unknown. Leave it at 1. Vector2 beamStartPos = centerFloored - Main.screenPosition; Vector2 beamEndPos = beamStartPos + projectile.velocity * beamLength; Utils.LaserLineFraming llf = new Utils.LaserLineFraming(DelegateMethods.RainbowLaserDraw); // Draw the outer beam // c_1 is an unnamed decompiled variable which is the render color of the beam drawn by DelegateMethods.RainbowLaserDraw Color outerBeamColor = GetBeamColor(); DelegateMethods.c_1 = outerBeamColor * OuterBeamOpacityMultiplier * projectile.Opacity; Utils.DrawLaser(Main.spriteBatch, tex, beamStartPos, beamEndPos, scaleVec, llf); // Draw the inner beam (reduced size) scaleVec *= 0.5f; Color innerBeamColor = Color.White; DelegateMethods.c_1 = innerBeamColor * InnerBeamOpacityMultiplier * projectile.Opacity; Utils.DrawLaser(Main.spriteBatch, tex, beamStartPos, beamEndPos, scaleVec, llf); return false; } private void ProduceBeamDust(Color beamColor) { // Create a few dust per frame a small distance from where the beam ends. Vector2 laserEndPos = projectile.Center + projectile.velocity * (projectile.localAI[1] - MainDustBeamEndOffset * projectile.scale); for (int i = 0; i < 2; ++i) { // 50% chance for the dust to come off on either side of the beam. float dustAngle = projectile.rotation + (Main.rand.NextBool() ? 1f : -1f) * MathHelper.PiOver2; float dustStartDist = Main.rand.NextFloat(1f, 1.8f); Vector2 dustVel = dustAngle.ToRotationVector2() * dustStartDist; int d = Dust.NewDust(laserEndPos, 0, 0, 244, dustVel.X, dustVel.Y, 0, beamColor, 3.3f); Main.dust[d].color = beamColor; Main.dust[d].noGravity = true; Main.dust[d].scale = 1.2f; // Scale up dust with the projectile if it's large. if (projectile.scale > 1f) { Main.dust[d].velocity *= projectile.scale; Main.dust[d].scale *= projectile.scale; } // If the beam isn't at max scale, then make additional smaller dust. if (projectile.scale != MaxBeamScale) { Dust smallDust = Dust.CloneDust(d); smallDust.scale /= 2f; } } // Low chance every frame to spawn a large "directly sideways" dust which doesn't move. if (Main.rand.NextBool(5)) { // Velocity, flipped sideways, times -50% to 50% of beam width. Vector2 dustOffset = projectile.velocity.RotatedBy(MathHelper.PiOver2) * (Main.rand.NextFloat() - 0.5f) * projectile.width; Vector2 dustPos = laserEndPos + dustOffset - Vector2.One * SidewaysDustBeamEndOffset; int dustID = 244; int d = Dust.NewDust(dustPos, 8, 8, dustID, 0f, 0f, 100, beamColor, 5f); Main.dust[d].velocity *= 0.5f; // Force the dust to always move downwards, never upwards. Main.dust[d].velocity.Y = -Math.Abs(Main.dust[d].velocity.Y); } } private Color GetBeamColor() { float customHue = GetHue(projectile.ai[0]); float sat = 0.66f; Color c = Main.hslToRgb(customHue, sat, 0.53f); c.A = 64; return c; } // indexing = 0 through N-1 (it's beam ID, stored in projectile.ai[0]) private float GetHue(float indexing) { string name = Main.player[projectile.owner].name ?? ""; if (Main.player[projectile.owner].active) { switch (name) { case "Fabsol": case "Ziggums": return 2f; case "Poly": return 0.83f; case "Zach": return 1.5f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.1f; case "Grox the Great": return 1.27f; case "Jenosis": return 0.65f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.1f; case "DM DOKURO": return 0f; case "Uncle Danny": case "Phoenix": return 1.7f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.07f; case "Minecat": return 0.15f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.07f; case "Khaelis": return 1.15f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.18f; case "Purple Necromancer": return 1.7f + (float)Math.Cos(Main.time / 120.0 * Math.PI * 2.0) * 0.05f; case "gamagamer64": return 0.83f + (float)Math.Cos(Main.time / 120.0 * Math.PI * 2.0) * 0.03f; case "Svante": return 1.4f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.06f; case "Puff": return 0.31f + (float)Math.Cos(Main.time / 120.0 * Math.PI * 2.0) * 0.13f; case "Leviathan": return 1.9f + (float)Math.Cos(Main.time / 180.0 * Math.PI * 2.0) * 0.1f; case "Testdude": return Main.rand.NextFloat(); } } // Something in the range of red to yellow return (indexing / YharimsCrystalPrism.NumBeams) % 0.12f; } // Automatically iterates through every tile the laser is overlapping to cut grass at all those locations. public override void CutTiles() { // tilecut_0 is an unnamed decompiled variable which tells CutTiles how the tiles are being cut (in this case, via a projectile). DelegateMethods.tilecut_0 = TileCuttingContext.AttackProjectile; Utils.PerLinePoint cut = new Utils.PerLinePoint(DelegateMethods.CutTiles); Vector2 beamStartPos = projectile.Center; Vector2 beamEndPos = beamStartPos + projectile.velocity * projectile.localAI[1]; Utils.PlotTileLine(beamStartPos, beamEndPos, projectile.width * projectile.scale, cut); } } }