Helpers.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. using CustomExtensions;
  3. using System;
  4. public struct InputState{
  5. public int Tick;
  6. public Vector2 Input;
  7. public override string ToString()
  8. {
  9. return $"tick: {Tick}, input: {Input}";
  10. }
  11. public override bool Equals(object obj)
  12. {
  13. InputState _obj = (InputState) obj;
  14. if(_obj.Input == Input){
  15. return true;
  16. }
  17. return false;
  18. }
  19. public bool CloseEnough(InputState other, float threshold) => Mathf.Abs((other.Input-Input).magnitude) < threshold;
  20. }
  21. public struct PlayerState{
  22. public int Tick;
  23. public Vector3 Position;
  24. public Quaternion Rotation;
  25. public override string ToString()
  26. {
  27. return $"tick: {Tick},pos: {Position}, rot: {Rotation}";
  28. }
  29. public override bool Equals(object obj)
  30. {
  31. PlayerState _obj = (PlayerState) obj;
  32. if(_obj.Position == Position && _obj.Rotation == Rotation){
  33. return true;
  34. }
  35. return false;
  36. }
  37. public bool CloseEnough(PlayerState other, float threshold) => Mathf.Abs((other.Position-Position).magnitude) < threshold && Rotation.Approximately(other.Rotation, threshold);
  38. public float Difference(PlayerState other){
  39. return (Mathf.Abs((other.Position-Position).magnitude) + Rotation.Difference(other.Rotation))/2f;
  40. }
  41. }
  42. public class Helpers{
  43. public static int unixTimestamp = (int)System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1)).TotalSeconds;
  44. }
  45. namespace CustomExtensions{
  46. public static class QuaternionExtensions{
  47. public static bool Approximately(this Quaternion quatA, Quaternion value, float acceptableRange)
  48. {
  49. return 1 - Mathf.Abs(Quaternion.Dot(quatA, value)) < acceptableRange;
  50. }
  51. public static float Difference(this Quaternion quatA, Quaternion value){
  52. return 1 - Mathf.Abs(Quaternion.Dot(quatA, value));
  53. }
  54. public static long ToUnix(this DateTime time){
  55. return (long)time.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  56. }
  57. }
  58. }