using System;
using CalamityMod.CalPlayer;
using Microsoft.Xna.Framework;
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;
///
/// If , the minion will be considered grounded, this will turn on some features that are neccesary for grounded minions.
/// Defaults to .
///
public virtual bool Grounded => false;
///
/// The speed at which the minion falls.
/// It'll take the absolute value of whatever you input, to ensure that it's on Terraria's coordinate system.
/// This is intended to use for grounded minions.
/// Defaults to 0.4f.
///
public float Gravity
{
get => _gravity;
set => _gravity = MathF.Abs(value);
}
private float _gravity = 0.4f;
///
/// The max speed at which the minion falls.
/// It'll take the absolute value of whatever you input, to ensure that it's on Terraria's coordinate system.
/// This is intended to use for grounded minions.
/// Defaults to 20f.
///
public float MaxGravity
{
get => _maxGravity;
set => _maxGravity = MathF.Abs(value);
}
private float _maxGravity = 20f;
///
/// 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 = Grounded;
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();
}
public override bool OnTileCollide(Vector2 oldVelocity) => false;
#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);
// When minions are grounded, they are affected by tiles, meaning they have a much limited movement.
// Pre-Hardmode minions are affected the most since they can't see through walls.
// In consequence, some targets that may seem valid, aren't detected due to this.
// If the minion hasn't detected any targets with the center being the player, we give them a second chance with the center being the minion itself.
// If there are bosses on the world, we don't do this check because if there wasn't any targets with tile X-Ray, this won't do anything.
if (Grounded && Target is null && PreHardmodeMinionTileVision && !CalamityPlayer.areThereAnyDamnBosses)
Target = Projectile.Center.MinionHoming(AdaptiveEnemyDistanceDetection, Owner, false);
}
#endregion
#region Grounded Minion Utilities
///
/// A quick method to apply gravity to grounded minions.
/// You can choose to not use it and make your own if a simple one does not fit your need.
///
public void DoGravity()
{
float speedY = Projectile.velocity.Y;
if (speedY < _maxGravity)
speedY = MathF.Min(speedY + _gravity, _maxGravity);
Projectile.velocity.Y = speedY;
}
///
/// Checks whether there's a tile between two vectors vertically.
/// This method has many shorthand variants.
/// Useful for grounded minion behavior.
///
/// The starter vector. Its X component will be the one used, so put here the vector from where you want to check.
/// The ending vector.
/// Whether you want to check for platforms when is below
/// Returns if there's a tile between and , the X coordinate being the one from ; otherwise .
public bool IsTileBetweenTwoVectorsVertically(Vector2 startVector, Vector2 endVector, bool platformCheckDownwards = false)
{
Point startPoint = CalamityUtils.ToSafeTileCoordinates(startVector);
Point endPoint = CalamityUtils.ToSafeTileCoordinates(endVector);
if (endVector.Y > startVector.Y)
{
for (int coordY = endPoint.Y; coordY >= startPoint.Y; coordY--)
{
if (Main.tile[startPoint.X, coordY].IsTileSolidGround())
return true;
}
}
else
{
for (int coordY = endPoint.Y; coordY <= startPoint.Y; coordY++)
{
if (platformCheckDownwards)
{
if (Main.tile[startPoint.X, coordY].IsTileSolidGround())
return true;
}
else if (Main.tile[startPoint.X, coordY].IsTileSolid())
return true;
}
}
return false;
}
public bool IsTileBetweenOwnerAndVectorVertically(Vector2 vector, bool platformCheckDownwards = false) => IsTileBetweenTwoVectorsVertically(Owner.Center, vector, platformCheckDownwards);
public bool IsTileBetweenTargetAndVectorVertically(Vector2 vector, bool platformCheckDownwards = false) => IsTileBetweenTwoVectorsVertically(Target.Center, vector, platformCheckDownwards);
public bool IsTileBetweenVectorAndMinionVertically(Vector2 vector, bool platformCheckDownwards = false) => IsTileBetweenTwoVectorsVertically(vector, Projectile.Center, platformCheckDownwards);
public bool IsTileBetweenOwnerAndMinionVertically(bool platformCheckDownwards = false) => IsTileBetweenTwoVectorsVertically(Owner.Center, Projectile.Center, platformCheckDownwards);
public bool IsTileBetweenTargetAndMinionVertically(bool platformCheckDownwards = false) => IsTileBetweenTwoVectorsVertically(Target.Center, Projectile.Center, platformCheckDownwards);
///
/// Checks if the minion has a tile in front of him, at their left or right.
///
/// The extra distance it'll check at the left, not all minions are perfectly sized 1 tile, so you'll need to tweak this. Defaults to 0.
/// The extra distance it'll check at the right, not all minions are perfectly sized 1 tile, so you'll need to tweak this. Defaults to 0.
/// if there's a tile at the left or right, otherwise
public bool IsMinionFacingTile(int leftFluff = 0, int rightFluff = 0)
{
Point topLeft = CalamityUtils.ToSafeTileCoordinates(Projectile.TopLeft);
Point bottomLeft = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomLeft);
for (int coordY = topLeft.Y; coordY < bottomLeft.Y; coordY++)
{
if (Main.tile[topLeft.X - leftFluff, coordY].IsTileSolid())
return true;
}
Point topRight = CalamityUtils.ToSafeTileCoordinates(Projectile.TopRight);
Point bottomRight = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomRight);
for (int coordY = topRight.Y; coordY <= bottomRight.Y; coordY++)
{
if (Main.tile[topRight.X + rightFluff, coordY].IsTileSolid())
return true;
}
return false;
}
///
/// Checks whether the minion is standing on a tile or not.
///
/// if the minion's standing on a tile, otherwise
public bool IsMinionStandingOnTile()
{
Point minionPointLeft = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomLeft);
Point minionPointRight = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomRight);
for (int coordX = minionPointLeft.X; coordX <= minionPointRight.X; coordX++)
{
if (Main.tile[coordX, minionPointLeft.Y].IsTileSolidGround())
return true;
}
return false;
}
///
/// Checks whether the minion is inside a tile or not.
/// It checks if there's tiles in the diagonals of the minion's hitbox.
///
/// if there's a tile inside the minion, otherwise
public bool IsMinionInsideTile()
{
Point topLeft = CalamityUtils.ToSafeTileCoordinates(Projectile.TopLeft);
Point bottomRight = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomRight);
for (int coordX = topLeft.X; coordX <= bottomRight.X; coordX++)
{
if (Main.tile[coordX, (int)Utils.Remap(coordX, topLeft.X, bottomRight.X, topLeft.Y, bottomRight.Y)].IsTileSolid())
return true;
}
Point topRight = CalamityUtils.ToSafeTileCoordinates(Projectile.TopRight);
Point bottomLeft = CalamityUtils.ToSafeTileCoordinates(Projectile.BottomLeft);
for (int coordX = bottomLeft.X; coordX >= topRight.X; coordX--)
{
if (Main.tile[coordX, (int)Utils.Remap(coordX, bottomLeft.X, topRight.X, bottomLeft.Y, bottomRight.Y)].IsTileSolid())
return true;
}
return false;
}
#endregion
}
}