PatchQuest

Untitled

Feb 9th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. public struct IntVector2 {
  6.  
  7.     public static IntVector2 zero { get { return new IntVector2(0, 0); } }
  8.     public static IntVector2 one { get { return new IntVector2(1, 1); } }
  9.  
  10.     // -----------------------------------
  11.  
  12.     private readonly int _x;
  13.     private readonly int _y;
  14.  
  15.     public IntVector2(int x, int y) {
  16.         this._x = x;
  17.         this._y = y;
  18.     }
  19.  
  20.     public int x {
  21.         get { return _x; }
  22.     }
  23.  
  24.     public int y {
  25.         get { return _y; }
  26.     }
  27.  
  28.     public Vector2 ToVector2 () {
  29.         return new Vector2 (_x, _y);
  30.     }
  31.  
  32.     public Vector3 PadLast() {
  33.         return new Vector3(_x, _y, 0);
  34.     }
  35.  
  36.     public Vector3 PadMiddle() {
  37.         return new Vector3(_x, 0, _y);
  38.     }
  39.  
  40.     public IntVector2 Reverse() {
  41.         return new IntVector2(-_x, -_y);
  42.     }
  43.  
  44.     public float Average () {
  45.         return (_x + _y) / 2f;
  46.     }
  47.  
  48.     public int Sum () {
  49.         return _x + _y;
  50.     }
  51.  
  52.     public float Hypoteneuse () {
  53.         return Mathf.Sqrt(_x * _x + _y * _y);
  54.     }
  55.  
  56.     public bool IsDiagonal() {
  57.         return x != 0 && y != 0 && Math.Abs(x) == Math.Abs(y);
  58.     }
  59.  
  60.     public bool IsOrthogonal () {
  61.         return x == 0 || y == 0;
  62.     }
  63.  
  64.     public int Magnitude {
  65.         get { return Math.Abs(x) + Math.Abs(y); }
  66.     }
  67.  
  68.     public int LargestDimension {
  69.         get { return Mathf.Max (Math.Abs(x), Math.Abs(y)); }
  70.     }
  71.  
  72.     public int ManhattanDistanceTo (IntVector2 other) {
  73.         return Math.Abs(_x - other._x) + Math.Abs(_y - other._y);
  74.     }
  75.  
  76.     public int DiagonalDistanceTo (IntVector2 other) {
  77.         return Mathf.Max(Mathf.Abs (other.x - x), Mathf.Abs (other.y - y));
  78.     }
  79.  
  80.     public bool IsZero {
  81.         get { return x == 0 && y == 0; }
  82.     }
  83.  
  84.     public static bool operator ==(IntVector2 a, IntVector2 b) {
  85.         return a._x == b._x && a._y == b._y;
  86.     }
  87.  
  88.     public static bool operator !=(IntVector2 a, IntVector2 b) {
  89.         return !(a == b);
  90.     }
  91.  
  92.     public override bool Equals(object obj) {
  93.         if (!(obj is IntVector2)) {
  94.             return false;
  95.         }
  96.         IntVector2 other = (IntVector2)obj;
  97.         return _x == other._x && _y == other._y;
  98.     }
  99.  
  100.     public override int GetHashCode() {
  101.         return _x + _y;
  102.     }
  103.  
  104.     public override string ToString() {
  105.         return "(" + _x + "," + _y + ")";
  106.     }
  107.  
  108.     public static IntVector2 operator +(IntVector2 c1, IntVector2 c2) {
  109.         return new IntVector2(c1._x + c2._x, c1._y + c2._y);
  110.     }
  111.  
  112.     public static IntVector2 operator -(IntVector2 c1, IntVector2 c2) {
  113.         return new IntVector2(c1._x - c2._x, c1._y - c2._y);
  114.     }
  115.  
  116.     public static IntVector2 operator -(IntVector2 c) {
  117.         return new IntVector2(-c._x, -c._y);
  118.     }
  119.  
  120.     public static IntVector2 operator *(IntVector2 c1, int scalar) {
  121.         return new IntVector2(c1._x * scalar, c1._y * scalar);
  122.     }
  123.  
  124.     public static Vector2 operator *(IntVector2 c1, float scalar) {
  125.         return new Vector2(c1._x * scalar, c1._y * scalar);
  126.     }
  127.  
  128.     public static IntVector2 operator / (IntVector2 c1, int divisor) {
  129.         return new IntVector2(c1._x / divisor, c1._y / divisor);
  130.     }
  131.  
  132.     public static implicit operator Vector2(IntVector2 v) {
  133.         return new Vector2(v.x, v.y);
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment