Skip to content

Commit

Permalink
+[script] bind more
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualGMQ committed Feb 3, 2024
1 parent 915614e commit 84974af
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion nickel/src/script/luabind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct Stack<nickel::Flip>
: luabridge::Enum<nickel::Flip, nickel::Flip::None, nickel::Flip::Vertical,
nickel::Flip::Horizontal, nickel::Flip::Both> {};

template <>
struct Stack<gecs::entity>
: luabridge::Enum<gecs::entity, gecs::null_entity> {};

template <>
struct Stack<nickel::FileType>
: luabridge::Enum<nickel::FileType, nickel::FileType::Animation,
Expand Down Expand Up @@ -356,7 +360,6 @@ void bindHandle(luabridge::Namespace& scope, const std::string& name) {
return name + "(Null)";
}
})

.endClass();
}

Expand Down Expand Up @@ -738,6 +741,36 @@ void bindInput(luabridge::Namespace& scope) {
.endClass();
}

template <typename T>
bool hasComponent(lua_State* L) {
gecs::entity ent;
auto reg = ECS::Instance().World().cur_registry();
if (reg) {
return reg->has<T>(ent);
}
return false;
}

template <typename T>
T* getComponent(lua_State* L) {
gecs::entity ent;
auto reg = ECS::Instance().World().cur_registry();
if (reg) {
return &reg->get_mut<T>(ent);
}
return nullptr;
}

void bindECS(luabridge::Namespace& scope) {
scope =
scope.beginNamespace("ecs")
.addFunction("HasSprite", hasComponent<Sprite>)
.addFunction("GetSprite", getComponent<Sprite>)
.addFunction("HasTransform", hasComponent<Transform>)
.addFunction("GetTransform", getComponent<Transform>)
.endNamespace();
}

// clang-format on

void BindLua(lua_State* L) {
Expand All @@ -748,6 +781,7 @@ void BindLua(lua_State* L) {
bindTransform(scope);
bindGraphics(scope);
bindInput(scope);
bindECS(scope);
scope.endNamespace();
}

Expand Down

0 comments on commit 84974af

Please sign in to comment.