Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- namespace Lysander
- {
- [Serializable]
- public struct Vector3i : IEquatable<Vector3i>
- {
- // Properties
- public int x { get; set; }
- public int y { get; set; }
- public int z { get; set; }
- public Vector3i normalized
- {
- get
- {
- Vector3i newVector = new Vector3i(this.x, this.y, this.z);
- newVector.Normalize();
- return newVector;
- }
- }
- public float magnitude { get { return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } }
- public float sqrMagnitude { get { return this.x * this.x + this.y * this.y + this.z * this.z; } }
- // Indexer
- public int this[int index]
- {
- get
- {
- switch (index)
- {
- case 0:
- return this.x;
- case 1:
- return this.y;
- case 2:
- return this.z;
- default:
- throw new IndexOutOfRangeException("Invalid Vector3i index!");
- }
- }
- set
- {
- switch (index)
- {
- case 0:
- this.x = value;
- break;
- case 1:
- this.y = value;
- break;
- case 2:
- this.z = value;
- break;
- default:
- throw new IndexOutOfRangeException("Invalid Vector3i index!");
- }
- }
- }
- // Constructors
- public Vector3i(int x, int y, int z) : this()
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector3i(int x, int y) : this()
- {
- this.x = x;
- this.y = y;
- this.z = 0;
- }
- // Static Conversions From Other Types
- public static Vector3i fromVector2(Vector2 vector2)
- {
- Vector3i newVector = new Vector3i();
- newVector.x = Mathf.RoundToInt(vector2.x);
- newVector.y = Mathf.RoundToInt(vector2.y);
- return newVector;
- }
- public static Vector3i fromVector3(Vector3 vector3)
- {
- Vector3i newVector = new Vector3i();
- newVector.x = Mathf.RoundToInt(vector3.x);
- newVector.y = Mathf.RoundToInt(vector3.y);
- newVector.z = Mathf.RoundToInt(vector3.z);
- return newVector;
- }
- public static Vector3i fromVector2i(Vector2i vector2i)
- {
- Vector3i newVector = new Vector3i();
- newVector.x = vector2i.x;
- newVector.y = vector2i.y;
- return newVector;
- }
- // Conversions To Other Types
- public Vector2 toVector2() { return new Vector2(this.x, this.y); }
- public Vector3 toVector3() { return new Vector3(this.x, this.y, this.z); }
- public Vector2i toVector2i() { return new Vector2i(this.x, this.y); }
- // Static Vector3i Values
- public readonly static Vector3i zero = new Vector3i(0, 0, 0);
- public readonly static Vector3i forward = new Vector3i(0, 0, 1);
- public readonly static Vector3i back = new Vector3i(0, 0, -1);
- public readonly static Vector3i up = new Vector3i(0, 1, 0);
- public readonly static Vector3i down = new Vector3i(0, -1, 0);
- public readonly static Vector3i right = new Vector3i(1, 0, 0);
- public readonly static Vector3i left = new Vector3i(-1, 0, 0);
- public readonly static Vector3i one = new Vector3i(1, 1, 1);
- // Other Static Functions
- public static Vector3i Scale(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.x * b.x, a.y * b.y, a.z * b.z);
- }
- public static Vector3i Cross(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
- }
- public static Vector3i Reflect(Vector3i inDirection, Vector3i inNormal)
- {
- return -2f * Vector3i.Dot(inNormal, inDirection) * inNormal + inDirection;
- }
- public static float Dot(Vector3i a, Vector3i b)
- {
- return a.x * b.x + a.y * b.y + a.z * b.z;
- }
- public static float Angle(Vector3i from, Vector3i to)
- {
- return Mathf.Acos(Mathf.Clamp(Vector3i.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
- }
- public static float Distance(Vector3i a, Vector3i b)
- {
- Vector3i vector = new Vector3i(a.x - b.x, a.y - b.y, a.z - b.z);
- return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
- }
- public static Vector3i ClampMagnitude(Vector3i vector, float maxLength)
- {
- if (vector.sqrMagnitude > maxLength * maxLength)
- {
- return vector.normalized * maxLength;
- }
- return vector;
- }
- public static Vector3i Min(Vector3i a, Vector3i b)
- {
- return new Vector3i(Mathf.Min(a.x, b.x), Mathf.Min(a.y, b.y), Mathf.Min(a.z, b.z));
- }
- public static Vector3i Max(Vector3i a, Vector3i b)
- {
- return new Vector3i(Mathf.Max(a.x, b.x), Mathf.Max(a.y, b.y), Mathf.Max(a.z, b.z));
- }
- // Other Functions
- public void Set(int x, int y, int z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public void Scale(Vector3i scale)
- {
- this.x *= scale.x;
- this.y *= scale.y;
- this.z *= scale.z;
- }
- public void Normalize()
- {
- float num = magnitude;
- if (num > 1E-05f)
- this /= num;
- else
- this = Vector3i.zero;
- }
- public void ShiftAll(int amount)
- {
- this.x += amount;
- this.y += amount;
- this.z += amount;
- }
- public void ShiftAll(float amount)
- {
- ShiftAll(Mathf.RoundToInt(amount));
- }
- // Vector2i & Vector2i Operator Overloads
- public static Vector3i operator +(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.x + b.x, a.y + b.y, a.z + b.z);
- }
- public static Vector3i operator -(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.x - b.x, a.y - b.y, a.z - b.z);
- }
- public static Vector3i operator -(Vector3i a)
- {
- return new Vector3i(-a.x, -a.y, -a.z);
- }
- public static Vector3i operator *(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.x * b.x, a.y * b.y, a.z * b.z);
- }
- public static Vector3i operator /(Vector3i a, Vector3i b)
- {
- return new Vector3i(a.x / b.x, a.y / b.y, a.z / b.z);
- }
- // Vector2i & Int Operator Overloads
- public static Vector3i operator *(Vector3i a, int d)
- {
- return new Vector3i(a.x * d, a.y * d, a.z * d);
- }
- public static Vector3i operator *(Vector3i a, float d)
- {
- return new Vector3i(Mathf.RoundToInt(a.x * d), Mathf.RoundToInt(a.y * d), Mathf.RoundToInt(a.z * d));
- }
- public static Vector3i operator *(int d, Vector3i a)
- {
- return new Vector3i(a.x * d, a.y * d, a.z * d);
- }
- public static Vector3i operator *(float d, Vector3i a)
- {
- return new Vector3i(Mathf.RoundToInt(a.x * d), Mathf.RoundToInt(a.y * d), Mathf.RoundToInt(a.z * d));
- }
- public static Vector3i operator /(Vector3i a, int d)
- {
- return new Vector3i(a.x / d, a.y / d, a.z / d);
- }
- public static Vector3i operator /(Vector3i a, float d)
- {
- return new Vector3i(Mathf.RoundToInt((float)a.x / d), Mathf.RoundToInt((float)a.y / d), Mathf.RoundToInt((float)a.z / d));
- }
- // Equality and Inequality Operator Overloads
- public static bool operator ==(Vector3i a, Vector3i b)
- {
- return a.x.Equals(b.x) && a.y.Equals(b.y) && a.z.Equals(b.z);
- }
- public static bool operator !=(Vector3i a, Vector3i b)
- {
- return !a.x.Equals(b.x) || !a.y.Equals(b.y) || !a.z.Equals(b.z);
- }
- // Overrides & Implementations
- public override bool Equals(object obj)
- {
- if (obj is Vector3i)
- {
- Vector3i vector = (Vector3i)obj;
- return this.x.Equals(vector.x) && this.y.Equals(vector.y) && this.z.Equals(vector.z);
- }
- return false;
- }
- public bool Equals(Vector3i vector)
- {
- return this.x.Equals(vector.x) && this.y.Equals(vector.y) && this.z.Equals(vector.z);
- }
- public override int GetHashCode()
- {
- return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
- }
- public override string ToString()
- {
- return "(" + this.x.ToString() + ", " + this.y.ToString() + ", " + this.z.ToString() + ")";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement