Files
CalamityModPublic/Projectiles/Melee/StellarContemptHammer.cs
T

219 lines
8.8 KiB
C#

using CalamityMod.Buffs.DamageOverTime;
using CalamityMod.Items.Weapons.Melee;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.Audio;
namespace CalamityMod.Projectiles.Melee
{
public class StellarContemptHammer : ModProjectile
{
public override string Texture => "CalamityMod/Items/Weapons/Melee/StellarContempt";
private static float RotationIncrement = 0.22f;
private static int Lifetime = 240;
private static float ReboundTime = 26f;
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Stellar Contempt");
ProjectileID.Sets.TrailCacheLength[Projectile.type] = 8;
ProjectileID.Sets.TrailingMode[Projectile.type] = 1;
}
public override void SetDefaults()
{
Projectile.width = 44;
Projectile.height = 44;
Projectile.friendly = true;
Projectile.DamageType = DamageClass.MeleeNoSpeed;
Projectile.ignoreWater = true;
Projectile.tileCollide = false;
Projectile.penetrate = -1;
Projectile.extraUpdates = 3;
Projectile.timeLeft = Lifetime;
}
public override void AI()
{
DrawOffsetX = -11;
DrawOriginOffsetY = -10;
DrawOriginOffsetX = 0;
Lighting.AddLight(Projectile.Center, 0.7f, 0.3f, 0.6f);
// The hammer makes sound while flying.
if (Projectile.soundDelay == 0)
{
Projectile.soundDelay = 8;
SoundEngine.PlaySound(SoundID.Item7, Projectile.position);
}
// ai[0] stores whether the hammer 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 = StellarContempt.Speed;
float acceleration = 3.2f;
Player owner = Main.player[Projectile.owner];
// Delete the hammer 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(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 hammer as it flies.
Projectile.rotation += RotationIncrement;
}
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)
{
// Some dust gets produced on impact.
int dustCount = Main.rand.Next(20, 24);
int dustRadius = 6;
Vector2 corner = new Vector2(target.Center.X - dustRadius, target.Center.Y - dustRadius);
for (int i = 0; i < dustCount; ++i)
{
int dustType = 229;
float scale = 0.8f + Main.rand.NextFloat(1.1f);
int idx = Dust.NewDust(corner, 2 * dustRadius, 2 * dustRadius, dustType);
Main.dust[idx].noGravity = true;
Main.dust[idx].velocity *= 3f;
Main.dust[idx].scale = scale;
}
// Applies Nightwither on contact at night.
if (!Main.dayTime)
target.AddBuff(ModContent.BuffType<Nightwither>(), 240);
SpawnFlares(target.Center, target.width, target.height);
}
public override void OnHitPvp(Player target, int damage, bool crit)
{
// Some dust gets produced on impact.
int dustCount = Main.rand.Next(20, 24);
int dustRadius = 6;
Vector2 corner = new Vector2(target.Center.X - dustRadius, target.Center.Y - dustRadius);
for (int i = 0; i < dustCount; ++i)
{
int dustType = 229;
float scale = 0.8f + Main.rand.NextFloat(1.1f);
int idx = Dust.NewDust(corner, 2 * dustRadius, 2 * dustRadius, dustType);
Main.dust[idx].noGravity = true;
Main.dust[idx].velocity *= 3f;
Main.dust[idx].scale = scale;
}
// Applies Nightwither on contact at night.
if (!Main.dayTime)
target.AddBuff(ModContent.BuffType<Nightwither>(), 240);
SpawnFlares(target.Center, target.width, target.height);
}
private void SpawnFlares(Vector2 targetPos, int width, int height)
{
// Play the Lunar Flare sound centered on the user, not the target (consistent with Lunar Flare and Stellar Striker)
Player user = Main.player[Projectile.owner];
SoundEngine.PlaySound(SoundID.Item88, Projectile.position);
Projectile.netUpdate = true;
int numFlares = 2;
int flareDamage = (int)(0.3f * Projectile.damage);
float flareKB = 4f;
for (int i = 0; i < numFlares; ++i)
{
float flareSpeed = Main.rand.NextFloat(8f, 11f);
// Flares never come from straight up, there is always at least an 80 pixel horizontal offset
float xDist = Main.rand.NextFloat(80f, 320f) * (Main.rand.NextBool() ? -1f : 1f);
float yDist = Main.rand.NextFloat(1200f, 1440f);
Vector2 startPoint = targetPos + new Vector2(xDist, -yDist);
// The flare is somewhat inaccurate based on the size of the target.
float xVariance = width / 4f;
if (xVariance < 8f)
xVariance = 8f;
float yVariance = height / 4f;
if (yVariance < 8f)
yVariance = 8f;
float xOffset = Main.rand.NextFloat(-xVariance, xVariance);
float yOffset = Main.rand.NextFloat(-yVariance, yVariance);
Vector2 offsetTarget = targetPos + new Vector2(xOffset, yOffset);
// Finalize the velocity vector and make sure it's going at the right speed.
Vector2 velocity = offsetTarget - startPoint;
velocity.Normalize();
velocity *= flareSpeed;
float AI1 = Main.rand.Next(3);
if (Projectile.owner == Main.myPlayer)
{
int proj = Projectile.NewProjectile(Projectile.GetSource_FromThis(), startPoint, velocity, ProjectileID.LunarFlare, flareDamage, flareKB, Main.myPlayer, 0f, AI1);
if (proj.WithinBounds(Main.maxProjectiles))
Main.projectile[proj].DamageType = DamageClass.MeleeNoSpeed;
}
}
}
}
}