using System; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.DataStructures; using Terraria.GameContent; using Terraria.Localization; using Terraria.ModLoader; namespace CalamityMod.Projectiles.BaseProjectiles { /// /// An abstract class dedicated to ease the making of gun-like holdouts, regarding of the weapon class.
/// All the AI code goes to .
/// Sets the texture, localization and SetDefaults automatically.
/// You also have convenient properties for offsets, arm visuals and the tip of the gun.
/// You must name your holdout's class the name of the item's class plus "Holdout" at the end, you'll get errors otherwise. ///
public abstract class BaseGunHoldoutProjectile : ModProjectile, ILocalizedModType { #region Overridable Properties /// /// The ID of the item that this holdout belongs to. /// public abstract int AssociatedItemID { get; } /// /// The position of the gun tip, used for spawning the holdout's shots.
/// Override if it isn't aligned properly. For example: when the tip is a bit upwards. ///
public virtual Vector2 GunTipPosition => Projectile.Center + Vector2.UnitX.RotatedBy(Projectile.rotation) * Projectile.width * 0.5f; /// /// How fast returns back to .
/// In consequence, this is how fast the holdout goes back to its position.
/// Defaults to 0.3f. ///
public virtual float RecoilResolveSpeed => 0.3f; /// /// The distance of the holdout to the arm.
/// Used to properly position the weapon to be held on the player's arms. ///
public virtual float MaxOffsetLengthFromArm { get; } /// /// The offset in the X-axis of the holdout when the player's pointing upwards. /// public virtual float OffsetXUpwards { get; } /// /// The offset in the X-axis of the holdout when the player's pointing downwards. /// public virtual float OffsetXDownwards { get; } /// /// The base offset on the Y-axis of the holdout.
/// Used to align the holdout when pointing horizontally. ///
public virtual float BaseOffsetY { get; } /// /// The offset in the Y-axis of the holdout when the player's pointing upwards. /// public virtual float OffsetYUpwards { get; } /// /// The offset in the Y-axis of the holdout when the player's pointing downwards. /// public virtual float OffsetYDownwards { get; } #endregion #region Properties /// /// The owner of this holdout.
/// It's set on . ///
public Player Owner { get; private set; } /// /// The item that the player's holding, correspondent to .
/// It's set on . ///
public Item HeldItem { get; private set; } /// /// Whether the holdout will have its set 2 constantly.
/// Used when you want to keep it alive for a bit longer after it should despawn.
/// Defaults to . ///
public bool KeepRefreshingLifetime { get; set; } = true; /// /// The current offset length from the arm of the holdout.
/// Used to make recoil effects by setting this to a lower value than . ///
public float OffsetLengthFromArm { get; set; } /// /// Extra rotation added to the front arm if you want to make it be held cooler or more naturally.
/// Direction is already taken care of, so you don't need to multiply anything. ///
public float ExtraFrontArmRotation { get; set; } /// /// Extra rotation added to the back arm if you want to make it be held cooler or more naturally.
/// Direction is already taken care of, so you don't need to multiply anything. ///
public float ExtraBackArmRotation { get; set; } /// /// The visual stretch of the owner's front arm.
/// Defaults to . ///
public Player.CompositeArmStretchAmount FrontArmStretch { get; set; } = Player.CompositeArmStretchAmount.Full; /// /// The visual stretch of the owner's back arm.
/// Defaults to . ///
public Player.CompositeArmStretchAmount BackArmStretch { get; set; } = Player.CompositeArmStretchAmount.Full; private Type AssociatedItemType => ItemLoader.GetItem(AssociatedItemID).GetType(); private Asset ItemTexture => TextureAssets.Item[AssociatedItemID]; #endregion #region Overridden Members // Gets the display name of the associated item. public override LocalizedText DisplayName => CalamityUtils.GetItemName(AssociatedItemID); // Gets the texture of the associated item. public override string Texture => (AssociatedItemType.Namespace + "." + AssociatedItemType.Name).Replace('.', '/'); public override void SetDefaults() { Projectile.width = Projectile.height = ItemTexture is null ? 1 : ItemTexture.Width(); Projectile.tileCollide = false; Projectile.netImportant = true; } public override void OnSpawn(IEntitySource source) => OffsetLengthFromArm = MaxOffsetLengthFromArm; public override bool ShouldUpdatePosition() => false; public override bool? CanDamage() => false; #endregion #region AI public override void AI() { // Multiplayer null-checking. Owner ??= Main.player[Projectile.owner]; HeldItem ??= Owner.ActiveItem(); KillHoldoutLogic(); ManageHoldout(); HoldoutAI(); } /// /// Here goes the code for when the holdout needs to despawn.
/// Defaults to despawning when the owner isn't using the correspondent weapon. ///
public virtual void KillHoldoutLogic() { if (Owner.CantUseHoldout()) Projectile.Kill(); } /// /// Does all the needed code for a holdout to work. /// public virtual void ManageHoldout() { // The center of the player, taking into account if they have a mount or not. Vector2 armPosition = Owner.RotatedRelativePoint(Owner.MountedCenter, true); // The vector between the player and the mouse, used for pointing the holdout. Vector2 ownerToMouse = Owner.Calamity().mouseWorld - armPosition; // The direction this holdout's pointing at. float holdoutDirection = Projectile.velocity.ToRotation(); // A range from -1 to 1 for when the holdout is pointing downards of upwards, respectively. // Used for the offsets. float proximityLookingUpwards = Vector2.Dot(ownerToMouse.SafeNormalize(Vector2.Zero), -Vector2.UnitY * Owner.gravDir); int direction = MathF.Sign(ownerToMouse.X); Vector2 lengthOffset = Projectile.rotation.ToRotationVector2() * OffsetLengthFromArm; Vector2 armOffset = new Vector2(Utils.Remap(MathF.Abs(proximityLookingUpwards), 0f, 1f, 0f, proximityLookingUpwards > 0f ? OffsetXUpwards : OffsetXDownwards) * direction, BaseOffsetY * Owner.gravDir + Utils.Remap(MathF.Abs(proximityLookingUpwards), 0f, 1f, 0f, proximityLookingUpwards > 0f ? OffsetYUpwards : OffsetYDownwards) * Owner.gravDir); Projectile.Center = armPosition + lengthOffset + armOffset; Projectile.velocity = holdoutDirection.AngleTowards(ownerToMouse.ToRotation(), 0.2f).ToRotationVector2(); Projectile.rotation = holdoutDirection; Projectile.spriteDirection = direction; Owner.ChangeDir(direction); Owner.heldProj = Projectile.whoAmI; Owner.itemTime = Owner.itemAnimation = 2; Owner.itemRotation = (Projectile.velocity * Projectile.direction).ToRotation(); // -Pi/2 because the arms rotation starts with arms pointing down. float armRotation = (Projectile.rotation - MathHelper.PiOver2) * Owner.gravDir + (Owner.gravDir == -1 ? MathHelper.Pi : 0f); Owner.SetCompositeArmFront(true, FrontArmStretch, armRotation + ExtraFrontArmRotation * direction); Owner.SetCompositeArmBack(true, BackArmStretch, armRotation + ExtraBackArmRotation * direction); if (KeepRefreshingLifetime) Projectile.timeLeft = 2; if (OffsetLengthFromArm != MaxOffsetLengthFromArm) OffsetLengthFromArm = MathHelper.Lerp(OffsetLengthFromArm, MaxOffsetLengthFromArm, RecoilResolveSpeed); Projectile.netUpdate = true; Projectile.netSpam = 0; } /// /// Where all the actual AI goes to. /// public abstract void HoldoutAI(); #endregion #region Drawing public override bool PreDraw(ref Color lightColor) { Texture2D texture = TextureAssets.Projectile[Type].Value; Vector2 drawPosition = Projectile.Center - Main.screenPosition; float drawRotation = Projectile.rotation + (Projectile.spriteDirection == -1 ? MathHelper.Pi : 0f); Vector2 rotationPoint = texture.Size() * 0.5f; SpriteEffects flipSprite = (Projectile.spriteDirection * Owner.gravDir == -1) ? SpriteEffects.FlipHorizontally : SpriteEffects.None; Main.EntitySpriteDraw(texture, drawPosition, null, Projectile.GetAlpha(lightColor), drawRotation, rotationPoint, Projectile.scale * Owner.gravDir, flipSprite); return false; } #endregion #region Syncing public sealed override void SendExtraAI(BinaryWriter writer) { writer.Write(Projectile.rotation); writer.Write(KeepRefreshingLifetime); writer.Write(OffsetLengthFromArm); writer.Write7BitEncodedInt(Projectile.spriteDirection); SendExtraAIHoldout(writer); } public sealed override void ReceiveExtraAI(BinaryReader reader) { Projectile.rotation = reader.ReadSingle(); KeepRefreshingLifetime = reader.ReadBoolean(); OffsetLengthFromArm = reader.ReadSingle(); Projectile.spriteDirection = reader.Read7BitEncodedInt(); ReceiveExtraAIHoldout(reader); } /// /// The common is sealed to avoid deleting syncing of other properties without noticing.
/// Use this method instead, which uses the same . ///
/// public virtual void SendExtraAIHoldout(BinaryWriter writer) { } /// /// The common is sealed to avoid deleting syncing of other properties without noticing.
/// Use this method instead, which uses the same . ///
/// public virtual void ReceiveExtraAIHoldout(BinaryReader reader) { } #endregion } }