Files
CalamityModPublic/Projectiles/Rogue/GleamingDaggerProj.cs
T
2022-04-03 18:09:06 +02:00

145 lines
5.1 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.Audio;
namespace CalamityMod.Projectiles.Rogue
{
public class GleamingDaggerProj : ModProjectile
{
private bool hasHitEnemy = false;
private int targetNPC = -1;
private List<int> previousNPCs = new List<int>() { -1 };
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Gleaming Dagger");
}
public override void SetDefaults()
{
Projectile.width = 12;
Projectile.height = 12;
Projectile.friendly = true;
Projectile.penetrate = 2;
Projectile.timeLeft = 600;
Projectile.Calamity().rogue = true;
}
public override void AI()
{
if (Main.rand.NextBool(7))
{
Dust.NewDust(Projectile.position + Projectile.velocity, Projectile.width, Projectile.height, DustID.PlatinumCoin, Projectile.velocity.X * 0.5f, Projectile.velocity.Y * 0.5f);
}
if (hasHitEnemy)
{
Projectile.rotation += Projectile.direction * 0.4f;
}
else
{
Projectile.spriteDirection = Projectile.direction = (Projectile.velocity.X > 0).ToDirectionInt();
Projectile.rotation = Projectile.velocity.ToRotation() + (Projectile.spriteDirection == 1 ? 0f : MathHelper.PiOver2);
//Rotating 90 degrees if shooting right
if (Projectile.spriteDirection == 1)
{
Projectile.rotation += MathHelper.ToRadians(90f);
}
}
if (!Projectile.Calamity().stealthStrike && Projectile.timeLeft < 575)
{
Projectile.velocity.Y += 0.5f;
if (Projectile.velocity.Y > 16f)
{
Projectile.velocity.Y = 16f;
}
}
else if (Projectile.Calamity().stealthStrike && hasHitEnemy)
{
if (targetNPC >= 0 && Main.npc[targetNPC].active)
{
Vector2 newVelocity = Main.npc[targetNPC].Center - Projectile.Center;
newVelocity.Normalize();
newVelocity *= 15f;
Projectile.velocity = newVelocity;
}
}
}
public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
{
float minDist = 999f;
int index = 0;
for (int i = 0; i < Main.npc.Length; i++)
{
bool hasHitNPC = false;
for (int j = 0; j < previousNPCs.Count; j++)
{
if (previousNPCs[j] == i)
{
hasHitNPC = true;
}
}
NPC npc = Main.npc[i];
if (npc == target)
{
previousNPCs.Add(i);
}
if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC)
{
float dist = (Projectile.Center - npc.Center).Length();
if (dist < minDist)
{
minDist = dist;
index = i;
}
}
}
Vector2 velocityNew;
if (minDist < 999f)
{
if (Projectile.Calamity().stealthStrike)
{
Projectile.damage = (int)(Projectile.damage * 1.05f);
}
hasHitEnemy = true;
targetNPC = index;
velocityNew = Main.npc[index].Center - Projectile.Center;
velocityNew.Normalize();
velocityNew *= 15f;
Projectile.velocity = velocityNew;
}
}
public override bool OnTileCollide(Vector2 oldVelocity)
{
Collision.HitTiles(Projectile.position + Projectile.velocity, Projectile.velocity, Projectile.width, Projectile.height);
SoundEngine.PlaySound(SoundID.Dig, Projectile.position);
Projectile.Kill();
return false;
}
public override bool PreDraw(ref Color lightColor)
{
Texture2D tex = ModContent.Request<Texture2D>(Texture).Value;
Main.EntitySpriteDraw(tex, Projectile.Center - Main.screenPosition, null, Projectile.GetAlpha(lightColor), Projectile.rotation, tex.Size() / 2f, Projectile.scale, SpriteEffects.None, 0);
return false;
}
public override void Kill(int timeLeft)
{
for (int num621 = 0; num621 < 8; num621++)
{
int num622 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, DustID.PlatinumCoin, 0f, 0f, 100, default, 1f);
Main.dust[num622].velocity *= 1f;
}
}
}
}