diff --git a/include/simpleengine/math/math.h b/include/simpleengine/math/math.h new file mode 100644 index 0000000..a149cab --- /dev/null +++ b/include/simpleengine/math/math.h @@ -0,0 +1,47 @@ +#include +#include + +namespace simpleengine { + // Float vectors + using Vec4f = glm::vec4; + using Vec3f = glm::vec3; + using Vec2f = glm::vec2; + + // Integer vectors + using Vec4i = glm::ivec4; + using Vec3i = glm::ivec3; + using Vec2i = glm::ivec2; + + // Float matrixes + using Mat4f = glm::mat4; + using Mat3f = glm::mat3; + using Mat2f = glm::mat2; + + using Mat2x2f = glm::mat2x2; + using Mat2x3f = glm::mat2x3; + using Mat2x4f = glm::mat2x4; + using Mat3x2f = glm::mat3x2; + using Mat3x3f = glm::mat3x3; + using Mat3x4f = glm::mat3x4; + using Mat4x2f = glm::mat4x2; + using Mat4x3f = glm::mat4x3; + using Mat4x4f = glm::mat4x4; + + // Integer matrixes + using Mat4i = glm::imat4x4; + using Mat3i = glm::imat3x3; + using Mat2i = glm::imat2x2; + + using Mat2x2i = glm::imat2x2; + using Mat2x3i = glm::imat2x3; + using Mat2x4i = glm::imat2x4; + using Mat3x2i = glm::imat3x2; + using Mat3x3i = glm::imat3x3; + using Mat3x4i = glm::imat3x4; + using Mat4x2i = glm::imat4x2; + using Mat4x3i = glm::imat4x3; + using Mat4x4i = glm::imat4x4; + + // Quaternions + using Quat = glm::quat; +} \ No newline at end of file diff --git a/include/simpleengine/scripting/lua/engine_bindings.h b/include/simpleengine/scripting/lua/engine_bindings.h new file mode 100644 index 0000000..dbca4e9 --- /dev/null +++ b/include/simpleengine/scripting/lua/engine_bindings.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace simpleengine::scripting::lua { + class EngineBindings { + public: + static sol::table bind_full_engine(sol::this_state s); + + /// This binds general engine objects, like vec3, mat, quats, etc + static void bind_math_objects(sol::this_state s); + }; +} \ No newline at end of file diff --git a/src/scripting/lua/ecs_bindings.cpp b/src/scripting/lua/ecs_bindings.cpp index 8e50e0d..f5f44cd 100644 --- a/src/scripting/lua/ecs_bindings.cpp +++ b/src/scripting/lua/ecs_bindings.cpp @@ -45,8 +45,7 @@ namespace simpleengine::scripting::lua { "create", [](entt::registry& self) { return self.create(); }, "destroy", [](entt::registry& self, entt::entity entity) { return self.destroy(entity); }, - "emplace", - [](entt::registry& self, entt::entity entity, const sol::table& comp, sol::this_state s) -> sol::object { + "emplace", [](entt::registry& self, entt::entity entity, const sol::table& comp, sol::this_state s) -> sol::object { if (!comp.valid()) return sol::lua_nil_t{}; @@ -54,41 +53,38 @@ namespace simpleengine::scripting::lua { return maybe_any ? maybe_any.cast() : sol::lua_nil_t{}; }, - "remove", - [](entt::registry& self, entt::entity entity, const sol::object& type_or_id) { + "remove", [](entt::registry& self, entt::entity entity, const sol::object& type_or_id) { const auto maybe_any = EnttMetaHelper::invoke_meta_func(EnttMetaHelper::deduce_type(type_or_id), "remove"_hs, &self, entity); return maybe_any ? maybe_any.cast() : 0; }, - "has", - [](entt::registry& self, entt::entity entity, const sol::object& type_or_id) { + "has", [](entt::registry& self, entt::entity entity, const sol::object& type_or_id) { const auto maybe_any = EnttMetaHelper::invoke_meta_func(EnttMetaHelper::deduce_type(type_or_id), "has"_hs, &self, entity); return maybe_any ? maybe_any.cast() : false; }, - "any_of", - [](const sol::table& self, entt::entity entity, const sol::variadic_args& va) { + "any_of", [](const sol::table& self, entt::entity entity, const sol::variadic_args& va) { const auto types = collect_types(va); const auto has = self["has"].get(); return std::any_of(types.cbegin(), types.cend(), [&](auto type_id) { return has(self, entity, type_id).template get(); }); }, - "get", - [](entt::registry& self, entt::entity entity, const sol::object& type_or_id, sol::this_state s) { + "get", [](entt::registry& self, entt::entity entity, const sol::object& type_or_id, sol::this_state s) { const auto maybe_any = EnttMetaHelper::invoke_meta_func(EnttMetaHelper::deduce_type(type_or_id), "get"_hs, &self, entity, s); return maybe_any ? maybe_any.cast() : sol::lua_nil_t{}; }, - "clear", - sol::overload(&entt::registry::clear<>, [](entt::registry& self, sol::object type_or_id) { - EnttMetaHelper::invoke_meta_func(EnttMetaHelper::deduce_type(type_or_id), "clear"_hs, &self); - }), + "clear", sol::overload( + &entt::registry::clear<>, + [](entt::registry& self, sol::object type_or_id) { + EnttMetaHelper::invoke_meta_func(EnttMetaHelper::deduce_type(type_or_id), "clear"_hs, &self); + } + ), "orphan", &entt::registry::orphan, - "runtime_view", - [](entt::registry& self, const sol::variadic_args& va) { + "runtime_view", [](entt::registry& self, const sol::variadic_args& va) { const std::set types = collect_types(va); entt::runtime_view view{}; @@ -135,27 +131,41 @@ namespace simpleengine::scripting::lua { return std::tuple(pos, rot, scale); }, - // TODO: Implement glm::vec3 - /* "get_pos", &ecs::TransformComponent::get_pos, + "get_pos", &ecs::TransformComponent::get_pos, "get_scale", &ecs::TransformComponent::get_scale, - "get_rotation_quat", &ecs::TransformComponent::get_rotation_quat, */ - // combine_transform(const glm::mat4& transform_matrix) - // combine_transform(const TransformComponent& transformable) + "get_rotation_quat", &ecs::TransformComponent::get_rotation_quat, + "combine_transform", sol::overload( + [](ecs::TransformComponent& self, const glm::mat4& transform_matrix) { + self.combine_transform(transform_matrix); + }, + [](ecs::TransformComponent& self, const ecs::TransformComponent& transformable) { + self.combine_transform(transformable); + } + ), - "translate", sol::overload([](ecs::TransformComponent& self, float x, float y, float z) { - self.translate(x, y, z); - }), // TODO: Implement glm::vec3 translate + "translate", sol::overload( + [](ecs::TransformComponent& self, const glm::vec3& vec) { + self.translate(vec); + }, + [](ecs::TransformComponent& self, float x, float y, float z) { + self.translate(x, y, z); + } + ), "rotate", &ecs::TransformComponent::rotate, "rotate_x", &ecs::TransformComponent::rotate_x, "rotate_y", &ecs::TransformComponent::rotate_y, "rotate_z", &ecs::TransformComponent::rotate_z, - "scale", sol::overload([](ecs::TransformComponent& self, float scalar) { - self.scale(scalar); - }, [](ecs::TransformComponent& self, float x_scalar, float y_scalar, float z_scalar) { - self.scale(glm::vec3(x_scalar, y_scalar, z_scalar)); - }), // TODO: Implement glm::vec3 scale + "scale", sol::overload( + [](ecs::TransformComponent& self, float scalar) { + self.scale(scalar); + }, [](ecs::TransformComponent& self, float x_scalar, float y_scalar, float z_scalar) { + self.scale(glm::vec3(x_scalar, y_scalar, z_scalar)); + }, [](ecs::TransformComponent& self, const glm::vec3& vec) { + self.scale(vec); + } + ), sol::meta_function::to_string, [](const ecs::TransformComponent& self) { return glm::to_string(self.transform_matrix); diff --git a/src/scripting/lua/engine_bindings.cpp b/src/scripting/lua/engine_bindings.cpp new file mode 100644 index 0000000..e0c3327 --- /dev/null +++ b/src/scripting/lua/engine_bindings.cpp @@ -0,0 +1,328 @@ +#include "scripting/lua/engine_bindings.h" +#include "math/math.h" + +#include +#include + +#include +#include +#include + +#include + +namespace simpleengine::scripting::lua { + sol::table EngineBindings::bind_full_engine(sol::this_state s) { + bind_math_objects(s); + + return sol::table{}; + } + + void EngineBindings::bind_math_objects(sol::this_state s) { + sol::state_view lua{s}; + + lua.new_usertype("Vec2", + "x", &simpleengine::Vec2f::x, + "y", &simpleengine::Vec2f::y, + + sol::call_constructor, sol::factories( + [](const float& val) { + return simpleengine::Vec2f(val); + }, + [](const float& x, const float& y) { + return simpleengine::Vec2f(x, y); + } + ), + + sol::meta_function::addition, sol::overload( + [](const simpleengine::Vec2f& a, const simpleengine::Vec2f& b) { + return a + b; + }, + [](const simpleengine::Vec2f& self, const float& val) { + return self + val; + } + ), + sol::meta_function::subtraction, sol::overload( + [](const simpleengine::Vec2f& a, const simpleengine::Vec2f& b) { + return a - b; + }, + [](const simpleengine::Vec2f& self, const float& val) { + return self - val; + } + ), + sol::meta_function::multiplication, sol::overload( + [](const simpleengine::Vec2f& a, const simpleengine::Vec2f& b) { + return a * b; + }, + [](const simpleengine::Vec2f& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, sol::overload( + [](const simpleengine::Vec2f& a, const simpleengine::Vec2f& b) { + return a / b; + }, + [](const simpleengine::Vec2f& self, const float& val) { + return self / val; + } + ), + sol::meta_function::to_string, [](const simpleengine::Vec2f& self) { + std::stringstream ss; + ss << "(" << self.x << ", " << self.y << ")"; + + return ss.str(); + } + ); + + lua.new_usertype("Vec3", + "x", &simpleengine::Vec3f::x, + "y", &simpleengine::Vec3f::y, + "z", &simpleengine::Vec3f::z, + + sol::call_constructor, sol::factories( + [](const float& val) { + return simpleengine::Vec3f(val); + }, + [](const float& x, const float& y, const float& z) { + return simpleengine::Vec3f(x, y, z); + } + ), + + sol::meta_function::addition, sol::overload( + [](const simpleengine::Vec3f& a, const simpleengine::Vec3f& b) { + return a + b; + }, + [](const simpleengine::Vec3f& self, const float& val) { + return self + val; + } + ), + sol::meta_function::subtraction, sol::overload( + [](const simpleengine::Vec3f& a, const simpleengine::Vec3f& b) { + return a - b; + }, + [](const simpleengine::Vec3f& self, const float& val) { + return self - val; + } + ), + sol::meta_function::multiplication, sol::overload( + [](const simpleengine::Vec3f& a, const simpleengine::Vec3f& b) { + return a * b; + }, + [](const simpleengine::Vec3f& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, sol::overload( + [](const simpleengine::Vec3f& a, const simpleengine::Vec3f& b) { + return a / b; + }, + [](const simpleengine::Vec3f& self, const float& val) { + return self / val; + } + ), + sol::meta_function::index, [](const glm::mat3& self, const int& i) { + return self[i]; + }, + sol::meta_function::to_string, [](const simpleengine::Vec3f& self) { + std::stringstream ss; + ss << "(" << self.x << ", " << self.y << ", " << self.z << ")"; + + return ss.str(); + } + ); + + lua.new_usertype("Vec4", + "w", &simpleengine::Vec4f::w, + "x", &simpleengine::Vec4f::x, + "y", &simpleengine::Vec4f::y, + "z", &simpleengine::Vec4f::z, + + sol::call_constructor, sol::factories( + [](const float& val) { + return simpleengine::Vec4f(val); + }, + [](const float& x, const float& y, const float& z, const float& w) { + return simpleengine::Vec4f(x, y, z, w); + } + ), + + sol::meta_function::addition, sol::overload( + [](const simpleengine::Vec4f& a, const simpleengine::Vec4f& b) { + return a + b; + }, + [](const simpleengine::Vec4f& self, const float& val) { + return self + val; + } + ), + sol::meta_function::subtraction, sol::overload( + [](const simpleengine::Vec4f& a, const simpleengine::Vec4f& b) { + return a - b; + }, + [](const simpleengine::Vec4f& self, const float& val) { + return self - val; + } + ), + sol::meta_function::multiplication, sol::overload( + [](const simpleengine::Vec4f& a, const simpleengine::Vec4f& b) { + return a * b; + }, + [](const simpleengine::Vec4f& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, sol::overload( + [](const simpleengine::Vec4f& a, const simpleengine::Vec4f& b) { + return a / b; + }, + [](const simpleengine::Vec4f& self, const float& val) { + return self / val; + } + ), + sol::meta_function::index, [](const simpleengine::Vec4f& self, const int& i) { + return self[i]; + }, + sol::meta_function::to_string, [](const simpleengine::Vec4f& self) { + std::stringstream ss; + ss << "(" << self.x << ", " << self.y << ", " << self.z << ", " << self.w << ")"; + + return ss.str(); + } + ); + + lua.new_usertype("Quat", + "w", &simpleengine::Quat::w, + "x", &simpleengine::Quat::x, + "y", &simpleengine::Quat::y, + "z", &simpleengine::Quat::z, + + sol::call_constructor, sol::factories( + [](const float& w, const float& x, const float& y, const float& z) { + return simpleengine::Quat(w, x, y, z); + } + ), + + sol::meta_function::addition, [](const simpleengine::Quat& a, const simpleengine::Quat& b) { + return a + b; + }, + sol::meta_function::subtraction, [](const simpleengine::Quat& a, const simpleengine::Quat& b) { + return a - b; + }, + sol::meta_function::multiplication, sol::overload( + [](const simpleengine::Quat& a, const simpleengine::Quat& b) { + return a * b; + }, + [](const simpleengine::Quat& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, [](const simpleengine::Quat& self, const float& val) { + return self / val; + }, + sol::meta_function::to_string, [](const simpleengine::Quat& self) { + std::stringstream ss; + ss << "(" << self.w << ", " << self.x << ", " << self.y << ", " << self.z << ")"; + + return ss.str(); + } + ); + + lua.new_usertype("Mat3", + sol::call_constructor, sol::factories( + [](const float& val) { + return simpleengine::Mat3f(val); + }, + []() { + return simpleengine::Mat3f(1.f); + } + ), + + sol::meta_function::addition, sol::overload( + [](const glm::mat3& a, const glm::mat3& b) { + return a + b; + }, + [](const glm::mat3& self, const float& val) { + return self + val; + } + ), + sol::meta_function::subtraction, sol::overload( + [](const glm::mat3& a, const glm::mat3& b) { + return a - b; + }, + [](const glm::mat3& self, const float& val) { + return self - val; + } + ), + sol::meta_function::multiplication, sol::overload( + [](const glm::mat3& a, const glm::mat3& b) { + return a * b; + }, + [](const glm::mat3& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, sol::overload( + [](const glm::mat3& a, const glm::mat3& b) { + return a / b; + }, + [](const glm::mat3& self, const float& val) { + return self / val; + } + ), + sol::meta_function::index, [](const glm::mat3& self, const int& i) { + return self[i]; + }, + sol::meta_function::to_string, [](const glm::mat3& self) { + return glm::to_string(self); + } + ); + + lua.new_usertype("Mat4", + sol::call_constructor, sol::factories( + [](const float& val) { + return simpleengine::Mat4f(val); + }, + []() { + return simpleengine::Mat4f(1.f); + } + ), + + sol::meta_function::addition, sol::overload( + [](const simpleengine::Mat4f& a, const simpleengine::Mat4f& b) { + return a + b; + }, + [](const simpleengine::Mat4f& self, const float& val) { + return self + val; + } + ), + sol::meta_function::subtraction, sol::overload( + [](const simpleengine::Mat4f& a, const simpleengine::Mat4f& b) { + return a - b; + }, + [](const simpleengine::Mat4f& self, const float& val) { + return self - val; + } + ), + sol::meta_function::multiplication, sol::overload( + [](const simpleengine::Mat4f& a, const simpleengine::Mat4f& b) { + return a * b; + }, + [](const simpleengine::Mat4f& self, const float& val) { + return self * val; + } + ), + sol::meta_function::division, sol::overload( + [](const simpleengine::Mat4f& a, const simpleengine::Mat4f& b) { + return a / b; + }, + [](const simpleengine::Mat4f& self, const float& val) { + return self / val; + } + ), + sol::meta_function::index, [](const simpleengine::Mat4f& self, const int& i) { + return self[i]; + }, + sol::meta_function::to_string, [](const simpleengine::Mat4f& self) { + return glm::to_string(self); + } + ); + } +} \ No newline at end of file diff --git a/src/scripting/lua/lua_scripting_engine.cpp b/src/scripting/lua/lua_scripting_engine.cpp index 4bbca58..45409fe 100644 --- a/src/scripting/lua/lua_scripting_engine.cpp +++ b/src/scripting/lua/lua_scripting_engine.cpp @@ -6,6 +6,7 @@ #include "ecs/component/transform_component.h" #include "scripting/lua/ecs_bindings.h" +#include "scripting/lua/engine_bindings.h" #include @@ -107,16 +108,15 @@ namespace simpleengine::scripting::lua { // Make registry available to lua lua["registry"] = std::ref(entity_registry->get_inner()); - // Registry TransformComponent - //bindings::bind_registry(); - lua.require("ecs", sol::c_call, false); // create registry type - //lua.require("registry", sol::c_call, false); // create registry type - + lua.require("engine", sol::c_call, false); + lua.require("ecs", sol::c_call, false); // Bind all of the ECS entt::entity en = entity_registry->get_inner().create(); lua["dog"] = en; run_script(R"LUA( + local component = ecs.component + print('start') --local dog = registry:create() local cat = registry:create() @@ -125,16 +125,16 @@ namespace simpleengine::scripting::lua { print('Dog is ' .. dog .. ', and registry size is ' .. registry:size()) print('Cat is ' .. cat .. ', and cat size is ' .. registry:size()) - registry:emplace(dog, ecs.component.TransformComponent(5, 6, 3)) + registry:emplace(dog, component.TransformComponent(5, 6, 3)) - assert(registry:has(dog, ecs.component.TransformComponent)) - assert(registry:has(dog, ecs.component.TransformComponent.type_id())) + assert(registry:has(dog, component.TransformComponent)) + assert(registry:has(dog, component.TransformComponent.type_id())) assert(not registry:any_of(dog, -1, -2, -3)) function update(delta_time) - transform = registry:get(dog, ecs.component.TransformComponent) - print('Dog position = ' .. tostring(transform)) + transform = registry:get(dog, component.TransformComponent) + print('Dog position = ' .. tostring(transform:get_pos())) transform:translate(0, 0, 1) end