Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1
12#ifndef MINESERVER_VECTOR_HPP
13#define MINESERVER_VECTOR_HPP
14
15#include <cstdint>
16#include <type_traits>
17#include <plugins/luaheaders.h>
18
24template <typename T>
25class Vec
26{
27public:
28 static_assert(std::is_arithmetic_v<T>, "Type should be a number");
44
50 Vec() : x(0), y(0), z(0) {}
58 Vec(T x, T y) : x(x), y(y), z(0) {}
66 Vec(T x, T y, T z) : x(x), y(y), z(z) {}
71 ~Vec() = default;
72
79 template <typename O>
81 {
82 x = (T)other.x;
83 y = (T)other.y;
84 z = (T)other.z;
85 }
86
94 template <typename O>
96 {
97 x = (T)(x + other.x);
98 y = (T)(y + other.y);
99 z = (T)(z + other.z);
100 return *this;
101 }
102
110 template <typename O>
112 {
113 x = (T)(x - other.x);
114 y = (T)(y - other.y);
115 z = (T)(z - other.z);
116 return *this;
117 }
118
126 template <typename O>
128 {
129 x = (T)(x * other.x);
130 y = (T)(y * other.y);
131 z = (T)(z * other.z);
132 return *this;
133 }
134
142 template <typename O>
144 {
145 x = (T)(x / other.x);
146 y = (T)(y / other.y);
147 z = (T)(z / other.z);
148 return *this;
149 }
150
158 template <typename O>
160 {
161 static_assert(std::is_arithmetic_v<O>, "Type should be a number");
162 x *= other;
163 y *= other;
164 z *= other;
165 return *this;
166 }
167
175 template <typename O>
177 {
178 static_assert(std::is_arithmetic_v<O>, "Type should be a number");
179 x /= other;
180 y /= other;
181 z /= other;
182 return *this;
183 }
184};
185
198
206static void loadVectorLua(lua_State *state, const char *namespaceName)
207{
208 luabridge::getGlobalNamespace(state)
209 .beginNamespace(namespaceName)
210 .beginClass<Vecf>("Vecf")
211 .addConstructor<void(), void(float, float), void(float, float, float)>()
212 .addProperty("x", &Vecf::x)
213 .addProperty("y", &Vecf::y)
214 .addProperty("z", &Vecf::z)
215 .endClass()
216 .beginClass<Veci32>("Veci32")
217 .addConstructor<void(), void(std::int32_t, std::int32_t), void(std::int32_t, std::int32_t, std::int32_t)>()
218 .addProperty("x", &Veci32::x)
219 .addProperty("y", &Veci32::y)
220 .addProperty("z", &Veci32::z)
221 .endClass()
222 .endNamespace();
223}
224
225#endif // MINESERVER_VECTOR_HPP
Vector 3 (x, y, z) with variable type.
Definition vector.hpp:26
T z
Z coordinate.
Definition vector.hpp:43
Vec()
Construct a new Vec object.
Definition vector.hpp:50
Vec & operator-(const Vec< O > &other)
Vector Substraction operator.
Definition vector.hpp:111
Vec & operator*(const O &other)
Scalar Multiplication operator.
Definition vector.hpp:159
Vec & operator*(const Vec< O > &other)
Vector Multiplication operator.
Definition vector.hpp:127
Vec & operator+(const Vec< O > &other)
Vector Addition operator.
Definition vector.hpp:95
Vec(T x, T y)
Construct a new Vec object.
Definition vector.hpp:58
Vec & operator/(const Vec< O > &other)
Vector Division operator.
Definition vector.hpp:143
Vec(const Vec< O > &other)
Construct a new Vec object from another.
Definition vector.hpp:80
Vec(T x, T y, T z)
Construct a new Vec object.
Definition vector.hpp:66
T x
X coordinate.
Definition vector.hpp:33
~Vec()=default
Destroy the Vec object.
Vec & operator/(const O &other)
Scalar Division operator.
Definition vector.hpp:176
T y
Y coordinate.
Definition vector.hpp:38
constexpr std::string_view type_name()
Gets the name of the type paramater.
Definition event.h:56
Utility header file for lua things.
Vec< float > Vecf
Vector 3 Float.
Definition vector.hpp:191
Vec< std::int32_t > Veci32
Vector 3 Int.
Definition vector.hpp:197
static void loadVectorLua(lua_State *state, const char *namespaceName)
Loads Vector to lua state.
Definition vector.hpp:206