Skip to content

Commit

Permalink
feat(math): add some setters for scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 12, 2019
1 parent a3a8846 commit aefc553
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 27 deletions.
10 changes: 4 additions & 6 deletions modules/lua/antara/gaming/lua/antara.lua.system.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,10 @@ namespace antara::gaming::lua::tests
assert(entt.entity_registry:has_layer_1_component(entity) == true)
entt.entity_registry:remove_layer_1_component(entity)
assert(entt.entity_registry:has_layer_1_component(entity) == false)
local pos = entt.entity_registry:add_position_component(entity)
print("pos.pos_x: " .. pos.pos_x)
pos.pos_x = pos.pos_x + 1
print("pos.pos_x: " .. pos.pos_x)
local same_pos = entt.entity_registry:get_position_component(entity)
assert(same_pos.pos_x == pos.pos_x, "should be equal")
local pos = entt.entity_registry:add_position_2d_component(entity)
pos:set_x(pos:x() + 1)
local same_pos = entt.entity_registry:get_position_2d_component(entity)
assert(same_pos:x() == pos:x())
entt.entity_registry:destroy(entity)
end
test_basis()
Expand Down
10 changes: 9 additions & 1 deletion modules/math/antara/gaming/math/antara.math.vector.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ namespace antara::gaming::math::tests

CHECK_EQ(vec3_float, math::vec3f::scalar(43.f));

vec3_float.set_x(3.f);
vec3_float.set_y(4.f);
vec3_float.set_z(5.f);
CHECK_EQ(vec3_float, math::vec3f{3.f, 4.f, 5.f});
vec3_float.set_xy(42.f, 42.f);
CHECK_EQ(vec3_float, math::vec3f{42.f, 42.f, 5.f});
vec3_float.set_xyz(42.f, 42.f, 42.f);
CHECK_EQ(vec3_float, math::vec3f{42.f, 42.f, 42.f});
}

TEST_CASE("cast")
Expand All @@ -149,7 +157,7 @@ namespace antara::gaming::math::tests
math::vec2f vec_float{42.f, 42.f};
refl::util::for_each(refl::reflect(vec_float).members, [&](auto member)
{
MESSAGE(member(vec_float));
MESSAGE(member.name);
});
}
}
79 changes: 61 additions & 18 deletions modules/math/antara/gaming/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

namespace antara::gaming::math
{
template<class Unit, size_t Size, template<class> class...Mixins>
class basic_vector : public Mixins<basic_vector<Unit, Size, Mixins...>> ...
template<class Unit, size_t Size, template<class, class> class...Mixins>
class basic_vector : public Mixins<basic_vector<Unit, Size, Mixins...>, Unit> ...
{

template<class, size_t, template<class> class...>
template<class, size_t, template<class, class> class...>
friend
class basic_vector;

Expand Down Expand Up @@ -144,7 +144,7 @@ namespace antara::gaming::math
{ return data_.end(); }

// Implicit cast
template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr operator basic_vector<NewUnit, Size, NewMixins...>() const noexcept
{
static_assert(std::is_convertible_v<Unit, NewUnit>, "Impossible cast from [value_type] to [NewUnit]");
Expand Down Expand Up @@ -269,37 +269,37 @@ namespace antara::gaming::math
return *this / length();
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator==(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return test_predicate([](Unit a, Unit b) { return a == b; }, rhs, sequence_type{});
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator!=(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return !(*this == rhs);
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator<(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return test_predicate([](Unit a, Unit b) { return a < b; }, rhs, sequence_type{});
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator>=(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return !(*this < rhs);
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator>(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return rhs < *this;
}

template<class NewUnit, template<class> class...NewMixins>
template<class NewUnit, template<class, class> class...NewMixins>
constexpr bool operator<=(basic_vector<NewUnit, Size, NewMixins...> const &rhs) const noexcept
{
return !(rhs < *this);
Expand All @@ -308,10 +308,11 @@ namespace antara::gaming::math

namespace vector_mixins
{
template<class Derived>
template<class Derived, class Unit>
class access_xy
{
public:
using value_type = Unit;
constexpr auto x() const noexcept
{ return static_cast<Derived const *>(this)->template get<0>(); }

Expand All @@ -323,17 +324,49 @@ namespace antara::gaming::math

constexpr auto &y_ref() noexcept
{ return static_cast<Derived *>(this)->template get<1>(); }

constexpr void set_x(value_type value) noexcept
{
x_ref() = value;
}

constexpr void set_y(value_type value) noexcept
{
y_ref() = value;
}

constexpr void set_xy(value_type value_x, value_type value_y) noexcept
{
set_x(value_x);
set_y(value_y);
}
};

template<class Derived>
template<class Derived, class Unit>
class access_z
{
public:
using value_type = Unit;

constexpr auto z() const noexcept
{ return static_cast<Derived const *>(this)->template get<2>(); }

constexpr auto &z_ref() noexcept
{ return static_cast<Derived *>(this)->template get<2>(); }

constexpr void set_z(value_type value_z) noexcept
{
z_ref() = value_z;
}

constexpr void set_xyz(value_type value_x, value_type value_y, value_type value_z) noexcept
{
auto& x_ref = static_cast<Derived *>(this)->template get<0>();
x_ref = value_x;
auto& y_ref = static_cast<Derived *>(this)->template get<1>();
y_ref = value_y;
z_ref() = value_z;
}
};
}

Expand Down Expand Up @@ -372,13 +405,23 @@ namespace antara::gaming::math
using vec3ld = vec3<long double>;
}

namespace std {
namespace std
{

template<class Unit, size_t Size, template<class> class...Mixins>
struct tuple_size<antara::gaming::math::basic_vector<Unit, Size, Mixins...>> : integral_constant<size_t, Size> {};
template<class Unit, size_t Size, template<class, class> class...Mixins>
struct tuple_size<antara::gaming::math::basic_vector<Unit, Size, Mixins...>> : integral_constant<size_t, Size>
{
};

template <size_t I, class Unit, size_t Size, template<class> class...Mixins>
struct tuple_element<I, antara::gaming::math::basic_vector<Unit, Size, Mixins...>> { using type = Unit; };
template<size_t I, class Unit, size_t Size, template<class, class> class...Mixins>
struct tuple_element<I, antara::gaming::math::basic_vector<Unit, Size, Mixins...>>
{
using type = Unit;
};
}

REFL_AUTO(type(antara::gaming::math::vec2f), func(x), func(y), func(x_ref), func(y_ref), func(size));
REFL_AUTO(type(antara::gaming::math::vec2f), func(x), func(y), func(x_ref), func(y_ref), func(size), func(set_x),
func(set_y));

REFL_AUTO(type(antara::gaming::math::vec3f), func(x), func(y), func(x_ref), func(y_ref), func(size), func(set_x),
func(set_y), func(set_xyz));
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ namespace antara::gaming::transform
struct position_2d : public math::vec2f
{
template<typename ... Args>
position_2d(Args&& ...args) noexcept: math::vec2f(std::forward<Args>(args)...)
position_2d(Args &&...args) noexcept: math::vec2f(std::forward<Args>(args)...)
{

}
};
}

REFL_AUTO(type(antara::gaming::transform::position_2d), func(x), func(y), func(x_ref), func(y_ref), func(size))
REFL_AUTO(type(antara::gaming::transform::position_2d), func(x), func(y), func(x_ref), func(y_ref), func(size),
func(set_x), func(set_y))

0 comments on commit aefc553

Please sign in to comment.