88 lines
2.9 KiB
C#
88 lines
2.9 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Terraria;
|
|
using Terraria.DataStructures;
|
|
using Terraria.ID;
|
|
using Terraria.ModLoader;
|
|
using ForgottenWawiTech.Content.GlobalProjectiles;
|
|
|
|
namespace ForgottenWawiTech.Content.Projectiles.Protogen
|
|
{
|
|
public class ProtogenSentinelFragment : ModProjectile
|
|
{
|
|
private const float HomingRange = 560f;
|
|
private const float HomingStrength = 0.18f;
|
|
private const float DesiredSpeed = 15f;
|
|
|
|
public override void SetStaticDefaults()
|
|
{
|
|
ProjectileID.Sets.MinionShot[Type] = true;
|
|
}
|
|
|
|
public override void SetDefaults()
|
|
{
|
|
Projectile.width = 14;
|
|
Projectile.height = 14;
|
|
Projectile.friendly = true;
|
|
Projectile.penetrate = 1;
|
|
Projectile.timeLeft = 240;
|
|
Projectile.tileCollide = false;
|
|
Projectile.ignoreWater = true;
|
|
Projectile.DamageType = DamageClass.Generic;
|
|
Projectile.extraUpdates = 1;
|
|
Projectile.usesLocalNPCImmunity = true;
|
|
Projectile.localNPCHitCooldown = 15;
|
|
}
|
|
|
|
public override void AI()
|
|
{
|
|
Projectile.rotation = Projectile.velocity.ToRotation();
|
|
Lighting.AddLight(Projectile.Center, 0.3f, 0.8f, 1.15f);
|
|
|
|
NPC target = AcquireTarget(Projectile.Center, HomingRange);
|
|
if (target != null)
|
|
{
|
|
Vector2 desiredVelocity = (target.Center - Projectile.Center).SafeNormalize(Vector2.Zero) * DesiredSpeed;
|
|
Projectile.velocity = Vector2.Lerp(Projectile.velocity, desiredVelocity, HomingStrength);
|
|
}
|
|
else
|
|
{
|
|
Projectile.velocity *= 0.99f;
|
|
}
|
|
|
|
if (Main.rand.NextBool(4))
|
|
{
|
|
Dust dust = Dust.NewDustDirect(Projectile.position, Projectile.width, Projectile.height, DustID.Electric, 0f, 0f, 120, default, 1.05f);
|
|
dust.noGravity = true;
|
|
}
|
|
}
|
|
|
|
public override void OnSpawn(IEntitySource source)
|
|
{
|
|
Projectile.GetGlobalProjectile<ProtogenSentinelGlobalProjectile>().DisableSentinelSplit = true;
|
|
}
|
|
|
|
private static NPC AcquireTarget(Vector2 center, float range)
|
|
{
|
|
NPC closest = null;
|
|
float squaredRange = range * range;
|
|
for (int i = 0; i < Main.maxNPCs; i++)
|
|
{
|
|
NPC npc = Main.npc[i];
|
|
if (!npc.active || npc.friendly || npc.dontTakeDamage || npc.life <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float distance = Vector2.DistanceSquared(npc.Center, center);
|
|
if (distance <= squaredRange && Collision.CanHitLine(center, 0, 0, npc.Center, 0, 0))
|
|
{
|
|
squaredRange = distance;
|
|
closest = npc;
|
|
}
|
|
}
|
|
|
|
return closest;
|
|
}
|
|
}
|
|
}
|