using CalamityMod.CalPlayer;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Projectiles.BaseProjectiles
{
///
/// An abstract class that contains the necessary code for a minion to work.
/// Contains useful properties for minions, such as IFrames, Enemy Distance Detection, and sets the Owner and Target.
/// Also has a hook, with basic animation code that can be overriden.
///
public abstract class BaseMinionProjectile : ModProjectile, ILocalizedModType
{
public new string LocalizationCategory => "Projectiles.Summon";
#region Properties
///
/// Input here the correspondent of the minion.
///
public abstract int AssociatedProjectileTypeID { get; }
///
/// Input here the correspondent of the minion.
///
public abstract int AssociatedBuffTypeID { get; }
///
/// Input the reference of the minion bool from with .
/// Example for Elemental Axe:
/// public override ref bool AssociatedMinionBool => ref ModdedOwner.eAxe;
///
public abstract ref bool AssociatedMinionBool { get; }
///
/// The amount of minion slots this summon consumes.
/// Defaults to 1f.
///
public virtual float MinionSlots => 1f;
///
/// The max distance in which the minion can detect an enemy, in pixels.
/// is set to this value.
/// Defaults to 1200f (75 tiles).
///
public virtual float EnemyDistanceDetection => 1200f;
///
/// The min distance in which the minion can detect an enemy before it goes to its correspondent enemy distance detection.
/// Defaults to 960f (60 tiles), the radius of a 1080p monitor at max zoom.
///
public virtual float MinEnemyDistanceDetection => 960f;
private float AdaptiveEnemyDistanceDetection => Target == null ? MinEnemyDistanceDetection : EnemyDistanceDetection;
///
/// The amount of local I-Frames this minion has.
/// Multiplied by so changing the updates won't affect this.
/// Defaults to 10.
///
public int IFrames { get; set; } = 10;
///
/// If , makes the minion only be able to detect and attack enemies through tiles only when there are any bosses alive.
/// Defaults to .
///
public virtual bool PreHardmodeMinionTileVision => false;
///
/// If , makes the minion not target certain NPCs (Like Abyss' or Sunken Sea's enemies) until they're hit.
/// Defaults to .
///
public virtual bool PreventTargettingUntilTargetHit => true;
///
/// The amount of animation frames this minion has.
/// Defaults to 1 frame.
///
public virtual int AnimationFrames => 1;
///
/// The frames that it takes to go to the next frame of animation.
/// Defaults to 5.
///
public int FramesUntilNextAnimationFrame { get; set; } = 5;
///
/// Set here .
/// Defaults to 2.
///
public int TrailingMode { get; set; } = 2;
///
/// Set here .
/// Defaults to 5.
///
public int TrailCacheLength { get; set; } = 5;
public Player Owner { get; set; }
public CalamityPlayer ModdedOwner { get; set; }
public NPC Target { get; set; }
#endregion
public override void SetStaticDefaults()
{
Main.projFrames[Type] = AnimationFrames;
ProjectileID.Sets.MinionTargettingFeature[Type] = true;
ProjectileID.Sets.TrailingMode[Type] = TrailingMode;
ProjectileID.Sets.TrailCacheLength[Type] = TrailCacheLength;
ProjectileID.Sets.DrawScreenCheckFluff[Type] = (int)EnemyDistanceDetection;
}
public override void SetDefaults()
{
Projectile.DamageType = DamageClass.Summon;
Projectile.minionSlots = MinionSlots;
Projectile.penetrate = -1;
Projectile.friendly = true;
Projectile.tileCollide = false;
Projectile.ignoreWater = true;
Projectile.minion = true;
Projectile.usesLocalNPCImmunity = true;
Projectile.netImportant = true;
}
public override bool PreAI()
{
Projectile.Calamity().overridesMinionDamagePrevention = !PreventTargettingUntilTargetHit;
return true;
}
public override void AI()
{
Projectile.localNPCHitCooldown = IFrames * Projectile.MaxUpdates;
SetOwnerTarget();
CheckMinionExistence();
DoAnimation();
MinionAI();
}
#region AI Methods
///
/// Where all of the actual AI of the minion will be placed.
///
public abstract void MinionAI();
///
/// The universal way all minions check if they can still exist.
///
public virtual void CheckMinionExistence()
{
Owner.AddBuff(AssociatedBuffTypeID, 2);
if (Type != AssociatedProjectileTypeID)
return;
if (Owner.dead)
AssociatedMinionBool = false;
if (AssociatedMinionBool)
Projectile.timeLeft = 2;
}
///
/// Basic animation code that the majority of minions use.
/// It will only run if the minion is set to have more than 1 frame of animation.
/// If something more complex is needed, override it. Or leave it blank if not needed at all.
///
public virtual void DoAnimation()
{
if (Main.projFrames[Type] <= 1)
return;
Projectile.frameCounter++;
if (Projectile.frameCounter >= FramesUntilNextAnimationFrame * Projectile.MaxUpdates)
{
Projectile.frameCounter = 0;
Projectile.frame = (Projectile.frame + 1) % Main.projFrames[Type];
}
}
///
/// Where the null property , and is set to a non-null value.
///
public virtual void SetOwnerTarget()
{
Owner = Main.player[Projectile.owner];
ModdedOwner = Owner.Calamity();
Target = Owner.Center.MinionHoming(AdaptiveEnemyDistanceDetection, Owner, !PreHardmodeMinionTileVision || CalamityPlayer.areThereAnyDamnBosses);
}
#endregion
}
}