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);
}
///
/// 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);
}
}
}