Files
CalamityModPublic/Projectiles/Summon/OldDukeSharkVomit.cs
T

129 lines
5.1 KiB
C#

using CalamityMod.Buffs.DamageOverTime;
using CalamityMod.Buffs.StatDebuffs;
using CalamityMod.Dusts;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Projectiles.Summon
{
public class OldDukeSharkVomit : ModProjectile, ILocalizedModType
{
public new string LocalizationCategory => "Projectiles.Summon";
public override void SetStaticDefaults()
{
ProjectileID.Sets.MinionShot[Projectile.type] = true;
}
public override void SetDefaults()
{
Projectile.friendly = true;
Projectile.width = 36;
Projectile.height = 36;
Projectile.minion = true;
Projectile.minionSlots = 0f;
Projectile.ignoreWater = true;
Projectile.tileCollide = true;
Projectile.timeLeft = 360;
Projectile.alpha = 255;
Projectile.DamageType = DamageClass.Summon;
}
public override void AI()
{
Projectile.ai[0] += 1f;
if (Projectile.ai[0] < 10f)
{
Projectile.alpha = 255 - (int)(255 * Projectile.ai[0] / 10f);
}
Projectile.velocity.Y += 0.2f;
if (Projectile.velocity.X < 0f)
{
Projectile.spriteDirection = -1;
Projectile.rotation = (float)Math.Atan2((double)-(double)Projectile.velocity.Y, (double)-(double)Projectile.velocity.X);
}
else
{
Projectile.spriteDirection = 1;
Projectile.rotation = (float)Math.Atan2((double)Projectile.velocity.Y, (double)Projectile.velocity.X);
}
//Homing
if (Projectile.ai[0] > 20f)
HomingAI();
}
private void HomingAI()
{
Player player = Main.player[Projectile.owner];
int targetIdx = -1;
float maxHomingRange = 600f;
bool hasHomingTarget = false;
if (player.HasMinionAttackTargetNPC)
{
NPC npc = Main.npc[player.MinionAttackTargetNPC];
if (npc.CanBeChasedBy(Projectile, false))
{
float dist = (Projectile.Center - npc.Center).Length();
if (dist < maxHomingRange && Collision.CanHit(Projectile.Center, Projectile.width, Projectile.height, npc.Center, npc.width, npc.height))
{
targetIdx = player.MinionAttackTargetNPC;
maxHomingRange = dist;
hasHomingTarget = true;
}
}
}
if (!hasHomingTarget)
{
for (int i = 0; i < Main.npc.Length; ++i)
{
NPC npc = Main.npc[i];
if (npc is null || !npc.active)
continue;
if (npc.CanBeChasedBy(Projectile, false))
{
float dist = (Projectile.Center - npc.Center).Length();
if (dist < maxHomingRange && Collision.CanHit(Projectile.Center, Projectile.width, Projectile.height, npc.Center, npc.width, npc.height))
{
targetIdx = i;
maxHomingRange = dist;
hasHomingTarget = true;
}
}
}
}
// Home in on said closest NPC.
if (hasHomingTarget)
{
NPC target = Main.npc[targetIdx];
Vector2 homingVector = (target.Center - Projectile.Center).SafeNormalize(Vector2.Zero) * 25f;
float homingRatio = 20f;
Projectile.velocity = (Projectile.velocity * homingRatio + homingVector) / (homingRatio + 1f);
}
}
public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone) => target.AddBuff(ModContent.BuffType<SulphuricPoisoning>(), 180);
public override bool PreDraw(ref Color lightColor)
{
SpriteEffects spriteEffects = Projectile.spriteDirection == -1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
Texture2D texture = ModContent.Request<Texture2D>(Texture).Value;
Main.EntitySpriteDraw(texture, Projectile.Center - Main.screenPosition + new Vector2(0f, Projectile.gfxOffY), new Microsoft.Xna.Framework.Rectangle?(new Rectangle(0, 0, texture.Width, texture.Height)), Projectile.GetAlpha(lightColor), Projectile.rotation, texture.Size() / 2f, Projectile.scale, spriteEffects, 0);
return false;
}
public override void Kill(int timeLeft)
{
for (int i = 0; i < Main.rand.Next(28, 41); i++)
{
Dust.NewDustPerfect(Projectile.Center + Utils.NextVector2Unit(Main.rand) * Main.rand.NextFloat(10f),
(int)CalamityDusts.SulfurousSeaAcid,
Utils.NextVector2Unit(Main.rand) * Main.rand.NextFloat(1f, 4f));
}
}
}
}