using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using Terraria; using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Projectiles.Rogue { public class ButcherKnife : ModProjectile { private static float RotationIncrement = 0.22f; private static float ReboundTime = 26f; public override void SetStaticDefaults() { DisplayName.SetDefault("Butcher Knife"); ProjectileID.Sets.TrailCacheLength[projectile.type] = 6; ProjectileID.Sets.TrailingMode[projectile.type] = 0; } public override void SetDefaults() { projectile.width = 14; projectile.height = 14; projectile.friendly = true; projectile.penetrate = -1; projectile.timeLeft = 300; projectile.Calamity().rogue = true; projectile.extraUpdates = 1; projectile.usesLocalNPCImmunity = true; projectile.localNPCHitCooldown = 60; projectile.tileCollide = true; } public override void AI() { if (Main.rand.Next(2) == 0) { Dust.NewDust(projectile.position + projectile.velocity, projectile.width, projectile.height, 5, projectile.velocity.X * 0.5f, projectile.velocity.Y * 0.5f); } drawOffsetX = -11; drawOriginOffsetY = -10; drawOriginOffsetX = 0; // ai[0] stores whether the knife is returning. If 0, it isn't. If 1, it is. if (projectile.ai[0] == 0f) { projectile.ai[1] += 1f; if (projectile.ai[1] >= ReboundTime) { projectile.ai[0] = 1f; projectile.ai[1] = 0f; projectile.netUpdate = true; } } else { projectile.tileCollide = false; float returnSpeed = 16f; float acceleration = 3.2f; Player owner = Main.player[projectile.owner]; // Delete the knife 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 = projectile.velocity.X + acceleration; if (projectile.velocity.X < 0f && xDist > 0f) projectile.velocity.X += acceleration; } else if (projectile.velocity.X > xDist) { projectile.velocity.X = projectile.velocity.X - acceleration; if (projectile.velocity.X > 0f && xDist < 0f) projectile.velocity.X -= acceleration; } if (projectile.velocity.Y < yDist) { projectile.velocity.Y = projectile.velocity.Y + acceleration; if (projectile.velocity.Y < 0f && yDist > 0f) projectile.velocity.Y += acceleration; } else if (projectile.velocity.Y > yDist) { projectile.velocity.Y = 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(); } // Rotate the knife as it flies. projectile.rotation += RotationIncrement; return; } public override bool OnTileCollide(Vector2 oldVelocity) { projectile.ai[0] += 0.1f; if (projectile.velocity.X != oldVelocity.X) { projectile.velocity.X = -oldVelocity.X; } if (projectile.velocity.Y != oldVelocity.Y) { projectile.velocity.Y = -oldVelocity.Y; } return false; } public override void OnHitNPC(NPC target, int damage, float knockback, bool crit) { Player player = Main.player[projectile.owner]; player.AddBuff(BuffID.WellFed, 180); } public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { CalamityGlobalProjectile.DrawCenteredAndAfterimage(projectile, lightColor, ProjectileID.Sets.TrailingMode[projectile.type], 1); return false; } } }