From 1e2004fa4ec0331dc2c3e0079cb583a05e8e6a01 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Thu, 13 Feb 2020 00:37:26 -0500 Subject: [PATCH] yharim's crystal 100% done (not balanced!) --- Items/Weapons/Magic/YharimsCrystal.cs | 2 +- Projectiles/Magic/YharimsCrystalBeam.cs | 497 +++++++++++------------ Projectiles/Magic/YharimsCrystalPrism.cs | 154 +++---- 3 files changed, 331 insertions(+), 322 deletions(-) diff --git a/Items/Weapons/Magic/YharimsCrystal.cs b/Items/Weapons/Magic/YharimsCrystal.cs index fdc352bbd9..2af36c1f44 100644 --- a/Items/Weapons/Magic/YharimsCrystal.cs +++ b/Items/Weapons/Magic/YharimsCrystal.cs @@ -17,7 +17,7 @@ namespace CalamityMod.Items.Weapons.Magic { item.damage = 220; item.magic = true; - item.mana = 12; + item.mana = 15; item.width = 16; item.height = 16; item.useTime = 10; diff --git a/Projectiles/Magic/YharimsCrystalBeam.cs b/Projectiles/Magic/YharimsCrystalBeam.cs index 7393fa006a..7bc72d68bb 100644 --- a/Projectiles/Magic/YharimsCrystalBeam.cs +++ b/Projectiles/Magic/YharimsCrystalBeam.cs @@ -13,18 +13,20 @@ namespace CalamityMod.Projectiles.Magic { public class YharimsCrystalBeam : ModProjectile { - private const float PiOver6 = MathHelper.Pi / 6f; + private const float PiBeamDivisor = MathHelper.Pi / YharimsCrystalPrism.NumBeams; - private const float FullDamageMultiplier = 3f; + 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 = 0f; + private const float BeamTileCollisionWidth = 1f; private const float BeamHitboxCollisionWidth = 22f; - private const int NumSamplePoints = 2; - private const float LaserLerpRatio = 0.75f; + 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; @@ -37,30 +39,256 @@ namespace CalamityMod.Projectiles.Magic public override void SetStaticDefaults() { - DisplayName.SetDefault("Yermes Christal"); + DisplayName.SetDefault("Yharim's Beam"); } public override void SetDefaults() { projectile.width = 18; projectile.height = 18; - projectile.friendly = true; 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 = 5; } - public override void SendExtraAI(BinaryWriter writer) + 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() { - writer.Write(projectile.localAI[1]); + // 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)); } - public override void ReceiveExtraAI(BinaryReader reader) + // 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) { - projectile.localAI[1] = reader.ReadSingle(); + 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() @@ -68,15 +296,15 @@ namespace CalamityMod.Projectiles.Magic float customHue = GetHue(projectile.ai[0]); float sat = 0.66f; Color c = Main.hslToRgb(customHue, sat, 0.53f); - c.A = 64; // 127 + c.A = 64; return c; } - // indexing = 0, 1, 2, 3, 4, or 5 (it's beam ID, stored in projectile.ai[0]) + // 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) + if (Main.player[projectile.owner].active) { switch (name) { @@ -114,230 +342,8 @@ namespace CalamityMod.Projectiles.Magic return Main.rand.NextFloat(); } } - return (indexing / 6f) % 0.12f; - } - - // projectile.ai[0] = 0, 1, 2, 3, 4 or 5, 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. - public override void AI() - { - // Sanity check velocity to prevent crashes - if (projectile.velocity.HasNaNs() || projectile.velocity == Vector2.Zero) - projectile.velocity = -Vector2.UnitY; - - // 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); - - // -2.5, -1.5, 0.5, 1.5, or 2.5 based on the beam ID. Used to make the beams orient differently. - float beamIdOffset = (int)projectile.ai[0] - 2.5f; - float projScale; - float beamSpread; - float spinRate; - float beamStartSidewaysOffset; - float beamStartForwardsOffset; - if (hostCrystal.ai[0] < 180f) - { - float chargeRatio = hostCrystal.ai[0] / 180f; - projScale = MathHelper.Lerp(0f, MaxBeamScale, chargeRatio); - beamSpread = MathHelper.Lerp(1.22f, 0f, chargeRatio); - beamStartSidewaysOffset = MathHelper.Lerp(20f, 6f, chargeRatio); - beamStartForwardsOffset = MathHelper.Lerp(-17f, -13f, chargeRatio); - - // From 0 to 120 frames, the opacity scales up from 0% to 40%. - // Spin rate increases slowly for the first 120 frames. - if (hostCrystal.ai[0] < 120f) - { - projectile.Opacity = hostCrystal.ai[0] / 120f * 0.4f; - spinRate = 20f - 4f * (hostCrystal.ai[0] / 120f); - } - - // Between 120 and 180 frames, the opacity scales up from 40% to 100%. - // Spin rate increases dramatically during these frames. - else - { - projectile.Opacity = 0.4f + (hostCrystal.ai[0] - 120f) / 60f * 0.6f; - spinRate = 16f - 10f * ((hostCrystal.ai[0] - 120f) / 60f); - } - } - // Past 180 frames, these variables are fixed in place. - else - { - projScale = 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 beamAngle = (hostCrystal.ai[0] + beamIdOffset * spinRate) / (spinRate * 6f) * MathHelper.TwoPi; - Vector2 unitRot = Vector2.UnitY.RotatedBy(beamAngle); - float sinusoidYOffset = unitRot.Y * PiOver6 * 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 (offset because we're setting projectile position, not center) - projectile.Center = hostCrystal.Center; - // projectile.position = hostCrystal.Center - projectile.Size / 2f; - // Not sure what this step does. - projectile.position += hostCrystalDir * BeamPosOffset + new Vector2(0f, -hostCrystal.gfxOffY); - // Not sure what this step does. - projectile.position += hostCrystal.velocity.ToRotation().ToRotationVector2() * beamStartForwardsOffset; - // Not sure what this step does. - projectile.position += beamSpanVector; - // Not sure what this step does. - projectile.velocity = hostCrystalDir.RotatedBy(sinusoidYOffset); - // Scale the projectile up as the beam continues to be used. - projectile.scale = projScale; - - // Update the damage based on the host crystal damage. - // The host crystal itself updates its damage every frame based on the player's magic damage to account for Mana Sickness. - projectile.damage = hostCrystal.damage; - - // If at full power, the beam deals significantly more damage. - // Additionally, its interpolation starts exactly at the host crystal's center. - if (hostCrystal.ai[0] >= 180f) - projectile.damage = (int)(projectile.damage * FullDamageMultiplier); - - // The beam does not deal damage for the first 30 frames. - projectile.friendly = hostCrystal.ai[0] > 30f; - - // Sanity check the projectile's velocity. - if (projectile.velocity.HasNaNs() || projectile.velocity == Vector2.Zero) - projectile.velocity = -Vector2.UnitY; - - // Rotate the projectile's velocity by 90 degrees. Why this couldn't have been done earlier, I do not know. - float velAngle = projectile.velocity.ToRotation(); - projectile.rotation = velAngle - MathHelper.PiOver2; - projectile.velocity = velAngle.ToRotationVector2(); - - // By default, the interpolation starts at the projectile's center. - // However, if the host crystal is at full power, the interpolation starts at the host crystal's center. - // 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] >= 180f) - 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 and sum the results (not sure what that means...) - float[] laserScanResults = new float[NumSamplePoints]; - Collision.LaserScan(samplingPoint, projectile.velocity, BeamTileCollisionWidth * projectile.scale, MaxBeamLength, laserScanResults); - float sum = 0f; - for (int i = 0; i < laserScanResults.Length; ++i) - sum += laserScanResults[i]; - sum /= NumSamplePoints; - - // Pretty sure this sets the endpoint/length of the beam? - projectile.localAI[1] = MathHelper.Lerp(projectile.localAI[1], sum, LaserLerpRatio); - - // Not sure what this check is, but if it fails the beam doesn't even attempt to draw. - if (Math.Abs(projectile.localAI[1] - sum) < 100f && projectile.scale > 0.15f) - { - Color beamColor = GetBeamColor(); - - // 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 = velAngle + (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, new Color(255, Main.DiscoG, 53), 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); - } - - // X = beam length. Y = beam width. - Vector2 beamDims = new Vector2(projectile.velocity.Length() * projectile.localAI[1], projectile.width * projectile.scale); - - // 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(velAngle); - // Yes, wave data is encoded as a color. I guess someone forgot about Vector3? - 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, velAngle); - } - - // Make the beam cast light along its length. - // v3_1 is an unnamed decompiled variable which is the color of the light cast by DelegateMethods.CastLight - DelegateMethods.v3_1 = beamColor.ToVector3() * BeamLightBrightness; - Utils.PlotTileLine(projectile.Center, projectile.Center + projectile.velocity * projectile.localAI[1], beamDims.Y, new Utils.PerLinePoint(DelegateMethods.CastLight)); - } - } - - public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) - { - // If the beam isn't "moving" or "pointed" anywhere, don't do anything. - if (projectile.velocity == Vector2.Zero) - return false; - - Texture2D tex = Main.projectileTexture[projectile.type]; - float beamLength = projectile.localAI[1]; - Vector2 projCenterTile = 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; - - // Draw the outer beam - // f_1 is an unnamed decompiled variable which is probably the opacity of the beam - DelegateMethods.f_1 = 1f; - Color outerBeamColor = GetBeamColor(); - // c_1 is an unnamed decompiled variable which is the render color of the beam drawn by DelegateMethods.RainbowLaserDraw - DelegateMethods.c_1 = outerBeamColor * OuterBeamOpacityMultiplier * projectile.Opacity; - Utils.LaserLineFraming llf = new Utils.LaserLineFraming(DelegateMethods.RainbowLaserDraw); - Utils.DrawLaser(Main.spriteBatch, tex, projCenterTile - Main.screenPosition, projCenterTile + projectile.velocity * beamLength - Main.screenPosition, scaleVec, llf); - - // Draw the inner beam (half size) - Color innerBeamColor = Color.White; - DelegateMethods.c_1 = innerBeamColor * InnerBeamOpacityMultiplier * projectile.Opacity; - llf = new Utils.LaserLineFraming(DelegateMethods.RainbowLaserDraw); - Utils.DrawLaser(Main.spriteBatch, tex, projCenterTile - Main.screenPosition, projCenterTile + projectile.velocity * beamLength - Main.screenPosition, scaleVec / 2f, llf); - return false; + // 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. @@ -346,18 +352,9 @@ namespace CalamityMod.Projectiles.Magic // 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); - Utils.PlotTileLine(projectile.Center, projectile.Center + projectile.velocity * projectile.localAI[1], projectile.width * projectile.scale, cut); - } - - // 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; - // TODO -- can replace this with a discard - float intersectDist = 0f; - return Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), projectile.Center, projectile.Center + projectile.velocity * projectile.localAI[1], BeamHitboxCollisionWidth * projectile.scale, ref intersectDist); + Vector2 beamStartPos = projectile.Center; + Vector2 beamEndPos = beamStartPos + projectile.velocity * projectile.localAI[1]; + Utils.PlotTileLine(beamStartPos, beamEndPos, projectile.width * projectile.scale, cut); } } } diff --git a/Projectiles/Magic/YharimsCrystalPrism.cs b/Projectiles/Magic/YharimsCrystalPrism.cs index 5e2d0bcc98..23afdb83ac 100644 --- a/Projectiles/Magic/YharimsCrystalPrism.cs +++ b/Projectiles/Magic/YharimsCrystalPrism.cs @@ -9,9 +9,14 @@ namespace CalamityMod.Projectiles.Magic { public class YharimsCrystalPrism : ModProjectile { - private const int AimUpdateFrameInterval = 1; + public const int NumBeams = 6; + public const float MaxCharge = 180f; + public const float DamageStart = 30f; + private const float DustStart = 30f; + private const float AimResponsiveness = 0.89f; // Last Prism is 0.92f. Lower makes the prism turn faster. private const int SoundInterval = 20; - private const float AimResponsiveness = 0.92f; + private const float MaxManaConsumptionDelay = 15f; + private const float MinManaConsumptionDelay = 5f; public override void SetStaticDefaults() { @@ -23,61 +28,29 @@ namespace CalamityMod.Projectiles.Magic { projectile.width = 14; projectile.height = 22; - projectile.friendly = true; projectile.penetrate = -1; projectile.tileCollide = false; projectile.magic = true; projectile.ignoreWater = true; } + // ai[0] is a frame counter. ai[1] is the next frame mana will be consumed. + // localAI[0] is the number of frames between mana consumptions. public override void AI() { Player player = Main.player[projectile.owner]; Vector2 rrp = player.RotatedRelativePoint(player.MountedCenter, true); - float manaConsumptionFrameInterval = 30f; - if (projectile.ai[0] > 90f) // 3 instances of 30 frames - manaConsumptionFrameInterval = 15f; - if (projectile.ai[0] > 120f) // 3 instances of 30 frames + 2 instances of 15 frames - manaConsumptionFrameInterval = 5f; - - // Update beam damage based on curent magic damage stat (so Mana Sickness affects it) + // Update damage based on curent magic damage stat (so Mana Sickness affects it) projectile.damage = (int)((player.HeldItem?.damage ?? 0) * player.MagicDamage()); // ai[0] is the overall frame counter. projectile.ai[0] += 1f; + float chargeRatio = MathHelper.Clamp(projectile.ai[0] / MaxCharge, 0f, 1f); - // ai[1] is the frame counter for how frequently to update the aim of the crystal - projectile.ai[1] += 1f; - - // Every ramp up interval, set these flags to true - bool consumeManaThisFrame = projectile.ai[0] % manaConsumptionFrameInterval == 0f; - bool shouldCastBeams = projectile.ai[0] % manaConsumptionFrameInterval == 0f; - - // Gently adjusts the aim vector of the crystal to point towards the mouse. - if (projectile.ai[1] >= AimUpdateFrameInterval) - { - projectile.ai[1] = 0f; - shouldCastBeams = true; - if (projectile.owner == Main.myPlayer) - { - // Scale seemingly never changes, so this just scales with shoot speed (Yharim's Crystal is 30 by default) - float shootSpeedScaledFactor = player.inventory[player.selectedItem].shootSpeed * projectile.scale; - Vector2 aimVector = Vector2.Normalize(Main.MouseWorld - rrp); - if (aimVector.HasNaNs()) - aimVector = -Vector2.UnitY; - aimVector = Vector2.Normalize(Vector2.Lerp(aimVector, Vector2.Normalize(projectile.velocity), AimResponsiveness)); - aimVector *= shootSpeedScaledFactor; - - if (aimVector != projectile.velocity) - projectile.netUpdate = true; - projectile.velocity = aimVector; - } - } - - // Animate the crystal as it is used + // Update the crystal's animation, with the animation accelerating as the crystal charges projectile.frameCounter++; - int framesPerAnimationUpdate = projectile.ai[0] >= 180f ? 2 : projectile.ai[0] >= 120f ? 3 : 4; + int framesPerAnimationUpdate = projectile.ai[0] >= MaxCharge ? 2 : projectile.ai[0] >= (MaxCharge * 0.66f) ? 3 : 4; if (projectile.frameCounter >= framesPerAnimationUpdate) { projectile.frameCounter = 0; @@ -94,28 +67,21 @@ namespace CalamityMod.Projectiles.Magic Main.PlaySound(SoundID.Item15, projectile.position); } - // Produce dust beyond a certain charge level, with more dust as the crystal continues charging - if(projectile.ai[0] >= 60f && Main.rand.NextFloat(180f) <= projectile.ai[0]) - { - Vector2 projDir = Vector2.Normalize(projectile.velocity); - int dustType = 90; - float dustAngle = MathHelper.Pi * 0.76f * (Main.rand.NextBool() ? 1f : -1f); - float scale = Main.rand.NextFloat(0.9f, 1.2f); - float speed = MathHelper.Clamp(projectile.ai[0] / 10f, 0f, 18f); - Vector2 dustVel = projDir.RotatedBy(dustAngle) * speed; - float dustForwardOffset = 11f; - Vector2 dustOrigin = projectile.Center + dustForwardOffset * projDir; - Dust d = Dust.NewDustDirect(dustOrigin, 1, 1, dustType, dustVel.X, dustVel.Y); - d.position += Main.rand.NextVector2Circular(2f, 2f); - d.noGravity = true; - d.scale = scale; - } + // Once the crystal reaches a certain charge, start producing dust. More charge = more dust. + if (projectile.ai[0] > DustStart && Main.rand.NextFloat() < chargeRatio) + SpawnEjectionDust(chargeRatio); - // Update the crystal's existence: project beams if no beams exist yet, and despawn if out of mana. - if (shouldCastBeams && Main.myPlayer == projectile.owner) + UpdatePlayerVisuals(player, rrp); + + // Update the crystal's existence: project beams on frame 1, and despawn if out of mana. + if (projectile.owner == Main.myPlayer) { + // Scale seemingly never changes, so this just scales with shoot speed (Yharim's Crystal is 30 by default) + float speedTimesScale = player.inventory[player.selectedItem].shootSpeed * projectile.scale; + UpdateAim(rrp, speedTimesScale); + // CheckMana returns true if the mana cost can be paid. If mana isn't consumed this frame, the CheckMana short-circuits out of being evaluated. - bool allowContinuedUse = !consumeManaThisFrame || player.CheckMana(player.inventory[player.selectedItem].mana, true, false); + bool allowContinuedUse = !ShouldConsumeMana() || player.CheckMana(player.inventory[player.selectedItem].mana, true, false); bool crystalStillInUse = player.channel && allowContinuedUse && !player.noItems && !player.CCed; // The beams are only projected once (on frame 1). @@ -127,7 +93,7 @@ namespace CalamityMod.Projectiles.Magic int damage = projectile.damage; float kb = projectile.knockBack; // should always be 0 - for (int b = 0; b < 6; b++) + for (int b = 0; b < NumBeams; b++) Projectile.NewProjectile(projectile.Center, beamVelocity, ModContent.ProjectileType(), damage, kb, projectile.owner, b, projectile.whoAmI); projectile.netUpdate = true; } @@ -135,14 +101,50 @@ namespace CalamityMod.Projectiles.Magic projectile.Kill(); } + // Ensures the crystal will disappear immediately if anything goes wrong + projectile.timeLeft = 2; + } + + private bool ShouldConsumeMana() + { + // If the mana consumption timer hasn't been initialized yet, initialize it and consume mana on frame 1. + if (projectile.localAI[0] == 0f) + { + projectile.ai[1] = projectile.localAI[0] = MaxManaConsumptionDelay; + return true; + } + bool consume = projectile.ai[0] == projectile.ai[1]; + if(consume) + { + projectile.localAI[0] = MathHelper.Clamp(projectile.localAI[0] - 1f, MinManaConsumptionDelay, MaxManaConsumptionDelay); + projectile.ai[1] += projectile.localAI[0]; + } + return consume; + } + + // Gently adjusts the aim vector of the crystal to point towards the mouse. + private void UpdateAim(Vector2 source, float speed) + { + Vector2 aimVector = Vector2.Normalize(Main.MouseWorld - source); + if (aimVector.HasNaNs()) + aimVector = -Vector2.UnitY; + aimVector = Vector2.Normalize(Vector2.Lerp(aimVector, Vector2.Normalize(projectile.velocity), AimResponsiveness)); + aimVector *= speed; + + if (aimVector != projectile.velocity) + projectile.netUpdate = true; + projectile.velocity = aimVector; + } + + private void UpdatePlayerVisuals(Player player, Vector2 rrp) + { // Place the projectile directly into the player's hand at all times - projectile.position = rrp - projectile.Size / 2f; + projectile.Center = rrp; // The beam comes out of the tip of the crystal, not the side projectile.rotation = projectile.velocity.ToRotation() + MathHelper.PiOver2; projectile.spriteDirection = projectile.direction; - // Ensures the crystal will disappear immediately if the left mouse button is not held anymore - projectile.timeLeft = 2; - // This is a holdout projectile, so change the player's variables to reflect that + + // The crystal is a holdout projectile, so change the player's variables to reflect that player.ChangeDir(projectile.direction); player.heldProj = projectile.whoAmI; player.itemTime = 2; @@ -150,22 +152,32 @@ namespace CalamityMod.Projectiles.Magic player.itemRotation = (float)Math.Atan2(projectile.velocity.Y * projectile.direction, projectile.velocity.X * projectile.direction); } - // Completely custom drawcode + private void SpawnEjectionDust(float charge) + { + Vector2 projDir = Vector2.Normalize(projectile.velocity); + int dustType = 90; + float dustAngle = MathHelper.Pi * 0.76f * (Main.rand.NextBool() ? 1f : -1f); + float scale = Main.rand.NextFloat(0.9f, 1.2f); + float speed = 18f * charge; + Vector2 dustVel = projDir.RotatedBy(dustAngle) * speed; + float dustForwardOffset = 11f; + Vector2 dustOrigin = projectile.Center + dustForwardOffset * projDir; + Dust d = Dust.NewDustDirect(dustOrigin, 1, 1, dustType, dustVel.X, dustVel.Y); + d.position += Main.rand.NextVector2Circular(2f, 2f); + d.noGravity = true; + d.scale = scale; + } + + // Completely custom drawcode because it's a holdout projectile. The projectile is also fullbright. public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { - Point enclosingTile = projectile.Center.ToTileCoordinates(); SpriteEffects eff = projectile.spriteDirection == -1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; Texture2D tex = Main.projectileTexture[projectile.type]; int frameHeight = Main.projectileTexture[projectile.type].Height / Main.projFrames[projectile.type]; int texYOffset = frameHeight * projectile.frame; - Vector2 sheetInsertVec = (projectile.Center + Vector2.UnitY * projectile.gfxOffY - Main.screenPosition).Floor(); - - // Draw the crystal itself Main.spriteBatch.Draw(tex, sheetInsertVec, new Rectangle?(new Rectangle(0, texYOffset, tex.Width, frameHeight)), Color.White, projectile.rotation, new Vector2(tex.Width / 2f, frameHeight / 2f), projectile.scale, eff, 0f); return false; } - - public override Color? GetAlpha(Color lightColor) => base.GetAlpha(lightColor); } }