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 2.0KB

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