Files
CalamityModPublic/Projectiles/Melee/BrokenBiomeBlade_DecaysRetort.cs
T

162 lines
7.0 KiB
C#

using CalamityMod.Items.Weapons.Melee;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using static Terraria.ModLoader.ModContent;
using Terraria.Audio;
namespace CalamityMod.Projectiles.Melee
{
public class DecaysRetort : ModProjectile, ILocalizedModType
{
public new string LocalizationCategory => "Projectiles.Melee";
public override string Texture => "CalamityMod/Projectiles/Melee/BrokenBiomeBlade_DecaysRetort";
private bool initialized = false;
Vector2 direction = Vector2.Zero;
public ref float MaxTime => ref Projectile.ai[0];
public ref float CanLunge => ref Projectile.ai[1];
public float Timer => MaxTime - Projectile.timeLeft;
public Player Owner => Main.player[Projectile.owner];
public const float LungeSpeed = 16;
public ref float CanBounce => ref Projectile.localAI[0];
public override void SetStaticDefaults()
{
}
public override void SetDefaults()
{
Projectile.DamageType = DamageClass.Melee;
Projectile.width = Projectile.height = 84;
Projectile.width = Projectile.height = 84;
Projectile.tileCollide = false;
Projectile.friendly = true;
Projectile.penetrate = -1;
Projectile.extraUpdates = 1;
}
public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox)
{
float collisionPoint = 0f;
float bladeLength = 120f * Projectile.scale;
Vector2 displace = direction * ((float)Math.Sin(Timer / MaxTime * 3.14f) * 60);
return Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), Owner.Center + displace, Owner.Center + displace + (direction * bladeLength), 24, ref collisionPoint);
}
public override void AI()
{
if (!initialized) //Initialization
{
CanBounce = 1f;
Projectile.timeLeft = (int)MaxTime;
direction = Owner.SafeDirectionTo(Owner.Calamity().mouseWorld, Vector2.Zero);
direction.Normalize();
Projectile.rotation = direction.ToRotation();
if (CanLunge == 1f)
Lunge();
SoundEngine.PlaySound(SoundID.Item103, Projectile.Center);
initialized = true;
Projectile.netUpdate = true;
Projectile.netSpam = 0;
}
//Manage position and rotation
Projectile.scale = 1f + ((float)Math.Sin(Timer / MaxTime * MathHelper.Pi) * 0.6f); //SWAGGER
Projectile.Center = Owner.Center + (direction * ((float)Math.Sin(Timer / MaxTime * MathHelper.Pi) * 60));
Lighting.AddLight(Projectile.Center, new Vector3(0.9f, 0f, 0.35f) * (float)Math.Sin(Timer / MaxTime * MathHelper.Pi));
//Make the owner look like theyre holding the sword bla bla
Owner.heldProj = Projectile.whoAmI;
Owner.direction = Math.Sign(direction.X);
Owner.itemRotation = direction.ToRotation();
if (Owner.direction != 1)
{
Owner.itemRotation -= MathHelper.Pi;
}
Owner.itemRotation = MathHelper.WrapAngle(Owner.itemRotation);
}
public void Lunge()
{
if (Main.myPlayer != Projectile.owner)
return;
Owner.velocity = direction.SafeNormalize(Vector2.UnitX * Owner.direction) * LungeSpeed;
}
public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone) => OnHitEffects(!target.canGhostHeal || Main.player[Projectile.owner].moonLeech);
public override void OnHitPlayer(Player target, Player.HurtInfo info) => OnHitEffects(Main.player[Projectile.owner].moonLeech);
private void OnHitEffects(bool cannotLifesteal)
{
Projectile.netUpdate = true;
Projectile.netSpam = 0;
if (Main.myPlayer != Owner.whoAmI || CanBounce == 0f)
return;
if (!cannotLifesteal) //trolled
{
Owner.statLife += BrokenBiomeBlade.EvilAttunement_Lifesteal;
Owner.HealEffect(BrokenBiomeBlade.EvilAttunement_Lifesteal); //Idk if its too much or what but at the same time its close range as fuck
}
// Bounce off
float bounceStrength = Math.Max((LungeSpeed / 2f), Owner.velocity.Length());
bounceStrength *= Owner.velocity.Y == 0 ? 0.2f : 1f; //Reduce the bounce if the player is on the ground
Owner.velocity = -direction.SafeNormalize(Vector2.Zero) * MathHelper.Clamp(bounceStrength, 0f, 22f);
CanBounce = 0f;
Owner.GiveIFrames(BrokenBiomeBlade.EvilAttunement_BounceIFrames); // i frames for free!
}
public override bool PreDraw(ref Color lightColor)
{
Texture2D handle = Request<Texture2D>("CalamityMod/Items/Weapons/Melee/BrokenBiomeBlade").Value;
Texture2D tex = Request<Texture2D>("CalamityMod/Projectiles/Melee/BrokenBiomeBlade_DecaysRetort").Value;
float drawAngle = direction.ToRotation();
float drawRotation = drawAngle + MathHelper.PiOver4;
Vector2 displace = direction * ((float)Math.Sin(Timer / MaxTime * 3.14f) * 60);
Vector2 drawOrigin = new Vector2(0f, handle.Height);
Vector2 drawOffset = Owner.Center + direction * 10f - Main.screenPosition;
Main.EntitySpriteDraw(handle, drawOffset + displace, null, lightColor, drawRotation, drawOrigin, Projectile.scale, 0f, 0);
//Turn on additive blending
Main.spriteBatch.End();
Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
//Update the parameters
drawOrigin = new Vector2(0f, tex.Height);
drawOffset = Owner.Center + (drawAngle.ToRotationVector2() * 24f * Projectile.scale) - Main.screenPosition;
Main.EntitySpriteDraw(tex, drawOffset + displace, null, Color.Lerp(Color.White, lightColor, 0.5f) * 0.9f, drawRotation, drawOrigin, Projectile.scale, 0f, 0);
//Back to normal
Main.spriteBatch.End();
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
return false;
}
public override void SendExtraAI(BinaryWriter writer)
{
writer.Write(initialized);
writer.WriteVector2(direction);
writer.Write(CanBounce);
}
public override void ReceiveExtraAI(BinaryReader reader)
{
initialized = reader.ReadBoolean();
direction = reader.ReadVector2();
CanBounce = reader.ReadSingle();
}
}
}