using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using Terraria; using Terraria.Audio; using Terraria.GameContent; using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; namespace CalamityMod.Projectiles.BaseProjectiles { // Adapted from ExampleMod's ExampleAdvancedFlailProjectile (which in turn also adapted from Vanilla's AI Style 15) public abstract class BaseMaceFlailProjectile : ModProjectile { public abstract int AssociatedItemID { get; } // The following can also be used in each individual projectile class, without the need to re-define: #region Utilities public enum FlailState { Spinning = 0, LaunchingForward = 1, Retracting = 2, ForcedRetracting = 4, Ricochet = 5, Dropping = 6 } public FlailState CurrentFlailState { get => (FlailState)Projectile.ai[0]; set => Projectile.ai[0] = (float)value; } public ref float StateTimer => ref Projectile.ai[1]; public ref float CollisionCounter => ref Projectile.localAI[0]; // ai[2] and localAI[1] are completely open for additional gimmicks. public Player Owner => Main.player[Projectile.owner]; #endregion // Core properties of the mace's function. #region Properties /// /// Local immunity frames granted while dropped or retracted
/// Defaults to 10. ///
public virtual int BaseIFrames => 10; /// /// Local immunity frames granted during the spin.
/// Defaults to 20. ///
public virtual int SpinIFrames => 20; /// /// Local immunity frames granted while launched or ricochet.
/// Defaults to 10. ///
public virtual int LaunchIFrames => 10; /// /// The rate of which the mace visually spins around the player. Does NOT influence any other aspects.
/// Defaults to 10f. ///
public virtual float SpinSpeed => 10f; /// /// Horizontal radius for the mace's hitbox while spinning.
/// Defaults to 55f. (3.4375 tiles) ///
public virtual float SpinHitboxRadius => 55f; /// /// The distance between the projectile and the player's centers while spinning.
/// Defaults to 30f. (1.875 tiles) /// public virtual float SpinVisualRadius => 30f; /// /// Effect which runs as soon as a spinning mace launches. Does nothing by default. /// public virtual Action EffectBeforeLaunch => null; /// /// Max amount of afterimages, which is only drawn while the mace is launched.
/// Defaults to 6. ///
public virtual int AfterimageLength => 6; /// /// Constant speed when launched, affected by attack speed.
/// Defaults to 16f. ///
public virtual float LaunchSpeed { get; set; } = 16f; /// /// How long the mace launches before retracting. Multiply by its speed to determine its base range.
/// Defaults to 15. (0.25 seconds) ///
public virtual int LaunchLifespan => 15; /// /// Max launch range before forcibly being set to retract.
/// Typically not useful as it should retract naturally through lifespan before doing so by force.
/// Defaults to 960f. (60 tiles) ///
public virtual float MaxLaunchRange => 960f; /// /// Max drop range before forcibly being set to retract.
/// Defaults to 400f. (25 tiles) ///
public virtual float MaxDropRange => 400f; /// /// Effect which runs as soon as a launched mace retracts. Does nothing by default. /// public virtual Action EffectBeforePullback => null; /// /// When hitting a tile while launched, how long the ricochet lasts before dropping. /// Defaults to the launch's lifespan added by 5. (20 / 0.33 seconds) /// public virtual int RicochetLifespan => LaunchLifespan + 5; /// /// Max speed when retracted, affected by attack speed.
/// Defaults to 15f. ///
public virtual float MaxRetractSpeed { get; set; } = 15f; /// /// Acceleration when retracted, affected by attack speed.
/// Defaults to 2.5f. ///
public virtual float RetractAcceleration { get; set; } = 2.5f; #endregion // Most of these should be kept default for consistency with vanilla maces. // However, they can be overridden anyway. #region Overridable Values /// /// File path for the mace's chain. These should just be the name of the projectile with "Chain" appended at the end.
/// Custom maces may also elect to not draw chains. Simply override this value with string.Empty. ///
public virtual string ChainTexturePath => Texture + "Chain"; /// /// Damage multiplier while spinning. Defaults to 1.2f. /// public virtual float SpinDamage => 1.2f; /// /// Knockback multiplier while spinning. Defaults to 0.25f. /// public virtual float SpinKnockback => 0.25f; /// /// The vertical multiplier which makes the spin motion elliptical. Defaults to 0.8f. /// public virtual float SpinVerticalFactor => 0.8f; /// /// Damage multiplier while launching or non-forced retracting. Defaults to 2f. /// public virtual float LaunchDamage => 2f; /// /// Knockback multiplier while dropping. Defaults to 0.5f. /// public virtual float DropKnockback => 0.5f; #endregion // Methods for most individual behaviour steps which can be useful to override. #region Overridable Methods /// /// Scales every value that should be affected by melee speed.
/// Can be overridden to leave out certain variables. ///
public virtual void ScaleWithMeleeSpeed(ref float launchSpeed, ref float maxSpeed, ref float acceleration) { float meleeSpeedMultiplier = Owner.GetTotalAttackSpeed(DamageClass.Melee); launchSpeed *= meleeSpeedMultiplier; maxSpeed *= meleeSpeedMultiplier; acceleration *= meleeSpeedMultiplier; } /// /// Calculates the damage and knockback multiplier based on the current behaviour.
/// Can be overridden to include any extra stat adjustments. ///
public virtual void UpdateDamageKB(out float damageMult, out float kbMult) { damageMult = 1f; kbMult = 1f; if (CurrentFlailState == FlailState.Spinning) { damageMult = SpinDamage; kbMult = SpinKnockback; } else if (CurrentFlailState == FlailState.LaunchingForward || CurrentFlailState == FlailState.Retracting) damageMult = LaunchDamage; else if (CurrentFlailState == FlailState.Dropping) kbMult = DropKnockback; } public virtual void SpinAI(float launchSpeed) { Projectile.ownerHitCheck = true; if (Projectile.owner == Main.myPlayer) { Vector2 toMouse = Owner.MountedCenter.DirectionTo(Main.MouseWorld).SafeNormalize(Vector2.UnitX * Owner.direction); Owner.ChangeDir((toMouse.X > 0f).ToDirectionInt()); if (!Owner.channel) { CurrentFlailState = FlailState.LaunchingForward; StateTimer = 0f; Projectile.Center = Owner.MountedCenter; Projectile.velocity = toMouse * launchSpeed + Owner.velocity; Projectile.netUpdate = true; Projectile.ResetLocalNPCHitImmunity(); Projectile.localNPCHitCooldown = LaunchIFrames * Projectile.MaxUpdates; Projectile.ownerHitCheck = false; EffectBeforeLaunch?.Invoke(Projectile); return; } } StateTimer++; Vector2 spinOffset = new Vector2(Owner.direction).RotatedBy(MathHelper.Pi * SpinSpeed * (StateTimer / 60f) * Owner.direction); spinOffset.Y *= SpinVerticalFactor; if (spinOffset.Y * Owner.gravDir > 0f) spinOffset.Y *= 0.5f; Projectile.Center = Owner.MountedCenter + spinOffset * SpinVisualRadius + new Vector2(0, Owner.gfxOffY); Projectile.velocity = Vector2.Zero; Projectile.localNPCHitCooldown = SpinIFrames * Projectile.MaxUpdates; } public virtual void LaunchAI() { bool shouldRetract = StateTimer++ >= LaunchLifespan; shouldRetract |= Projectile.Distance(Owner.MountedCenter) >= MaxLaunchRange; if (Owner.controlUseItem) // If the player clicks, transition to the Dropping state { CurrentFlailState = FlailState.Dropping; StateTimer = 0f; Projectile.netUpdate = true; Projectile.velocity *= 0.2f; return; } if (shouldRetract) { CurrentFlailState = FlailState.Retracting; StateTimer = 0f; Projectile.netUpdate = true; Projectile.velocity *= 0.3f; EffectBeforePullback?.Invoke(Projectile); } Owner.ChangeDir((Owner.Center.X < Projectile.Center.X).ToDirectionInt()); Projectile.localNPCHitCooldown = LaunchIFrames * Projectile.MaxUpdates; } public virtual void RetractAI(bool forced, float maxSpeed, float acceleration) { if (forced) { Projectile.tileCollide = false; Projectile.ignoreWater = true; } float forceMult = forced ? 2f : 1f; Vector2 toPlayer = Projectile.SafeDirectionTo(Owner.MountedCenter); Vector2 value = Owner.MountedCenter.DirectionFrom(Projectile.Center + Projectile.velocity).SafeNormalize(Vector2.Zero); if (Projectile.Distance(Owner.MountedCenter) <= maxSpeed * forceMult || (forced && Vector2.Dot(toPlayer, value) < 0f)) { Projectile.Kill(); return; } if (Owner.controlUseItem && !forced) // If the player clicks, transition to the Dropping state { CurrentFlailState = FlailState.Dropping; StateTimer = 0f; Projectile.netUpdate = true; Projectile.velocity *= 0.2f; return; } Projectile.velocity *= 0.98f; Projectile.velocity = Projectile.velocity.MoveTowards(toPlayer * maxSpeed * forceMult, acceleration * forceMult); Owner.ChangeDir((Owner.Center.X < Projectile.Center.X).ToDirectionInt()); } public virtual void RicochetAI() { if (StateTimer++ >= RicochetLifespan) { CurrentFlailState = FlailState.Dropping; StateTimer = 0f; Projectile.netUpdate = true; return; } Projectile.localNPCHitCooldown = LaunchIFrames * Projectile.MaxUpdates; Projectile.velocity.Y += 0.6f; Projectile.velocity.X *= 0.95f; Owner.ChangeDir((Owner.Center.X < Projectile.Center.X).ToDirectionInt()); } public virtual void DropAI() { if (!Owner.controlUseItem || Projectile.Distance(Owner.MountedCenter) > MaxDropRange) { CurrentFlailState = FlailState.ForcedRetracting; StateTimer = 0f; Projectile.netUpdate = true; EffectBeforePullback?.Invoke(Projectile); return; } Projectile.localNPCHitCooldown = BaseIFrames * Projectile.MaxUpdates; Projectile.velocity.Y += 0.8f; Projectile.velocity.X *= 0.95f; Owner.ChangeDir((Owner.Center.X < Projectile.Center.X).ToDirectionInt()); } /// /// Reserved for extra AI that is always ran regardless of state. Does nothing by default.
/// Example cases include shooting projectiles in a similar manner to Flower Pow. ///
public virtual void ExtraBehavior() { } /// /// Handles simple chain drawing. Can be overridden for cases such as chains with multiple frames/variants. /// Example cases include Drippler Cripper's chain variants. /// public virtual void DrawChain() { Texture2D Chain = ModContent.Request(ChainTexturePath).Value; Vector2 playerArmPosition = Main.GetPlayerArmPosition(Projectile); // This fixes a vanilla GetPlayerArmPosition bug causing the chain to draw incorrectly when stepping up slopes. The flail itself still draws incorrectly due to another similar bug. // This should be removed once the vanilla bug is fixed. playerArmPosition.Y -= Owner.gfxOffY; Vector2 chainPos = Projectile.Center; Vector2 toArms = playerArmPosition.MoveTowards(chainPos, 4f) - chainPos; float chainSegmentLength = MathF.Max(1f, Chain.Height); float rotation = toArms.ToRotation() + MathHelper.PiOver2; float chainsLeft = toArms.Length() + chainSegmentLength * 0.5f; while (chainsLeft > 0f) { Color chainDrawColor = Lighting.GetColor((int)(chainPos.X / 16f), (int)(chainPos.Y / 16f)); Main.spriteBatch.Draw(Chain, chainPos - Main.screenPosition, null, chainDrawColor, rotation, Chain.Size() * 0.5f, 1f, SpriteEffects.None, 0f); chainPos += toArms.SafeNormalize(Vector2.Zero) * chainSegmentLength; chainsLeft -= chainSegmentLength; } } #endregion #region Hook Overrides public override LocalizedText DisplayName => CalamityUtils.GetItemName(AssociatedItemID); public override void SetStaticDefaults() { ProjectileID.Sets.TrailCacheLength[Type] = AfterimageLength; ProjectileID.Sets.TrailingMode[Type] = 2; ProjectileID.Sets.HeldProjDoesNotUsePlayerGfxOffY[Type] = true; } public override void SetDefaults() { Projectile.netImportant = true; Projectile.friendly = true; Projectile.penetrate = -1; Projectile.DamageType = DamageClass.Melee; Projectile.usesLocalNPCImmunity = true; Projectile.localNPCHitCooldown = BaseIFrames * Projectile.MaxUpdates; } public override void AI() { if (!Owner.active || Owner.dead || Owner.noItems || Owner.CCed || Vector2.Distance(Projectile.Center, Owner.Center) > MaxLaunchRange + 160f) { Projectile.Kill(); return; } if (Main.myPlayer == Projectile.owner && Main.mapFullscreen) { Projectile.Kill(); return; } float launchSpeed = LaunchSpeed; float maxRetractSpeed = MaxRetractSpeed; float retractAcceleration = RetractAcceleration; ScaleWithMeleeSpeed(ref launchSpeed, ref maxRetractSpeed, ref retractAcceleration); switch (CurrentFlailState) { case FlailState.Spinning: SpinAI(launchSpeed); break; case FlailState.LaunchingForward: LaunchAI(); break; case FlailState.Retracting: RetractAI(false, maxRetractSpeed, retractAcceleration); break; case FlailState.ForcedRetracting: RetractAI(true, maxRetractSpeed, retractAcceleration); break; case FlailState.Ricochet: RicochetAI(); break; case FlailState.Dropping: DropAI(); break; } ExtraBehavior(); Projectile.spriteDirection = Projectile.direction = (Projectile.velocity.X > 0f).ToDirectionInt(); // Non-symmetric rotation (symmetric is just in this if condition here) if (CurrentFlailState == FlailState.Ricochet || CurrentFlailState == FlailState.Dropping) { if (Projectile.velocity.Length() > 1f) Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.Pi + Projectile.velocity.X * 0.1f; else Projectile.rotation += Projectile.velocity.X * 0.1f; } else { Vector2 vectorTowardsPlayer = Projectile.SafeDirectionTo(Owner.MountedCenter); Projectile.rotation = vectorTowardsPlayer.ToRotation() + MathHelper.ToRadians(270f); } Projectile.timeLeft = 2; Owner.heldProj = Projectile.whoAmI; Owner.SetDummyItemTime(2); Owner.itemRotation = Projectile.DirectionFrom(Owner.MountedCenter).ToRotation(); if (Projectile.Center.X < Owner.MountedCenter.X) Owner.itemRotation += MathHelper.Pi; Owner.itemRotation = MathHelper.WrapAngle(Owner.itemRotation); } public override bool OnTileCollide(Vector2 oldVelocity) { int impactIntensity = 0; Vector2 velocity = Projectile.velocity; float bounceFactor = 0.2f; if (CurrentFlailState == FlailState.LaunchingForward || CurrentFlailState == FlailState.Ricochet) bounceFactor = 0.4f; if (CurrentFlailState == FlailState.Dropping) bounceFactor = 0f; if (oldVelocity.X != Projectile.velocity.X) { if (Math.Abs(oldVelocity.X) > 4f) impactIntensity = 1; Projectile.velocity.X = oldVelocity.X * -bounceFactor; CollisionCounter++; } if (oldVelocity.Y != Projectile.velocity.Y) { if (Math.Abs(oldVelocity.Y) > 4f) impactIntensity = 1; Projectile.velocity.Y = oldVelocity.Y * -bounceFactor; CollisionCounter++; } if (CurrentFlailState == FlailState.LaunchingForward) { CurrentFlailState = FlailState.Ricochet; Projectile.localNPCHitCooldown = BaseIFrames * Projectile.MaxUpdates; Projectile.netUpdate = true; Point scanAreaStart = Projectile.TopLeft.ToTileCoordinates(); Point scanAreaEnd = Projectile.BottomRight.ToTileCoordinates(); impactIntensity = 2; Projectile.CreateImpactExplosion(2, Projectile.Center, ref scanAreaStart, ref scanAreaEnd, Projectile.width, out bool causedShockwaves); Projectile.CreateImpactExplosion2_FlailTileCollision(Projectile.Center, causedShockwaves, velocity); Projectile.position -= velocity; } if (impactIntensity > 0) { Projectile.netUpdate = true; for (int i = 0; i < impactIntensity; i++) Collision.HitTiles(Projectile.position, velocity, Projectile.width, Projectile.height); SoundEngine.PlaySound(SoundID.Dig, Projectile.position); } // Force retraction if stuck on tiles while retracting if (CurrentFlailState != FlailState.Spinning && CurrentFlailState != FlailState.Ricochet && CurrentFlailState != FlailState.Dropping && CollisionCounter >= 10f) { CurrentFlailState = FlailState.ForcedRetracting; Projectile.netUpdate = true; } return false; } public override bool? CanDamage() { if (CurrentFlailState == FlailState.Spinning && StateTimer <= 12f) return false; return base.CanDamage(); } public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) { if (CurrentFlailState == FlailState.Spinning) { Vector2 distance = targetHitbox.ClosestPointInRect(Owner.MountedCenter) - Owner.MountedCenter; distance.Y /= SpinVerticalFactor; return distance.Length() <= SpinHitboxRadius; } return base.Colliding(projHitbox, targetHitbox); } public override void ModifyHitNPC(NPC target, ref NPC.HitModifiers modifiers) { UpdateDamageKB(out float damageMult, out float kbMult); modifiers.SourceDamage *= damageMult; modifiers.Knockback *= kbMult; modifiers.HitDirectionOverride = (Owner.Center.X < target.Center.X).ToDirectionInt(); } public override bool PreDraw(ref Color lightColor) { if (!String.IsNullOrEmpty(ChainTexturePath)) DrawChain(); // By default, maces have afterimages when launched. // This can still be disabled via config or set individually by the projectile. // (Does not use utils due to set trail length conditions) if (CurrentFlailState == FlailState.LaunchingForward && CalamityClientConfig.Instance.Afterimages && AfterimageLength > 0) { Texture2D maceTex = TextureAssets.Projectile[Type].Value; Vector2 drawOrigin = new Vector2(maceTex.Width * 0.5f, maceTex.Height * 0.5f); SpriteEffects spriteEffects = Projectile.spriteDirection == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; for (int k = 0; k < Projectile.oldPos.Length && k < StateTimer; k++) { Vector2 drawPos = Projectile.oldPos[k] - Main.screenPosition + drawOrigin * Projectile.scale + new Vector2(0f, Projectile.gfxOffY); Color color = Projectile.GetAlpha(lightColor) * ((float)(Projectile.oldPos.Length - k) / (float)Projectile.oldPos.Length); Main.spriteBatch.Draw(maceTex, drawPos, null, color, Projectile.rotation, drawOrigin, Projectile.scale - k / (float)Projectile.oldPos.Length / 3, spriteEffects, 0f); } } return true; } #endregion } }