diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 09:41:31 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 09:41:31 -0500 |
commit | 410f971972bde37fb852420ea2ca0e2f69f27256 (patch) | |
tree | 73262614d4688e4f9a26c97557db5720b049029e /src/vector.h | |
parent | 78e5bd2e622204d0deab252d9b2ab90c3095b67d (diff) | |
download | tanetane-410f971972bde37fb852420ea2ca0e2f69f27256.tar.gz tanetane-410f971972bde37fb852420ea2ca0e2f69f27256.tar.bz2 tanetane-410f971972bde37fb852420ea2ca0e2f69f27256.zip |
Encapsulated some player movement stuff
Imported vector from therapy5
Diffstat (limited to 'src/vector.h')
-rw-r--r-- | src/vector.h | 102 |
1 files changed, 102 insertions, 0 deletions
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 @@ | |||
1 | #ifndef VECTOR_H_5458ED71 | ||
2 | #define VECTOR_H_5458ED71 | ||
3 | |||
4 | template <typename T> | ||
5 | class vec2 { | ||
6 | public: | ||
7 | |||
8 | T coords[2]; | ||
9 | |||
10 | vec2() : coords{0, 0} { | ||
11 | } | ||
12 | |||
13 | vec2(T x, T y) : coords{x, y} { | ||
14 | } | ||
15 | |||
16 | inline T& x() { | ||
17 | return coords[0]; | ||
18 | } | ||
19 | |||
20 | inline const T& x() const { | ||
21 | return coords[0]; | ||
22 | } | ||
23 | |||
24 | inline T& w() { | ||
25 | return coords[0]; | ||
26 | } | ||
27 | |||
28 | inline const T& w() const { | ||
29 | return coords[0]; | ||
30 | } | ||
31 | |||
32 | inline T& y() { | ||
33 | return coords[1]; | ||
34 | } | ||
35 | |||
36 | inline const T& y() const { | ||
37 | return coords[1]; | ||
38 | } | ||
39 | |||
40 | inline T& h() { | ||
41 | return coords[1]; | ||
42 | } | ||
43 | |||
44 | inline const T& h() const { | ||
45 | return coords[1]; | ||
46 | } | ||
47 | |||
48 | template <typename R> | ||
49 | operator vec2<R>() const { | ||
50 | return vec2<R>(x(), y()); | ||
51 | } | ||
52 | |||
53 | vec2 operator+(const vec2& other) const { | ||
54 | return vec2(x() + other.x(), y() + other.y()); | ||
55 | } | ||
56 | |||
57 | vec2& operator+=(const vec2& other) { | ||
58 | x() += other.x(); | ||
59 | y() += other.y(); | ||
60 | |||
61 | return *this; | ||
62 | } | ||
63 | |||
64 | vec2 operator-(const vec2& other) const { | ||
65 | return vec2(x() - other.x(), y() - other.y()); | ||
66 | } | ||
67 | |||
68 | vec2 operator-=(const vec2& other) { | ||
69 | x() -= other.x(); | ||
70 | y() -= other.y(); | ||
71 | |||
72 | return *this; | ||
73 | } | ||
74 | |||
75 | vec2 operator-() const { | ||
76 | return vec2(-x(), -y()); | ||
77 | } | ||
78 | |||
79 | vec2 operator*(T s) const { | ||
80 | return vec2(x() * s, y() * s); | ||
81 | } | ||
82 | |||
83 | vec2& operator*=(T s) { | ||
84 | x() *= s; | ||
85 | y() *= s; | ||
86 | |||
87 | return *this; | ||
88 | } | ||
89 | |||
90 | bool operator==(const vec2& other) const { | ||
91 | return std::tie(x(), other.x()) == std::tie(y(), other.y()); | ||
92 | } | ||
93 | |||
94 | bool operator!=(const vec2& other) const { | ||
95 | return std::tie(x(), other.x()) != std::tie(y(), other.y()); | ||
96 | } | ||
97 | |||
98 | }; | ||
99 | |||
100 | using vec2i = vec2<int>; | ||
101 | |||
102 | #endif /* end of include guard: VECTOR_H_5458ED71 */ | ||