From 410f971972bde37fb852420ea2ca0e2f69f27256 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 30 Jan 2021 09:41:31 -0500 Subject: Encapsulated some player movement stuff Imported vector from therapy5 --- src/vector.h | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/vector.h (limited to 'src/vector.h') diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..9973ea6 --- /dev/null +++ b/src/vector.h @@ -0,0 +1,102 @@ +#ifndef VECTOR_H_5458ED71 +#define VECTOR_H_5458ED71 + +template +class vec2 { +public: + + T coords[2]; + + vec2() : coords{0, 0} { + } + + vec2(T x, T y) : coords{x, y} { + } + + inline T& x() { + return coords[0]; + } + + inline const T& x() const { + return coords[0]; + } + + inline T& w() { + return coords[0]; + } + + inline const T& w() const { + return coords[0]; + } + + inline T& y() { + return coords[1]; + } + + inline const T& y() const { + return coords[1]; + } + + inline T& h() { + return coords[1]; + } + + inline const T& h() const { + return coords[1]; + } + + template + operator vec2() const { + return vec2(x(), y()); + } + + vec2 operator+(const vec2& other) const { + return vec2(x() + other.x(), y() + other.y()); + } + + vec2& operator+=(const vec2& other) { + x() += other.x(); + y() += other.y(); + + return *this; + } + + vec2 operator-(const vec2& other) const { + return vec2(x() - other.x(), y() - other.y()); + } + + vec2 operator-=(const vec2& other) { + x() -= other.x(); + y() -= other.y(); + + return *this; + } + + vec2 operator-() const { + return vec2(-x(), -y()); + } + + vec2 operator*(T s) const { + return vec2(x() * s, y() * s); + } + + vec2& operator*=(T s) { + x() *= s; + y() *= s; + + return *this; + } + + bool operator==(const vec2& other) const { + return std::tie(x(), other.x()) == std::tie(y(), other.y()); + } + + bool operator!=(const vec2& other) const { + return std::tie(x(), other.x()) != std::tie(y(), other.y()); + } + +}; + +using vec2i = vec2; + +#endif /* end of include guard: VECTOR_H_5458ED71 */ -- cgit 1.4.1