153 lines
5.4 KiB
C#
153 lines
5.4 KiB
C#
using CalamityMod.Items.Weapons.Rogue;
|
|
using Microsoft.Xna.Framework;
|
|
using System;
|
|
using Terraria;
|
|
using Terraria.ID;
|
|
using Terraria.ModLoader;
|
|
using Terraria.Audio;
|
|
|
|
namespace CalamityMod.Projectiles.Rogue
|
|
{
|
|
public class GlaiveProj : ModProjectile
|
|
{
|
|
public override string Texture => "CalamityMod/Items/Weapons/Rogue/Glaive";
|
|
|
|
private static int Lifetime = 180;
|
|
private static int ReboundTime = 45;
|
|
|
|
public override void SetStaticDefaults()
|
|
{
|
|
DisplayName.SetDefault("Glaive");
|
|
ProjectileID.Sets.TrailCacheLength[Projectile.type] = 3;
|
|
ProjectileID.Sets.TrailingMode[Projectile.type] = 0;
|
|
}
|
|
|
|
public override void SetDefaults()
|
|
{
|
|
Projectile.width = 16;
|
|
Projectile.height = 16;
|
|
Projectile.friendly = true;
|
|
Projectile.tileCollide = true;
|
|
Projectile.penetrate = 3;
|
|
Projectile.timeLeft = Lifetime;
|
|
DrawOffsetX = -10;
|
|
Projectile.DamageType = RogueDamageClass.Instance;
|
|
}
|
|
|
|
public override void AI()
|
|
{
|
|
// ai[1] = 1 means that the projectile is a stealth strike, in which case it pierces infinitely.
|
|
if (Projectile.ai[1] == 1f)
|
|
Projectile.penetrate = Projectile.maxPenetrate = -1;
|
|
|
|
// Boomerang rotation
|
|
Projectile.rotation += 0.4f * (float)Projectile.direction;
|
|
|
|
// Boomerang sound
|
|
if (Projectile.soundDelay == 0)
|
|
{
|
|
Projectile.soundDelay = 8;
|
|
SoundEngine.PlaySound(SoundID.Item7, Projectile.position);
|
|
}
|
|
|
|
// Returns after some number of frames in the air
|
|
if (Projectile.timeLeft < Lifetime - ReboundTime)
|
|
Projectile.ai[0] = 1f;
|
|
|
|
if (Projectile.ai[0] != 0f)
|
|
{
|
|
Projectile.tileCollide = false;
|
|
|
|
float returnSpeed = Glaive.Speed * 1.6f;
|
|
float acceleration = 1.4f;
|
|
|
|
if (Projectile.ai[1] == 1f)
|
|
{
|
|
returnSpeed *= Glaive.StealthSpeedMult;
|
|
acceleration *= Glaive.StealthSpeedMult;
|
|
}
|
|
|
|
Player owner = Main.player[Projectile.owner];
|
|
|
|
// Delete the projectile if it's excessively far away.
|
|
Vector2 playerCenter = owner.Center;
|
|
float xDist = playerCenter.X - Projectile.Center.X;
|
|
float yDist = playerCenter.Y - Projectile.Center.Y;
|
|
float dist = (float)Math.Sqrt((double)(xDist * xDist + yDist * yDist));
|
|
if (dist > 3000f)
|
|
Projectile.Kill();
|
|
|
|
dist = returnSpeed / dist;
|
|
xDist *= dist;
|
|
yDist *= dist;
|
|
|
|
// Home back in on the player.
|
|
if (Projectile.velocity.X < xDist)
|
|
{
|
|
Projectile.velocity.X += acceleration;
|
|
if (Projectile.velocity.X < 0f && xDist > 0f)
|
|
Projectile.velocity.X += acceleration;
|
|
}
|
|
else if (Projectile.velocity.X > xDist)
|
|
{
|
|
Projectile.velocity.X -= acceleration;
|
|
if (Projectile.velocity.X > 0f && xDist < 0f)
|
|
Projectile.velocity.X -= acceleration;
|
|
}
|
|
if (Projectile.velocity.Y < yDist)
|
|
{
|
|
Projectile.velocity.Y += acceleration;
|
|
if (Projectile.velocity.Y < 0f && yDist > 0f)
|
|
Projectile.velocity.Y += acceleration;
|
|
}
|
|
else if (Projectile.velocity.Y > yDist)
|
|
{
|
|
Projectile.velocity.Y -= acceleration;
|
|
if (Projectile.velocity.Y > 0f && yDist < 0f)
|
|
Projectile.velocity.Y -= acceleration;
|
|
}
|
|
|
|
// Delete the projectile if it touches its owner.
|
|
if (Main.myPlayer == Projectile.owner)
|
|
if (Projectile.Hitbox.Intersects(owner.Hitbox))
|
|
Projectile.Kill();
|
|
}
|
|
}
|
|
|
|
public override bool PreDraw(ref Color lightColor)
|
|
{
|
|
CalamityUtils.DrawAfterimagesCentered(Projectile, ProjectileID.Sets.TrailingMode[Projectile.type], lightColor, 1);
|
|
return false;
|
|
}
|
|
|
|
public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
|
|
{
|
|
// After its last hit, starts returning instead of vanishing. Can pierce infinitely on the way back.
|
|
if (Projectile.penetrate == 1)
|
|
{
|
|
Projectile.penetrate = -1;
|
|
Projectile.ai[0] = 1f;
|
|
}
|
|
}
|
|
|
|
// Make it bounce on tiles.
|
|
public override bool OnTileCollide(Vector2 oldVelocity)
|
|
{
|
|
// Impacts the terrain even though it bounces off.
|
|
SoundEngine.PlaySound(SoundID.Dig, Projectile.Center);
|
|
Collision.HitTiles(Projectile.position, Projectile.velocity, Projectile.width, Projectile.height);
|
|
|
|
if (Projectile.velocity.X != oldVelocity.X)
|
|
{
|
|
Projectile.velocity.X = -oldVelocity.X;
|
|
}
|
|
if (Projectile.velocity.Y != oldVelocity.Y)
|
|
{
|
|
Projectile.velocity.Y = -oldVelocity.Y;
|
|
}
|
|
Projectile.ai[0] = 1f;
|
|
return false;
|
|
}
|
|
}
|
|
}
|