Files
dusk/src/dusk/script/module/entity/display/moduleentitymesh.h
T
2026-04-21 23:27:53 -05:00

84 lines
2.4 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/module/modulebase.h"
#include "entity/entity.h"
#include "entity/component/display/entitymesh.h"
typedef struct {
entityid_t entityId;
componentid_t compId;
} entitymesh_handle_t;
static void pushEntityMeshHandle(
lua_State *L,
entityid_t entityId,
componentid_t compId
) {
entitymesh_handle_t *h = lua_newuserdata(L, sizeof(entitymesh_handle_t));
h->entityId = entityId;
h->compId = compId;
luaL_getmetatable(L, "entitymesh_mt");
lua_setmetatable(L, -2);
}
static int entityMeshIndex(lua_State *L) {
lua_getmetatable(L, 1);
lua_getfield(L, -1, luaL_checkstring(L, 2));
return 1;
}
static int entityMeshGeneratePlaneMethod(lua_State *L) {
entitymesh_handle_t *h = luaL_checkudata(L, 1, "entitymesh_mt");
assertNotNull(h, "EntityMesh handle cannot be NULL");
float_t width = (float_t)luaL_checknumber(L, 2);
float_t height = (float_t)luaL_checknumber(L, 3);
errorret_t err = entityMeshGeneratePlane(h->entityId, h->compId, width, height);
if(err.code != ERROR_OK) {
luaL_error(L, "Failed to generate plane mesh");
}
return 0;
}
static int entityMeshGenerateCapsuleMethod(lua_State *L) {
entitymesh_handle_t *h = luaL_checkudata(L, 1, "entitymesh_mt");
assertNotNull(h, "EntityMesh handle cannot be NULL");
float_t radius = (float_t)luaL_checknumber(L, 2);
float_t halfHeight = (float_t)luaL_checknumber(L, 3);
errorret_t err = entityMeshGenerateCapsule(
h->entityId, h->compId, radius, halfHeight
);
if(err.code != ERROR_OK) {
luaL_error(L, "Failed to generate capsule mesh");
}
return 0;
}
static int entityMeshAdd(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_MESH);
pushEntityMeshHandle(L, entityId, compId);
return 1;
}
static void moduleEntityMesh(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
luaL_newmetatable(L, "entitymesh_mt");
lua_pushcfunction(L, entityMeshIndex);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, entityMeshGeneratePlaneMethod);
lua_setfield(L, -2, "generatePlane");
lua_pushcfunction(L, entityMeshGenerateCapsuleMethod);
lua_setfield(L, -2, "generateCapsule");
lua_pop(L, 1);
lua_register(L, "entityMeshAdd", entityMeshAdd);
}