A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector2.h 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <SFML/System/Vector2.hpp>
  3. #include <utility>
  4. namespace Swan {
  5. template<typename T>
  6. struct Vector2 {
  7. T x;
  8. T y;
  9. constexpr Vector2(T x = 0, T y = 0): x(x), y(y) {}
  10. Vector2<T> &set(T x, T y) {
  11. this->x = x;
  12. this->y = y;
  13. return *this;
  14. }
  15. operator sf::Vector2<T>() const {
  16. return sf::Vector2<T>(x, y);
  17. }
  18. operator std::pair<T, T>() const {
  19. return std::pair<T, T>(x, y);
  20. }
  21. operator Vector2<float>() const {
  22. return Vector2<float>(x, y);
  23. }
  24. bool operator==(const Vector2<T> &vec) const {
  25. return x == vec.x && y == vec.y;
  26. }
  27. bool operator!=(const Vector2<T> &vec) const {
  28. return !(*this == vec);
  29. }
  30. Vector2<T> operator-() const {
  31. return Vector2<T>(-x, -y);
  32. }
  33. Vector2<T> operator+(const Vector2<T> &vec) const {
  34. return Vector2<T>(x + vec.x, y + vec.y);
  35. }
  36. Vector2<T> &operator+=(const Vector2<T> &vec) {
  37. x += vec.x;
  38. y += vec.y;
  39. return *this;
  40. }
  41. Vector2<T> operator-(const Vector2<T> &vec) const {
  42. return Vector2<T>(x - vec.x, y - vec.y);
  43. }
  44. Vector2<T> &operator-=(const Vector2<T> &vec) {
  45. x -= vec.x;
  46. y -= vec.y;
  47. return *this;
  48. }
  49. Vector2<T> operator*(const Vector2<T> &vec) const {
  50. return Vector2<T>(x * vec.x, y * vec.y);
  51. }
  52. Vector2<T> &operator*=(const Vector2<T> &vec) {
  53. x *= vec.x;
  54. y *= vec.y;
  55. return *this;
  56. }
  57. Vector2<T> operator*(T num) const {
  58. return Vector2<T>(x * num, y * num);
  59. }
  60. Vector2<T> operator*=(T num) {
  61. x *= num;
  62. y *= num;
  63. return *this;
  64. }
  65. Vector2<T> operator/(const Vector2<T> &vec) const {
  66. return Vector2<T>(x / vec.x, y / vec.y);
  67. }
  68. Vector2<T> &operator/=(const Vector2<T> &vec) {
  69. x /= vec.x;
  70. y /= vec.y;
  71. return *this;
  72. }
  73. Vector2<T> operator/(T num) const {
  74. return Vector2<T>(x / num, y / num);
  75. }
  76. Vector2<T> operator/=(T num) {
  77. x /= num;
  78. y /= num;
  79. return *this;
  80. }
  81. static const Vector2<T> ZERO;
  82. };
  83. template<typename T> const Vector2<T> Vector2<T>::ZERO = Vector2<T>(0, 0);
  84. using Vec2 = Vector2<float>;
  85. using Vec2i = Vector2<int>;
  86. }