116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using System;
|
|
using CalamityMod.CalPlayer;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Terraria;
|
|
using Terraria.DataStructures;
|
|
using Terraria.ID;
|
|
using Terraria.ModLoader;
|
|
using Terraria.GameContent;
|
|
|
|
namespace CalamityMod.Items.Accessories
|
|
{
|
|
[AutoloadEquip(EquipType.Balloon)]
|
|
[LegacyName("SamuraiBadge")]
|
|
public class WarbanneroftheSun : ModItem, ILocalizedModType
|
|
{
|
|
public new string LocalizationCategory => "Items.Accessories";
|
|
internal const float MaxBonus = 0.2f;
|
|
internal const float MaxDistance = 480f;
|
|
|
|
public override void SetStaticDefaults()
|
|
{
|
|
Main.RegisterItemAnimation(Item.type, new DrawAnimationVertical(6, 5));
|
|
ItemID.Sets.AnimatesAsSoul[Type] = true;
|
|
}
|
|
|
|
public override void SetDefaults()
|
|
{
|
|
Item.width = 32;
|
|
Item.height = 78;
|
|
Item.value = CalamityGlobalItem.Rarity11BuyPrice;
|
|
Item.rare = ItemRarityID.Purple;
|
|
Item.accessory = true;
|
|
}
|
|
|
|
public override void UpdateAccessory(Player player, bool hideVisual)
|
|
{
|
|
CalamityPlayer modPlayer = player.Calamity();
|
|
modPlayer.warbannerOfTheSun = true;
|
|
|
|
float bonus = CalculateBonus(player);
|
|
player.GetAttackSpeed<MeleeDamageClass>() += bonus;
|
|
player.GetDamage<MeleeDamageClass>() += bonus;
|
|
player.GetDamage<TrueMeleeDamageClass>() += bonus;
|
|
}
|
|
|
|
private static float CalculateBonus(Player player)
|
|
{
|
|
float bonus = 0f;
|
|
|
|
int closestNPC = -1;
|
|
for (int i = 0; i < Main.maxNPCs; i++)
|
|
{
|
|
NPC nPC = Main.npc[i];
|
|
if (nPC.active && !nPC.friendly && (nPC.damage > 0 || nPC.boss) && !nPC.dontTakeDamage)
|
|
{
|
|
closestNPC = i;
|
|
break;
|
|
}
|
|
}
|
|
float distance = -1f;
|
|
for (int j = 0; j < Main.maxNPCs; j++)
|
|
{
|
|
NPC nPC = Main.npc[j];
|
|
if (nPC.active && !nPC.friendly && (nPC.damage > 0 || nPC.boss) && !nPC.dontTakeDamage)
|
|
{
|
|
float distance2 = Math.Abs(nPC.position.X + (float)(nPC.width / 2) - (player.position.X + (float)(player.width / 2))) + Math.Abs(nPC.position.Y + (float)(nPC.height / 2) - (player.position.Y + (float)(player.height / 2)));
|
|
if (distance == -1f || distance2 < distance)
|
|
{
|
|
distance = distance2;
|
|
closestNPC = j;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (closestNPC != -1)
|
|
{
|
|
NPC actualClosestNPC = Main.npc[closestNPC];
|
|
|
|
float generousHitboxWidth = Math.Max(actualClosestNPC.Hitbox.Width / 2f, actualClosestNPC.Hitbox.Height / 2f);
|
|
float hitboxEdgeDist = actualClosestNPC.Distance(player.Center) - generousHitboxWidth;
|
|
|
|
if (hitboxEdgeDist < 0)
|
|
hitboxEdgeDist = 0;
|
|
|
|
if (hitboxEdgeDist < MaxDistance)
|
|
{
|
|
bonus = MathHelper.Lerp(0f, MaxBonus, 1f - (hitboxEdgeDist / MaxDistance));
|
|
|
|
if (bonus > MaxBonus)
|
|
bonus = MaxBonus;
|
|
}
|
|
}
|
|
|
|
return bonus;
|
|
}
|
|
|
|
public override bool PreDrawInInventory(SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale)
|
|
{
|
|
CalamityUtils.DrawInventoryCustomScale(
|
|
spriteBatch,
|
|
texture: TextureAssets.Item[Type].Value,
|
|
position,
|
|
frame,
|
|
drawColor,
|
|
itemColor,
|
|
origin,
|
|
scale,
|
|
wantedScale: 0.6f,
|
|
drawOffset: new(0f, -2f)
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
}
|