using System;
using CalamityMod.Particles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Projectiles.BaseProjectiles
{
public abstract class BaseCustomUseStyleProjectile : ModProjectile
{
public override void SetDefaults()
{
Projectile.friendly = true;
Projectile.tileCollide = false;
Projectile.penetrate = -1;
Projectile.noEnchantmentVisuals = true;
Projectile.ContinuouslyUpdateDamageStats = true;
}
public override void OnSpawn(IEntitySource source)
{
Projectile.timeLeft = Owner.HeldItem.useAnimation + 1;
}
#region Fields
///
/// REQUIRED. The item ID that this projectile is linked to.
///
public virtual int AssignedItemID => ItemID.None;
///
/// The offset in pixels of the weapon from the projectile owner's center. This works even while using AbsolutePosition.
///
public Vector2 Offset = Vector2.Zero;
///
/// The Player that is using the projectile.
///
public virtual Player Owner => Main.player[Projectile.owner];
///
/// The amount of pixels out the center of the projectile's hitbox is.
///
public virtual float HitboxOutset => 30;
///
/// The projectile's hitbox size in pixels.
///
public virtual Vector2 HitboxSize => new Vector2(30, 30);
///
/// The number of animations that this projectile has gone through. Useful for things like swing combos.
///
public int NumberOfAnimations = 0;
public float Animation = 0;
///
/// If true, flips the sprite with a 45-degree sword tilt in mind.
///
public bool FlipAsSword = false;
///
/// The offset in radians of the weapon's rotation from its current rotation.
///
public float RotationOffset = 0f;
///
/// The offset in radians of the rotation of the player's front arm.
///
public float ArmRotationOffset = 0f;
///
/// The offset in radians of the rotation of the player's back arm.
///
public float ArmRotationOffsetBack = 0f;
///
/// The amount of animation frames the projectile has in a vertical sheet.
///
public virtual int FrameCount => 1;
///
/// The individual frame currently being drawn in a vertical sheet.
///
public int Frame = 0;
///
/// The origin of the sprite when drawn on your character.
///
///
public virtual Vector2 SpriteOrigin => Projectile.Size / 2;
///
/// Helper method for getting the rotation of the sprite plus RotationOffset, without having to make your own local variable every time.
///
///
public float FinalRotation => Projectile.rotation + RotationOffset;
///
/// Useful for flipping the sprite horizontally or vertically in case of holdouts needing to be flipped based on player direction.
///
public SpriteEffects spriteEffects = SpriteEffects.None;
///
/// Rotation offset for the hitbox that doesn't affect the sprite.
/// Useful for if you have, for instance, a sword sprite at a 45 degree angle, and need that to reflect on the hitbox.
///
///
public virtual float HitboxRotationOffset => 0f;
///
/// Whether or not the projectile can hit enemies at the current frame.
/// Useful for wind-up animations where you don't want the projectile to deal damage.
///
public bool CanHit = true;
///
/// Overrides the projectile owner's center as the projectile's location, so long as it isn't Vector2.Zero.
/// If not equal to Vector2.Zero, this value will be affected by the projectile's velocity just like its normal position.
///
public Vector2 AbsolutePosition = Vector2.Zero;
///
/// If this field is true, the projectile will draw at all times. Otherwise, it will only draw when its associated item is in use.
///
public bool DrawUnconditionally = false;
///
/// The number of frames that the current use animation has progressed through.
///
public float AnimationProgress = 0;
#endregion
#region Use Style
///
/// Determines the behavior to reset to when the item is not in use.
///
public virtual void ResetStyle() { }
///
/// Determines the behavior of the projectile when the item is in use.
///
public virtual void UseStyle() { }
///
/// Determines the behavior of the projectile when the item use animation begins for the first time in a row.
///
public virtual void OnBeginUse() { }
///
/// Determines the behavior of the projectile when the item use animation ends.
///
public virtual void OnEndUse() { }
#endregion
public override void AI()
{
bool ItemAnimationActive = Owner.ItemAnimationActive;
if (Owner.HeldItem.type != AssignedItemID)
{
Projectile.Kill();
}
Owner.Calamity().mouseWorldListener = true;
Owner.Calamity().rightClickListener = true;
if (ItemAnimationActive)
{
Animation++;
UseStyle();
Owner.heldProj = Projectile.whoAmI;
Owner.SetCompositeArmFront(true, Player.CompositeArmStretchAmount.Full, Projectile.rotation + RotationOffset + ArmRotationOffset);
Owner.SetCompositeArmBack(true, Player.CompositeArmStretchAmount.Full, Projectile.rotation + RotationOffset + ArmRotationOffsetBack);
}
else
{
Animation = 0;
if (DrawUnconditionally)
{
Owner.heldProj = Projectile.whoAmI;
Owner.SetCompositeArmFront(true, Player.CompositeArmStretchAmount.Full, Projectile.rotation + RotationOffset + ArmRotationOffset);
Owner.SetCompositeArmBack(true, Player.CompositeArmStretchAmount.Full, Projectile.rotation + RotationOffset + ArmRotationOffsetBack);
}
NumberOfAnimations = 0;
ResetStyle();
}
AnimationProgress = Animation % Owner.itemAnimationMax;
if (AbsolutePosition == Vector2.Zero)
{
Projectile.position = Owner.position + (Owner.Size / 2) - (Projectile.Size / 2) + Offset;
}
else
{
AbsolutePosition += Projectile.velocity;
Projectile.position = AbsolutePosition - (Projectile.Size / 2) + Offset;
}
if (AnimationProgress == Owner.itemAnimationMax - 1)
{
OnEndUse();
NumberOfAnimations++;
}
if (Owner.itemAnimation == Owner.itemAnimationMax - 1)
{
Projectile.timeLeft = Owner.HeldItem.useAnimation + 1;
OnBeginUse();
}
if (DrawUnconditionally) Projectile.timeLeft = Math.Max(Projectile.timeLeft, 2);
}
public override bool? CanHitNPC(NPC target)
{
bool bb = (target.immune[0] <= 0) && !target.friendly && !target.dontTakeDamage;
return bb;
}
public override void ModifyHitNPC(NPC target, ref NPC.HitModifiers modifiers)
{
modifiers.HitDirectionOverride = Owner.direction;
base.ModifyHitNPC(target, ref modifiers);
}
public override bool? CanDamage()
{
return CanHit ? base.CanDamage() : false;
}
public override void ModifyDamageHitbox(ref Rectangle hitbox)
{
Vector2 cen = Projectile.Center + new Vector2(HitboxOutset, 0).RotatedBy(FinalRotation + HitboxRotationOffset);
hitbox = new Rectangle((int)cen.X - (int)(HitboxSize.X / 2), (int)cen.Y - (int)(HitboxSize.Y / 2), (int)HitboxSize.X, (int)HitboxSize.Y);
if (false) // Turn this on to show the hitbox, useful for testing if it's working how you want
{
Particle blastRing = new CustomPulse(hitbox.TopLeft(), Vector2.Zero, Color.Red, "CalamityMod/Particles/LargeBloom", Vector2.One, Main.rand.NextFloat(-10, 10), 0.2f, 0.2f, 4);
GeneralParticleHandler.SpawnParticle(blastRing);
Particle blastRing2 = new CustomPulse(hitbox.TopRight(), Vector2.Zero, Color.Gold, "CalamityMod/Particles/LargeBloom", Vector2.One, Main.rand.NextFloat(-10, 10), 0.2f, 0.2f, 4);
GeneralParticleHandler.SpawnParticle(blastRing2);
Particle blastRing3 = new CustomPulse(hitbox.BottomLeft(), Vector2.Zero, Color.Green, "CalamityMod/Particles/LargeBloom", Vector2.One, Main.rand.NextFloat(-10, 10), 0.2f, 0.2f, 4);
GeneralParticleHandler.SpawnParticle(blastRing3);
Particle blastRing4 = new CustomPulse(hitbox.BottomRight(), Vector2.Zero, Color.Cyan, "CalamityMod/Particles/LargeBloom", Vector2.One, Main.rand.NextFloat(-10, 10), 0.2f, 0.2f, 4);
GeneralParticleHandler.SpawnParticle(blastRing4);
}
base.ModifyDamageHitbox(ref hitbox);
}
public override bool PreDraw(ref Color lightColor)
{
// Only draw the projectile if the projectile's owner is currently using the item this projectile is attached to.
if (Owner.itemAnimation > 0 || DrawUnconditionally)
{
Asset tex = ModContent.Request(Texture);
float r = FlipAsSword ? MathHelper.ToRadians(90) : 0f;
Main.EntitySpriteDraw(tex.Value, Projectile.Center - Main.screenPosition + new Vector2(0, Owner.gfxOffY), tex.Frame(1, FrameCount, 0, Frame), lightColor, Projectile.rotation + RotationOffset + r, FlipAsSword ? new Vector2(tex.Width() - SpriteOrigin.X, SpriteOrigin.Y) : SpriteOrigin, Projectile.scale, spriteEffects != SpriteEffects.None ? spriteEffects : (FlipAsSword ? SpriteEffects.FlipHorizontally : SpriteEffects.None));
}
return false;
}
}
}