using CalamityMod.CalPlayer;
using System;
using Microsoft.Xna.Framework;
using Terraria;
namespace CalamityMod
{
public static partial class CalamityUtils
{
///
/// Gets a unit direction towards an arbitrary destination for an entity based on its center. Has safety in the form of a fallback vector.
///
/// The entity to check from.
/// The destination to get the direction to.
/// A fallback value to use in the event of an unsafe normalization.
public static Vector2 SafeDirectionTo(this Entity entity, Vector2 destination, Vector2? fallback = null)
{
// Fall back to zero by default. default(Vector2) could be used in the parameter definition, but
// this is more clear.
if (!fallback.HasValue)
fallback = Vector2.Zero;
return (destination - entity.Center).SafeNormalize(fallback.Value);
}
///
/// Sets a player's screenshake value. Automatically checks to ensure it will not override a stronger screenshake.
///
/// The player to add screenshake to.
/// The intensity of the screenshake.
public static void SetScreenshake(this Player player, float value)
{
if (player.Calamity().GeneralScreenShakePower < value)
player.Calamity().GeneralScreenShakePower = value;
}
///
/// Adds screenshake to the local player, using the given position and range to determine whether the player is able to see the screenshake.
///
/// The center of the screenshake, where it is most intense.
/// The maximum intensity of the screenshake.
/// The distance from which the screenshake's power becomes zero.
public static void AddScreenshakeAt(Vector2 position, float intensity, float range = 1000)
{
float dist = 1;
dist -= position.Distance(Main.LocalPlayer.Center) / range;
dist = Math.Max(dist, 0);
Main.LocalPlayer.GetModPlayer().GeneralScreenShakePower += (intensity * dist);
}
///
/// Check if Entity is null or Inactive (!active)
///
/// Entity to check
/// true if entity is null or inactive, otherwise false
public static bool IsNullOrInactive(this Entity entity)
{
if (entity is null) return true;
if (!entity.active) return true;
return false;
}
#region Fallback Method for IndexInRange
///
/// Fallback method for Main.npc.IndexInRange which provide accurate range check (0 <= index < Main.maxNPCs)
///
/// whoAmI index to check
/// true if index is in valid range [0 <= index < Main.maxNPCs]
public static bool IndexInRange(this NPC[] _, int index)
{
if (index >= 0)
return index < Main.maxNPCs;
return false;
}
///
/// Fallback method for Main.player.IndexInRange which provide accurate range check (0 <= index < Main.maxPlayers)
///
/// whoAmI index to check
/// true if index is in valid range [0 <= index < Main.maxPlayers]
public static bool IndexInRange(this Player[] _, int index)
{
if (index >= 0)
return index < Main.maxPlayers;
return false;
}
///
/// Fallback method for Main.projectile.IndexInRange which provide accurate range check (0 <= index < Main.maxProjectiles)
///
/// whoAmI index to check
/// true if index is in valid range [0 <= index < Main.maxProjectiles]
public static bool IndexInRange(this Projectile[] _, int index)
{
if (index >= 0)
return index < Main.maxProjectiles;
return false;
}
///
/// Fallback method for Main.gore.IndexInRange which provide accurate range check (0 <= index < Main.maxGore)
///
/// whoAmI index to check
/// true if index is in valid range [0 <= index < Main.maxGore]
public static bool IndexInRange(this Gore[] _, int index)
{
if (index >= 0)
return index < Main.maxGore;
return false;
}
///
/// Fallback method for Main.npc.IndexInRange which provide accurate range check (0 <= index < Main.maxDust)
///
/// whoAmI index to check
/// true if index is in valid range [0 <= index < Main.maxDust]
public static bool IndexInRange(this Dust[] _, int index)
{
if (index >= 0)
return index < Main.maxDust;
return false;
}
#endregion
}
}