Sdk/Binding: Add component access by name

Former-commit-id: 8b5cffb55c526124cddffc9f13a05bbecc638bb9
This commit is contained in:
Lynix 2016-05-05 20:40:25 +02:00
parent d678951c33
commit 5e52988d87
3 changed files with 63 additions and 45 deletions

View File

@ -83,12 +83,6 @@ namespace Ndk
Nz::LuaClass<Nz::Node> nodeClass;
// SDK
Nz::LuaClass<Application*> application;
Nz::LuaClass<EntityHandle> entityClass;
Nz::LuaClass<NodeComponentHandle> nodeComponent;
Nz::LuaClass<VelocityComponentHandle> velocityComponent;
Nz::LuaClass<WorldHandle> worldClass;
using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&);
using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&);
@ -100,7 +94,16 @@ namespace Ndk
Nz::String name;
};
ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 1);
Nz::LuaClass<Application*> application;
Nz::LuaClass<EntityHandle> entityClass;
Nz::LuaClass<NodeComponentHandle> nodeComponent;
Nz::LuaClass<VelocityComponentHandle> velocityComponent;
Nz::LuaClass<WorldHandle> worldClass;
std::vector<ComponentBinding> m_componentBinding;
std::unordered_map<Nz::String, ComponentIndex> m_componentBindingByName;
#ifndef NDK_SERVER
// Audio

View File

@ -21,6 +21,7 @@ namespace Ndk
m_componentBinding.resize(T::componentIndex + 1);
m_componentBinding[T::componentIndex] = std::move(binding);
m_componentBindingByName[name] = T::componentIndex;
}
template<typename T>

View File

@ -59,56 +59,29 @@ namespace Ndk
entityClass.BindMethod("Kill", &Entity::Kill);
entityClass.BindMethod("IsEnabled", &Entity::IsEnabled);
entityClass.BindMethod("IsValid", &Entity::IsValid);
entityClass.BindMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent);
entityClass.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents);
entityClass.BindMethod("__tostring", &EntityHandle::ToString);
entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
{
int index = 1;
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
ComponentBinding* binding = QueryComponentIndex(instance);
if (componentIndex > m_componentBinding.size())
{
lua.Error("Invalid component index");
return 0;
}
ComponentBinding& binding = m_componentBinding[componentIndex];
if (binding.name.IsEmpty())
{
lua.Error("This component is not available to the LuaAPI");
return 0;
}
return binding.adder(lua, handle);
return binding->adder(instance, handle);
});
entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
{
int index = 1;
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
ComponentBinding* binding = QueryComponentIndex(instance);
if (!handle->HasComponent(componentIndex))
{
lua.PushNil();
return 1;
}
return binding->getter(instance, handle->GetComponent(binding->index));
});
if (componentIndex > m_componentBinding.size())
{
lua.Error("Invalid component index");
return 0;
}
entityClass.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
{
ComponentBinding* binding = QueryComponentIndex(instance);
ComponentBinding& binding = m_componentBinding[componentIndex];
if (binding.name.IsEmpty())
{
lua.Error("This component is not available to the LuaAPI");
return 0;
}
return binding.getter(lua, handle->GetComponent(componentIndex));
handle->RemoveComponent(binding->index);
return 0;
});
/*********************************** Ndk::NodeComponent **********************************/
@ -199,4 +172,45 @@ namespace Ndk
}
instance.SetGlobal("ComponentType");
}
LuaBinding::ComponentBinding* LuaBinding::QueryComponentIndex(Nz::LuaInstance& instance, int argIndex)
{
switch (instance.GetType(argIndex))
{
case Nz::LuaType_Number:
{
ComponentIndex componentIndex = instance.Check<ComponentIndex>(&argIndex);
if (componentIndex > m_componentBinding.size())
{
instance.Error("Invalid component index");
return nullptr;
}
ComponentBinding& binding = m_componentBinding[componentIndex];
if (binding.name.IsEmpty())
{
instance.Error("Invalid component index");
return nullptr;
}
return &binding;
}
case Nz::LuaType_String:
{
const char* key = instance.CheckString(argIndex);
auto it = m_componentBindingByName.find(key);
if (it == m_componentBindingByName.end())
{
instance.Error("Invalid component name");
return nullptr;
}
return &m_componentBinding[it->second];
}
}
instance.Error("Invalid component index at #" + Nz::String::Number(argIndex));
return nullptr;
}
}