using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using rail;
using ReLogic.Content;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.ObjectInteractions;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Projectiles.BaseProjectiles
{
///
/// Base class for a whip that handles drawing, AI, and onHit. To make a simple whip, you only need to specify stats in setWhipStats
/// This was originally based off the ExampleMod implementation, but with several changes for ease of subclassing
///
public abstract class BaseWhipProjectile : ModProjectile
{
#region Overridable Properties
//Visual and SFX related variables
///
/// Color of the line connecting whip segments
///
public virtual Color FishingLineColor => Color.White;
///
/// Overrides the color the whips is drawn as. If null, use surrounding tile lighting
///
public virtual Color? DrawColor => null;
///
/// Color of light the whip emits
///
public virtual Color LightingColor => Color.Transparent;
///
/// Type of dust (ID) the whip emits, emits no dust if it is null
///
public virtual int? SwingDust => null;
///
/// How many times the dust spawning loop iterates
///
public virtual int DustAmount => 1;
///
/// If true, the tip of the whip kind of fades away when completing it's arc
///
public virtual bool ShrinkTip => true;
public virtual bool UseTimeDetermineLifetime => true;
public virtual SoundStyle? WhipCrackSound => SoundID.Item153;
//Textures are set to InvisibleProj by default, but this should be changed if you want it to be visible.
public override string Texture => "CalamityMod/Projectiles/InvisibleProj";
public abstract string WhipTipTexture { get;}
///
/// The order of textures in this list is the order which they will appear when drawn in game.
///
public abstract List WhipSegmentTexture { get;}
///
/// UNIMPLEMENTED
/// The Y offset of each whip segment texture, in order of appearance.
/// Length should match WhipSegmentTexture, or be left null.
/// When null, all offsets are set to 0.
/// Defaults to null.
///
public virtual List WhipSegmentYOffset => null;
public abstract string WhipHandleTexture { get; }
public virtual string WhipTipGlowTexture => null;
public virtual List WhipSegmentGlowTexture => null;
public virtual string WhipHandleGlowTexture => null;
//Tag related variables
public virtual int? TagBuffID => null;
public virtual int TagDuration => 240;
//Gameplay variables
///
/// Multiply the whip damage by this amount on hitting an enemy
///
public virtual float? MultihitModifier => .8f;
#endregion
internal List whipPoints = new List();
internal float Timer
{
get => Projectile.ai[0];
set => Projectile.ai[0] = value;
}
public int PointDrawTimer = 0;
public float lineScale = 1;
public int lineCount = 1;
internal Vector2? GetTipPosition()
{
if (whipPoints != null && whipPoints.Count > 2)
return whipPoints[whipPoints.Count - 1];
return null;
}
#region ModProjectile Functions
public override void SetStaticDefaults()
{
// This makes the projectile use whip collision detection and allows flasks to be applied to it.>
ProjectileID.Sets.IsAWhip[Type] = true;
}
public override void SetDefaults()
{
Projectile.friendly = true;
Projectile.penetrate = -1;
Projectile.tileCollide = false;
Projectile.ownerHitCheck = true; // This prevents the projectile from hitting through solid tiles.
Projectile.extraUpdates = 1;
Projectile.usesLocalNPCImmunity = true;
Projectile.localNPCHitCooldown = -1;
Projectile.DamageType = DamageClass.SummonMeleeSpeed;
SetWhipStats();
}
public override bool PreAI()
{
if (PointDrawTimer % 2 < .001)
{
whipPoints.Clear();
Projectile.FillWhipControlPoints(Projectile, whipPoints);
}
return true;
}
public override void AI()
{
WhipAIMotion();
WhipSFX(LightingColor, SwingDust, DustAmount, WhipCrackSound);
}
#endregion
#region Virtual Functions
///
/// Function is use to control custom whip stats, called in the parent class's set defaults
///
public virtual void SetWhipStats()
{
Projectile.width = 20;
Projectile.height = 20;
Projectile.WhipSettings.Segments = 30;
Projectile.WhipSettings.RangeMultiplier = 1f;
}
// This method draws a line between all points of the whip, in case there's empty space between the sprites.
public override bool PreDraw(ref Color lightColor)
{
return DrawWhip(FishingLineColor);
}
public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
{
WhipOnHit(target);
}
///
/// Applies tag buff if there is one, applies multihit penalty, and focuses minions on target.
/// Called in OnHitNPC
///
///
public virtual void WhipOnHit(NPC target)
{
if (TagBuffID != null)
{
target.AddBuff((int)TagBuffID, TagDuration);
}
Projectile.damage = (int)(Projectile.damage * MultihitModifier);
if (Projectile.damage < 1)
{
Projectile.damage = 1;
}
Main.player[Projectile.owner].MinionAttackTargetNPC = target.whoAmI;
}
///
/// Draws whip based on example mod, override if you want custom.
/// Called in PreDraw
///
/// What color the fishing line is
///
public virtual bool DrawWhip(Color lineColor)
{
//Gets every segment of the whip
if (whipPoints == null || whipPoints.Count < 1)
return false;
for (int i = 0; i < lineCount; i++)
CalamityUtils.DrawLineBetweenPoints(whipPoints, lineColor, scaleMod: lineScale - (lineScale / lineCount) * i);
SpriteEffects flip = Projectile.spriteDirection > 0 ? SpriteEffects.None : SpriteEffects.FlipVertically;
Main.instance.LoadProjectile(Type);
//Load projectiles using file paths
Texture2D texture = ModContent.Request(WhipHandleTexture).Value; ;
Texture2D glowtexture = texture;
bool drawGlow = false;
if (WhipHandleGlowTexture != null)
{
glowtexture = ModContent.Request(WhipHandleGlowTexture).Value;
drawGlow = true;
}
//Sets the frame which will be displayed
Rectangle sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
Vector2 origin = sourceRectangle.Size() / 2f;
Vector2 pos = whipPoints[0];
//Repeats for each whip point
for (int i = 0; i < whipPoints.Count; i++)
{
float scale = 1;
//Tip of the whip
if (i == whipPoints.Count - 1)
{
//Sets image to tip texture
texture = ModContent.Request(WhipTipTexture).Value;
//Moves the frame with the animation
sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
origin = sourceRectangle.Size() / 2f;
origin.Y += DrawOriginOffsetY * Projectile.spriteDirection;
origin.X += DrawOriginOffsetX;
drawGlow = false;
if (WhipTipGlowTexture != null)
{
glowtexture = ModContent.Request(WhipTipGlowTexture).Value;
drawGlow = true;
}
// For a more impactful look, this scales the tip of the whip up when fully extended, and down when curled up.
if (ShrinkTip)
{
Projectile.GetWhipSettings(Projectile, out float timeToFlyOut, out int _, out float _);
float t = Timer / timeToFlyOut;
scale = MathHelper.Lerp(0.5f, 1.5f,
Utils.GetLerpValue(0.1f, 0.7f, t, true) * Utils.GetLerpValue(0.9f, 0.7f, t, true)) * Projectile.scale;
}
}
else if (i > 0)
{
//Sets image to segment texture
texture = ModContent.Request(WhipSegmentTexture[i%WhipSegmentTexture.Count]).Value;
//sets the frame accordingly
sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
origin = sourceRectangle.Size() / 2f;
drawGlow = false;
if (WhipSegmentGlowTexture != null)
{
glowtexture = ModContent.Request(WhipSegmentGlowTexture[i % WhipSegmentTexture.Count]).Value;
drawGlow = true;
}
}
Vector2 element = whipPoints[i];
Vector2 diff = (i == whipPoints.Count - 1 ? element- whipPoints[i-1] : whipPoints[i + 1] - element);
float rotation = diff.ToRotation();
//Rotate the handle
if (i == 0)
{
//diff.toRotation makes it follow the rotation of the whip anim
//MathHelper.Pi can be subtracted / added to adjust where the handle is
//Use multiplication as needed to tweak that
rotation = diff.ToRotation();
}
Color color = Lighting.GetColor(element.ToTileCoordinates());
if (DrawColor != null) {
color = (Color)DrawColor;
}
Main.EntitySpriteDraw(texture, pos - Main.screenPosition, sourceRectangle, color, rotation, origin, scale, flip, 0);
if (drawGlow)
{
Main.EntitySpriteDraw(glowtexture, pos - Main.screenPosition, sourceRectangle, Color.White, rotation, origin, scale, flip, 0);
}
pos += diff;
}
return false;
}
bool runOnce = true;
///
/// Runs whip AI similar to example mod, but the center is now on the whip tip. Called in AI
///
public virtual void WhipAIMotion()
{
Player owner = Main.player[Projectile.owner];
float swingTime = owner.itemAnimationMax * Projectile.MaxUpdates;
if (runOnce)
{
Projectile.WhipSettings.Segments = (int)((owner.whipRangeMultiplier + 1) * Projectile.WhipSettings.Segments);
runOnce = false;
}
Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.PiOver2; // Without PiOver2, the rotation would be off by 90 degrees counterclockwise.
Projectile.Center = Vector2.Lerp(Projectile.Center, whipPoints[whipPoints.Count - 1], 1);
// Vanilla uses Vector2.Dot(Projectile.velocity, Vector2.UnitX) here. Dot Product returns the difference between two vectors, 0 meaning they are perpendicular.
// However, the use of UnitX basically turns it into a more complicated way of checking if the projectile's velocity is above or equal to zero on the X axis.
Projectile.spriteDirection = Projectile.velocity.X >= 0f ? 1 : -1;
Timer++;
PointDrawTimer++;
if (Timer >= swingTime || (UseTimeDetermineLifetime && owner.itemAnimation <= 0))
{
Projectile.Kill();
return;
}
}
///
/// Plays sound and runs dust, all the parameters should be set in whip stats, though you can override them.
/// Called in AI
///
///
///
///
///
public virtual void WhipSFX(Color lightingCol, int? dustID, int dustNum, SoundStyle? sound)
{
Player owner = Main.player[Projectile.owner];
float swingTime = owner.itemAnimationMax * Projectile.MaxUpdates;
//Main.NewText(lightingCol);
owner.heldProj = Projectile.whoAmI;
Vector2? tip = GetTipPosition();
if(tip is null)
return;
if (Timer == swingTime / 2 && sound != null)
{
// Plays a whipcrack sound at the tip of the whip.
SoundEngine.PlaySound(sound, tip);
}
if ((Timer >= swingTime * .45f) && Timer <= swingTime * 0.85f)
{
if (dustID != null)
{
for (int i = 0; i < dustNum; i++)
{
Dust.NewDust((Vector2)tip, 2, 2, (int)dustID, 0, 0, Scale: .5f);
}
}
if (lightingCol != Color.Transparent)
{
Lighting.AddLight((Vector2)tip, lightingCol.R / 255f, lightingCol.G / 255f, lightingCol.B / 255f);
}
}
}
#endregion
}
}