Decimation_Mod/Lib/Util/ProjectileUtils.cs
2020-07-13 15:22:20 -04:00

64 lines
2.2 KiB
C#

using Microsoft.Xna.Framework;
using Terraria;
namespace Decimation.Lib.Util
{
public static class ProjectileUtils
{
public static bool Richochet(this Terraria.Projectile projectile, Player player, bool fromPlayer,
ref int damage)
{
// if (Main.expertMode && Main.rand.NextBool(10) &&
// target.GetModPlayer().HasEquippedAccessory(ModContent.ItemType<Body>()))
// {
Entity newTarget = null;
float lastDistance = float.MaxValue;
if (fromPlayer)
{
for (int i = 0; i < Main.player.Length; i++)
{
Player targetPlayer = Main.player[i];
if (player != null && player.active && !player.dead && player.whoAmI != targetPlayer.whoAmI)
{
float distance = projectile.Distance(player.Center);
if (distance < 1000f && distance < lastDistance)
{
lastDistance = distance;
newTarget = player;
}
}
}
}
else
{
for (int i = 0; i < Main.npc.Length; i++)
{
NPC npc = Main.npc[i];
if (npc != null && npc.active && !npc.friendly)
{
float distance = projectile.Distance(npc.Center);
if (distance < 1000f && distance < lastDistance)
{
lastDistance = distance;
newTarget = npc;
}
}
}
}
if (newTarget == null) return false;
float speed = projectile.velocity.Length();
Vector2 velocity = newTarget.Center - projectile.Center;
velocity.Normalize();
velocity *= speed;
projectile.velocity = velocity;
projectile.damage = (int) (projectile.damage * 1.5f);
damage = 0;
return true;
}
}
}