using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Projectiles.Damageable
{
[Flags]
public enum DamageSourceType
{
HostileProjectiles = 1 << 1,
FriendlyProjectiles = 1 << 2, // Does NOT include itself.
HostileNPCs = 1 << 3
}
public abstract class DamageableProjectile : ModProjectile
{
#region General Fields
///
/// Current life of the projectile.
///
public int Life;
///
/// Total damage immunity frames. This projectile cannot be hit if this is greater than 0. Descends to 0 every frame.
///
public int DamageImmunityFrames;
#endregion
#region Overridable Fields/Properties
///
/// Total life of the projectile.
///
public abstract int LifeMax
{
get;
}
///
/// The sound the projectile makes on hit.
///
public abstract SoundStyle HitSound
{
get;
}
///
/// The sound the projectile makes on death.
///
public abstract SoundStyle DeathSound
{
get;
}
///
/// The various possibile damage sources for the projectile.
///
public abstract DamageSourceType DamageSources
{
get;
}
///
/// A boolean which determines if the HP bar should be drawn at all.
///
public virtual bool DrawHPBar
{
get;
set;
} = true;
///
/// A boolean which determines if the HP bar should be drawn if is equal to
///
public virtual bool DrawHPBarAtFullHealth
{
get;
set;
} = false;
///
/// How many damage immunity frames this projectile should recieve on hit.
///
public virtual int MaxDamageImmunityFrames
{
get;
set;
} = 10;
///
/// List of all enemies this projectile should ignore when checking if it's colliding with an enemy (assuming this is possible).
///
public virtual List NPCsToIgnore
{
get;
set;
} = new List();
///
/// List of all projectiles this projectile should ignore when checking if it's colliding with a projectile (assuming this is possible).
///
public virtual List ProjectilesToIgnore
{
get;
set;
} = new List();
#endregion
#region Virtual Methods + Sealed Counterparts
///
/// Copy of with integrations for this custom type.
///
public virtual void SafeSetDefaults()
{
}
public sealed override void SetDefaults()
{
SafeSetDefaults();
Life = LifeMax;
}
public sealed override void PostDraw(Color lightColor)
{
DrawHealthBar();
MouseOverText();
SafePostDraw(Main.spriteBatch, lightColor);
}
///
/// Copy of with integrations for this custom type.
///
public virtual void SafePostDraw(SpriteBatch spriteBatch, Color lightColor)
{
}
public sealed override void AI()
{
SafeAI();
if (DamageImmunityFrames > 0)
{
DamageImmunityFrames--;
return;
}
bool wasHit = NPCCollisionCheck();
if (!wasHit)
{
wasHit = ProjectileCollisionCheck();
}
if (wasHit && Life > 0)
{
if (HitSound != null)
{
SoundEngine.PlaySound(HitSound, Projectile.Center);
}
}
else if (Life <= 0)
{
if (DeathSound != null)
{
SoundEngine.PlaySound(DeathSound, Projectile.Center);
}
DamageKillEffect();
Projectile.Kill();
}
}
public virtual bool NPCCollisionCheck()
{
Player player = Main.player[Projectile.owner];
foreach (NPC n in Main.ActiveNPCs)
{
if (n.IsAnEnemy() && DamageSources.HasFlag(DamageSourceType.HostileNPCs))
{
if (n.Hitbox.Intersects(Projectile.Hitbox) && !NPCsToIgnore.Contains(n.type))
{
int damage = Main.DamageVar(n.damage);
int bannerBuffId = Item.NPCtoBanner(n.BannerID());
if (bannerBuffId > 0 && player.HasNPCBannerBuff(bannerBuffId))
{
if (Main.expertMode)
{
damage = (int)(damage * ItemID.Sets.BannerStrength[Item.BannerToItem(bannerBuffId)].ExpertDamageReceived);
}
else
{
damage = (int)(damage * ItemID.Sets.BannerStrength[Item.BannerToItem(bannerBuffId)].NormalDamageReceived);
}
}
CombatText.NewText(new Rectangle((int)Projectile.position.X, (int)Projectile.position.Y, Projectile.width, Projectile.height), CombatText.DamagedFriendly, damage);
Life -= damage;
HitEffectNPC(damage, n);
DamageImmunityFrames = MaxDamageImmunityFrames;
NetUpdate(true);
return true;
}
}
}
return false;
}
public virtual bool ProjectileCollisionCheck()
{
foreach (Projectile p in Main.ActiveProjectiles)
{
bool canBeHit = (p.friendly && DamageSources.HasFlag(DamageSourceType.FriendlyProjectiles) ||
p.hostile && DamageSources.HasFlag(DamageSourceType.HostileProjectiles)) &&
p.whoAmI != Projectile.whoAmI;
if (canBeHit && p.damage > 0)
{
if (p.Colliding(p.Hitbox, Projectile.Hitbox))
{
int damage = Main.DamageVar(p.damage) * 2;
damage = Main.expertMode ? (int)(damage * Main.RegisteredGameModes[GameModeID.Expert].EnemyDamageMultiplier) : damage;
CombatText.NewText(new Rectangle((int)Projectile.position.X, (int)Projectile.position.Y, Projectile.width, Projectile.height), CombatText.DamagedFriendly, damage);
Life -= damage;
HitEffectProjectile(damage, p);
DamageImmunityFrames = MaxDamageImmunityFrames;
if (Projectile.usesIDStaticNPCImmunity)
DamageImmunityFrames = Projectile.idStaticNPCHitCooldown;
if (Projectile.usesLocalNPCImmunity)
DamageImmunityFrames = Projectile.localNPCHitCooldown;
NetUpdate(true);
return true;
}
}
}
return false;
}
public virtual void DamageKillEffect()
{
}
///
/// Copy of with integrations for this custom type.
///
public virtual void SafeAI()
{
}
public sealed override void SendExtraAI(BinaryWriter writer)
{
SafeSendExtraAI(writer);
writer.Write(Life);
writer.Write(DamageImmunityFrames);
}
public sealed override void ReceiveExtraAI(BinaryReader reader)
{
SafeReceiveExtraAI(reader);
Life = reader.ReadInt32();
DamageImmunityFrames = reader.ReadInt32();
}
///
/// Copy of with integrations for this custom type.
/// The writer used to hold data which will be sent to the server.
///
public virtual void SafeSendExtraAI(BinaryWriter writer)
{
}
///
/// Copy of with integrations for this custom type.
/// The reader used to obtain data which will be sent to the server.
///
public virtual void SafeReceiveExtraAI(BinaryReader reader)
{
}
///
/// Syncs data from this npc to the server, and then to all clients.
///
/// Whether net spam should be ignored and a net update should be forced.
/// The condition required for a forceful sync. Defaults to: Main.netMode != NetmodeID.MultiplayerClient
public virtual void NetUpdate(bool force = false, Func forceCondition = null)
{
if (forceCondition == null)
forceCondition = () => Main.netMode != NetmodeID.MultiplayerClient;
if (force)
{
if (forceCondition())
{
NetMessage.SendData(MessageID.SyncProjectile, number: Projectile.whoAmI);
}
}
else
Projectile.netUpdate = true;
}
///
/// Does things when the projectile is hit.
///
/// The damage taken by the projectile.
/// The projectile which hit the target.
public virtual void HitEffectProjectile(int damage, Projectile target)
{
}
///
/// Does things when the projectile is hit.
///
/// The damage taken by the projectile.
/// The npc which hit the target.
public virtual void HitEffectNPC(int damage, NPC target)
{
}
#endregion
#region Misc Functions
///
/// Creates text, much akin to NPCs, when a mouse is over the projectile's hitbox.
///
public void MouseOverText()
{
if (!Main.mouseText)
{
Rectangle mouseRectangle = new Rectangle((int)(Main.mouseX + Main.screenPosition.X), (int)(Main.mouseY + Main.screenPosition.Y), 1, 1);
if (Main.LocalPlayer.gravDir == -1f)
{
mouseRectangle.Y = (int)Main.screenPosition.Y + Main.screenHeight - Main.mouseY;
}
if (Projectile.Hitbox.Intersects(mouseRectangle))
{
if (LifeMax > 1)
{
string lifeDataText = string.Concat(new object[]
{
Projectile.Name,
": ",
Life,
"/",
LifeMax
});
Main.spriteBatch.End();
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, null, null, null, Main.UIScaleMatrix);
Main.instance.MouseTextHackZoom(lifeDataText);
}
}
}
}
///
/// Draws a health bar below the projectile.
///
public void DrawHealthBar()
{
if (Life == LifeMax && !DrawHPBarAtFullHealth)
return;
Main.spriteBatch.End();
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, null, null, null, Main.UIScaleMatrix);
Main.instance.DrawHealthBar(Projectile.Bottom.X, Projectile.Bottom.Y, Life, LifeMax, 1f, 1f);
}
#endregion
}
}