Files
FirstStrike.Unity/Assets/Editor/SourceCommon/Vectors/SourceQAngle.cs
T
2025-05-13 03:08:33 +03:00

89 lines
2.2 KiB
C#

using System;
namespace SourceFormatParser.Common
{
/// <summary>
/// Vector x/y/z (float)
/// Size: 12 bytes (4 bytes per axis)
/// </summary>
public struct SourceQAngle
{
// Members
public float x, y, z;
// Initialization
public SourceQAngle(float ix = 0.0f, float iy = 0.0f, float iz = 0.0f)
{
this.x = ix;
this.y = iy;
this.z = iz;
}
public static SourceQAngle Random(float minVal, float maxVal)
{
Random rand = new Random();
return new SourceQAngle(
ix: minVal + ((float)rand.NextDouble()) * (float)(maxVal - minVal),
iy: minVal + ((float)rand.NextDouble()) * (float)(maxVal - minVal),
iz: minVal + ((float)rand.NextDouble()) * (float)(maxVal - minVal)
);
}
// equality
public static bool operator ==(SourceQAngle v1, SourceQAngle v2) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
public static bool operator !=(SourceQAngle v1, SourceQAngle v2) => !(v1 == v2);
public override bool Equals(object obj) => this == (SourceQAngle)obj;
public override int GetHashCode() => base.GetHashCode();
// arithmetic operations
public static SourceQAngle operator +(SourceQAngle v1, SourceQAngle v2) => new SourceQAngle(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
public static SourceQAngle operator -(SourceQAngle v1, SourceQAngle v2) => new SourceQAngle(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
public static SourceQAngle operator *(SourceQAngle v, float s) => new SourceQAngle(v.x * s, v.y * s, v.z * s);
public static SourceQAngle operator /(SourceQAngle v, float fl)
{
if (fl == 0.0f)
return new SourceQAngle();
float oofl = 1.0f / fl;
return new SourceQAngle(v.x * oofl, v.y * oofl, v.z * oofl);
}
public float this[int i]
{
get
{
switch (i)
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
}
throw new Exception("wrong index");
}
set
{
switch (i)
{
case 0:
x = value;
break;
case 1:
y = value;
break;
case 2:
z = value;
break;
}
}
}
// Get the vector's magnitude.
public float Length() => (float)Math.Sqrt(LengthSqr());
public float LengthSqr() => x * x + y * y + z * z;
public override string ToString() => $"{{{x}; {y}; {z}}}";
}
}