using System;
using Robust.Shared.Maths;
namespace Robust.Shared.Physics
{
///
/// A representation of a 2D ray.
///
[Serializable]
public readonly struct CollisionRay : IEquatable {
private readonly Ray _ray;
private readonly int _collisionMask;
///
/// Specifies the starting point of the ray.
///
public Vector2 Position => _ray.Position;
///
/// Specifies the direction the ray is pointing.
///
public Vector2 Direction => _ray.Direction;
public int CollisionMask => _collisionMask;
///
/// Creates a new instance of a Ray.
///
/// Starting position of the ray.
/// Unit direction vector that the ray is pointing.
public CollisionRay(Vector2 position, Vector2 direction, int collisionMask)
{
_ray = new Ray(position, direction);
_collisionMask = collisionMask;
}
#region Intersect Tests
public bool Intersects(Box2 box, out float distance, out Vector2 hitPos)
=> _ray.Intersects(box, out distance, out hitPos);
#endregion
#region Equality
///
/// Determines if this Ray and another Ray are equivalent.
///
/// Ray to compare to.
public bool Equals(CollisionRay other)
{
return Position.Equals(other.Position) && Direction.Equals(other.Direction);
}
///
/// Determines if this ray and another object is equivalent.
///
/// Object to compare to.
public override bool Equals(object? obj)
{
if (obj is null) return false;
return obj is CollisionRay ray && Equals(ray);
}
///
/// Calculates the hash code of this Ray.
///
public override int GetHashCode()
{
unchecked
{
return (Position.GetHashCode() * 397) ^ Direction.GetHashCode();
}
}
///
/// Determines if two instances of Ray are equal.
///
/// Ray on the left side of the operator.
/// Ray on the right side of the operator.
public static bool operator ==(CollisionRay a, CollisionRay b)
{
return a.Equals(b);
}
///
/// Determines if two instances of Ray are not equal.
///
/// Ray on the left side of the operator.
/// Ray on the right side of the operator.
public static bool operator !=(CollisionRay a, CollisionRay b)
{
return !(a == b);
}
#endregion
public static implicit operator Ray(CollisionRay a)
=> a._ray;
}
}