using System.Collections.Generic; using System.Linq; using CalamityMod.Graphics.Primitives; using CalamityMod.NPCs; using CalamityMod.Systems.Collections; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.DataStructures; using Terraria.GameContent.Prefixes; using Terraria.Graphics.Shaders; using Terraria.ID; using Terraria.ModLoader; using Terraria.Audio; namespace CalamityMod.Projectiles.BaseProjectiles { public class BaseSwordHoldoutPlayer : ModPlayer { /// /// Used to detect what swing a weapon is doing. /// public int swingNum = 0; } /// /// Manages all the required settings for custom sword holdouts done using BaseSwordHoldoutProjectile /// public abstract class BaseSwordHoldoutItem : ModItem { public virtual int ProjectileType { get; set; } public virtual bool SizeModifiers { get; set; } = true; public virtual bool RClickAutoswing { get; set; } = false; public override void SetStaticDefaults() { if (RClickAutoswing) ItemID.Sets.ItemsThatAllowRepeatedRightClick[Type] = true; } public override bool MeleePrefix() { return SizeModifiers; } public override void SetDefaults() { Item.noMelee = true; Item.noUseGraphic = true; Item.shoot = ProjectileType; Item.autoReuse = true; Item.useTurn = false; Item.useStyle = ItemUseStyleID.Shoot; if (SizeModifiers) PrefixLegacy.ItemSets.SwordsHammersAxesPicks[Item.type] = true; } public override bool CanUseItem(Player player) { if (player.itemTime > 0) { return false; } for (int i = 0; i < 1000; i++) { var proj = Main.projectile[i]; if (proj.type == ProjectileType && proj.owner == player.whoAmI && proj.active) { return false; } } return base.CanUseItem(player); } } [PierceResistException] //Doze 24apr2025 - Not a child of BaseCustomUseStyleProjectile because this doesn't use any of it's functionality. Comes from my melee rework mod. public abstract class BaseSwordHoldoutProjectile : ModProjectile { #region Overrideable Fields /// /// The width, in degrees, of the sword swing. /// Defaults to 180 /// public virtual int swingWidth { get; set; } = 180; /// /// How many frames the sword swing should take. /// Defaults to 20 /// public virtual int swingTime { get; set; } = 20; /// /// If the swing should alternate directions each use. /// Defaults to true /// public virtual bool AlternateSwings { get; set; } = true; /// /// How far the held sword should be offset from the player /// Defaults to 0 /// public virtual int OffsetDistance { get; set; } = 0; /// /// What item this projectile uses as a base. /// public virtual Item BaseItem { get; set; } /// /// Whether or not this projectile uses a base item at all. /// Defaults to true. /// public virtual bool UsesBaseItem { get; set; } = true; /// /// Length of after-image trail left by the projectile. /// Defaults to 0 /// public virtual int AfterImageLength { get; set; } = 0; /// /// Whether or not this should get melee speed bonuses /// Defaults to TRUE /// public virtual bool useMeleeSpeed { get; set; } = true; /// /// Whether or not this should get melee size bonuses (Titan Glove) /// Defaults to TRUE /// public virtual bool useMeleeSize { get; set; } = true; /// /// Array of three colors used for the built-in trail drawing code. /// Defaults to White, Black, Green. /// public virtual Color[] trailColors { get; set; } = [Color.White, Color.Black, Color.Green]; /// /// Whether or not to draw a trail when swung. /// public virtual bool drawSwordTrail { get; set; } = false; /// /// How far the trail should be offset from the center of the projectile. Use to draw trail at sword tip /// Defaults to 25 /// public virtual float trailOffset { get; set; } = 25; /// /// Length of the trail in frames /// Defaults to 25 /// public virtual int trailLength { get; set; } = 25; /// /// How long before the weapon should begin it's actual swing once used /// public virtual int StartupTime { get; set; } = 0; /// /// How long the weapon should "cool down" after swinging before ending the item use /// public virtual int CooldownTime { get; set; } = 0; /// /// Speed at which the projectile should rotate to match the mouse angle during StartupTime. /// Set to 0 to disable. /// Defaults to 0.5f /// public virtual float RotateInStartup { get; set; } = 0.5f; /// /// What sound to use when the sword begins the actual swing (after startup frames) /// public virtual SoundStyle? UseSound {get; set;} = null; #endregion #region Fields /// /// Angle of the swing. By default, gets set to mouse angle. Can be set in Spawn(IEntitySource) for fixed angles. /// public Vector2 angle { get; set; } = Vector2.Zero; /// /// Internal timer for the projectile's entire lifespan /// public int timer { get; set; } = 0; /// /// Internal timer for the projectile's swing animation /// internal int swingTimer = 0; public static Asset TrailTexture; public float baseScale; /// /// Old weapon scales used to track for trail drawing /// public List oldScale = new List(); List oldProjectileRot = new List { }; List oldProjectilePos = new List { }; internal int ExistsTime = 20; public bool inStartup => timer < StartupTime; public bool inCooldown => timer > CooldownStartFrame; public bool inSwing => !(inStartup || inCooldown); public int CooldownStartFrame => swingTime + StartupTime; public int CooldownTimer => timer - CooldownStartFrame; public float StartupCompletion => timer / (float)StartupTime; public float SwingCompletion => swingTimer / (float)swingTime; public float CooldownCompletion => CooldownTimer / (float)CooldownTime; #endregion #region Overridable Methods /// /// Happens after movement but before timer increases. Use as to not cancel the default AI behavior. /// public virtual void AdditionalAI() { } /// /// happens after OnSpawn. Use as not to cancel the default OnSpawn behavior. /// /// public virtual void Spawn(IEntitySource source) { } /// /// happens after SetDefaults. Use as not to cancel default SetDefaults behavior. /// public virtual void Defaults() { } /// /// Returns the swing offset from the center angle in radians. Automatically will be inverted if AlternateSwings is enabled. /// /// public virtual float SwingFunction() { return MathHelper.ToRadians(MathHelper.SmoothStep(-swingWidth / 2, swingWidth / 2, swingTimer / (float)swingTime)); } /// /// The function for the width of the trail /// /// /// public virtual float trailWidth(float completion) { return MathHelper.Lerp(30, 0, completion); } /// /// The function for the color of the trail /// /// /// public virtual Color trailColor(float completion) { return Color.Black; } #endregion #region Overrides public override void SetStaticDefaults() { ProjectileDestroyExceptionsList.List.Add(Projectile.type); } /// /// DO NOT OVERRIDE IN MOST SITUATIONS /// use Defaults() instead. /// That will set defaults after everything is set in the base projectile. /// public override void SetDefaults() { Projectile.timeLeft = swingTime * 2; if (UsesBaseItem) { Projectile.width = BaseItem.width; Projectile.height = BaseItem.height; } Projectile.friendly = true; Projectile.penetrate = -1; Projectile.localNPCHitCooldown = -1; Projectile.usesLocalNPCImmunity = true; Projectile.extraUpdates = 0; Projectile.aiStyle = -2; Projectile.DamageType = ModLoader.GetMod("CalamityMod").Find("TrueMeleeDamageClass"); Projectile.tileCollide = false; ProjectileID.Sets.TrailingMode[Projectile.type] = 2; ProjectileID.Sets.TrailCacheLength[Projectile.type] = 100; Defaults(); } /// /// DO NOT OVERRIDE IN MOST SITUATIONS /// use Spawn(IEntitySource) instead /// That will run spawn code in the proper timing. /// public override void OnSpawn(IEntitySource source) { var player = Main.player[Projectile.owner]; angle = (player.Center - Main.MouseWorld).SafeNormalize(Vector2.One); Projectile.velocity = Vector2.Zero; if (angle.X < 0) { player.direction = 1; Projectile.spriteDirection = 1 * (int)player.gravDir; } else { player.direction = -1; Projectile.spriteDirection = -1 * (int)player.gravDir; } if (AlternateSwings && player.GetModPlayer().swingNum % 2 == 0) { Projectile.spriteDirection *= -1; } swingTime = Main.player[Projectile.owner].HeldItem.useTime; Spawn(source); StartupTime *= Projectile.MaxUpdates; CooldownTime *= Projectile.MaxUpdates; swingTime *= Projectile.MaxUpdates; if (useMeleeSpeed) { var speed = Main.player[Projectile.owner].GetAttackSpeed(); if (speed > 3f) speed = 3f; if (speed != 0f) speed = 1f / speed; swingTime = (int)(swingTime * speed); if (swingTime < 1) { swingTime = 1; } StartupTime = (int)(StartupTime * speed); CooldownTime = (int)(CooldownTime * speed); } if (useMeleeSize) { if (player.meleeScaleGlove) Projectile.scale *= 1.1f; Projectile.scale *= player.HeldItem.scale; } baseScale = Projectile.scale; ExistsTime = swingTime + StartupTime + CooldownTime; Projectile.timeLeft = ExistsTime * 2; } /// /// DO NOT OVERRIDE IN MOST SITUATIONS /// use AdditionalAI() instead /// That will run AI code at the right time. /// public override void AI() { var player = Main.player[Projectile.owner]; var modplayer = player.GetModPlayer(); float adust = MathHelper.ToRadians(225); if (timer < StartupTime || timer > StartupTime + swingTime) { angle = Vector2.Lerp(angle, (player.Center - Main.MouseWorld).SafeNormalize(Vector2.One), RotateInStartup); if (angle.X < 0) { player.direction = 1; Projectile.spriteDirection = 1 * (int)player.gravDir; } else { player.direction = -1; Projectile.spriteDirection = -1 * (int)player.gravDir; } } if (Projectile.spriteDirection == -1) { adust = MathHelper.ToRadians(-45); } var armCenter = player.Center - new Vector2(5 * player.direction, 2); if (AfterImageLength > 0) { oldProjectileRot.Add(Projectile.rotation); oldProjectilePos.Add(Projectile.Center); if (oldProjectileRot.Count > AfterImageLength) { oldProjectileRot.RemoveAt(0); oldProjectilePos.RemoveAt(0); } } if (inSwing && swingTimer == 1 && UseSound != null) { SoundEngine.PlaySound((SoundStyle)UseSound,player.Center); } var angle2 = (AlternateSwings && (modplayer.swingNum % 2 == 1 ? false : true) ? SwingFunction() : SwingFunction()); Projectile.Center = armCenter - (angle * OffsetDistance * (1 + (Projectile.scale - 1) * 0.75f)).RotatedBy(Projectile.spriteDirection * angle2); Projectile.rotation = angle.RotatedBy(Projectile.spriteDirection * angle2).ToRotation() + adust; AdditionalAI(); player.itemTime = ExistsTime + 2 - timer; player.itemAnimation = ExistsTime + 2 - timer; if (timer > ExistsTime) { player.itemTime = 0; player.itemAnimation = 0; Projectile.Kill(); } timer++; if (timer >= StartupTime && timer < StartupTime + swingTime) { swingTimer++; } var armDir = armCenter - Projectile.Center; armDir.Y *= player.gravDir; player.SetCompositeArmFront(true, Player.CompositeArmStretchAmount.Full, armDir.ToRotation() + MathHelper.ToRadians(90)); oldScale.Insert(0, Projectile.scale); } public override bool PreDraw(ref Color lightColor) { var player = Main.player[Projectile.owner]; var modplayer = player.GetModPlayer(); if (AfterImageLength > 0) { Texture2D texture = ModContent.Request(Texture).Value; for (int i = 0; i < oldProjectileRot.Count; i++) { var col = Projectile.Opacity * (i / (float)AfterImageLength) * 0.1f; if (Projectile.spriteDirection == 1) { Main.EntitySpriteDraw(texture, oldProjectilePos[i] - Main.screenPosition, null, Color.White * col, oldProjectileRot[i], texture.Size() / 2, 1, SpriteEffects.None, 0); } else { Main.EntitySpriteDraw(texture, oldProjectilePos[i] - Main.screenPosition, null, Color.White * col, oldProjectileRot[i], texture.Size() / 2, 1, SpriteEffects.FlipHorizontally, 0); } } } if (drawSwordTrail && timer >= StartupTime && timer <= StartupTime + swingTime) { Main.spriteBatch.EnterShaderRegion(); if (TrailTexture == null) { TrailTexture = ModContent.Request("CalamityMod/ExtraTextures/GreyscaleGradients/VoronoiShapes", (AssetRequestMode)2); } Vector2 trailOffset = (Projectile.rotation - MathHelper.PiOver4).ToRotationVector2() + Projectile.Size * 0.5f; GameShaders.Misc["CalamityMod:ExobladeSlash"].SetShaderTexture(TrailTexture); GameShaders.Misc["CalamityMod:ExobladeSlash"].UseColor(trailColors[0]); GameShaders.Misc["CalamityMod:ExobladeSlash"].UseSecondaryColor(trailColors[1]); GameShaders.Misc["CalamityMod:ExobladeSlash"].Shader.Parameters["fireColor"].SetValue(trailColors[2].ToVector3()); GameShaders.Misc["CalamityMod:ExobladeSlash"].Shader.Parameters["flipped"].SetValue(Projectile.spriteDirection == -1 ? false : true); GameShaders.Misc["CalamityMod:ExobladeSlash"].Apply(); var positionsToUse = Projectile.oldPos.Take((int)MathHelper.Min(trailLength,swingTimer)).ToArray(); for (var i = 0; i < positionsToUse.Length; i++) { if (i >= timer) break; positionsToUse[i] += (Projectile.oldRot[i] - MathHelper.PiOver4 * (Projectile.spriteDirection == -1 ? 3 : 1)).ToRotationVector2() * this.trailOffset * oldScale[i]; } PrimitiveRenderer.RenderTrail(positionsToUse, new(trailWidth, trailColor, (_) => trailOffset, shader: GameShaders.Misc["CalamityMod:ExobladeSlash"]), 25); Main.spriteBatch.ExitShaderRegion(); Main.player[Projectile.owner].heldProj = Projectile.whoAmI; } Main.player[Projectile.owner].heldProj = Projectile.whoAmI; return true; } public override void ModifyDamageHitbox(ref Rectangle hitbox) { var center = hitbox.Center.ToVector2(); hitbox.Height = (int)(Projectile.height * Projectile.scale); hitbox.Width = (int)(Projectile.width * Projectile.scale); hitbox.Location = (center - new Vector2(hitbox.Width / 2, hitbox.Height / 2)).ToPoint(); } public override void ModifyHitNPC(NPC target, ref NPC.HitModifiers modifiers) { modifiers.HitDirectionOverride = ((Main.player[Projectile.owner].DirectionTo(target.Center)).X >= 0 ? 1 : -1); } public override bool? CanDamage() { return inSwing; } #endregion #region Helper Methods /// /// Spawns projectiles from the sword swing, automatically spacing them out on the if an amount is set. /// /// /// Velocity of the projectile. Automatically converted into a Vector2 in the direction of swing. /// The modifier from the source projectile's damage for this projectile /// The amount of projectiles for the sword to shoot. If set, it will space out those projectiles evenly. If unset, will force a shot. /// /// public void shootCheck(int type = 0, float velocity = 1, float damagemod = 1, int amount = 0, int negate = 0, int ai0 = 0) { if (negate == 0) { negate = Projectile.spriteDirection; } if (amount == 0) { Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, (Projectile.rotation + negate * MathHelper.ToRadians(45 - negate * 90)).ToRotationVector2() * velocity, type, (int)(Projectile.damage * damagemod), Projectile.knockBack, Projectile.owner, ai0); return; } amount += 1; if (swingTimer % (swingTime / amount) == 0 && swingTimer > 0 && swingTimer < swingTime - swingTime / amount / 2) { Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, (Projectile.rotation + negate * MathHelper.ToRadians(45 - negate * 90)).ToRotationVector2() * velocity, type, (int)(Projectile.damage * damagemod), Projectile.knockBack, Projectile.owner, ai0); } } #endregion } }