diff --git a/src/luaapi.c b/src/luaapi.c index fc45347..fac86cf 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -193,32 +193,74 @@ static int game_addentity(lua_State *L) { c.entity = id; seti("trigger", c.trigger); - seti("type", c.type); - seti("dynamics", c.dynamics); - if(c.type == CPHYSICS_CAPSULE) { - lua_getfield(L, -1, "capsule"); + c.dynamics = CPHYSICS_STATIC; + lua_getfield(L, -1, "dynamics"); + if(lua_tostring(L, -1)) { + if(!strcmp("dynamic", lua_tostring(L, -1))) { + c.dynamics = CPHYSICS_DYNAMIC; + } else if(!strcmp("static", lua_tostring(L, -1))) { + c.dynamics = CPHYSICS_STATIC; + } else if(!strcmp("kinematic", lua_tostring(L, -1))) { + c.dynamics = CPHYSICS_KINEMATIC; + } else { + k3Log(k3_WARN, "Invalid \"dynamics\" field %s", lua_tostring(L, -1)); + } + } + lua_pop(L, 1); + + lua_getfield(L, -1, "ghost"); + if(lua_toboolean(L, -1)) { + c.dynamics |= CPHYSICS_GHOST; + } + lua_pop(L, 1); + + // Default + c.type = CPHYSICS_SPHERE; + c.sphere.radius = 0.25; + bool found_shape = false; + + lua_getfield(L, -1, "capsule"); + if(lua_type(L, -1) == LUA_TTABLE) { + found_shape = true; + c.type = CPHYSICS_CAPSULE; setf("radius", c.capsule.radius); setf("length", c.capsule.length); - lua_pop(L, 1); - } else if(c.type == CPHYSICS_BOX) { - lua_getfield(L, -1, "box"); + } + lua_pop(L, 1); + + lua_getfield(L, -1, "box"); + if(lua_type(L, -1) == LUA_TTABLE) { + found_shape = true; + c.type = CPHYSICS_BOX; setf("w", c.box.w); setf("h", c.box.h); setf("l", c.box.l); - lua_pop(L, 1); - } else if(c.type == CPHYSICS_SPHERE) { - lua_getfield(L, -1, "sphere"); + } + lua_pop(L, 1); + + lua_getfield(L, -1, "sphere"); + if(lua_type(L, -1) == LUA_TTABLE) { + found_shape = true; + c.type = CPHYSICS_SPHERE; setf("radius", c.sphere.radius); - lua_pop(L, 1); - } else if(c.type == CPHYSICS_TRIMESH) { - lua_getfield(L, -1, "trimesh"); + } + lua_pop(L, 1); + + lua_getfield(L, -1, "trimesh"); + if(lua_type(L, -1) != LUA_TNIL) { + found_shape = true; + c.type = CPHYSICS_TRIMESH; if(lua_type(L, -1) == LUA_TSTRING) { strncpy(c.trimesh.name, lua_tostring(L, -1), sizeof(c.trimesh.name) - 1); } else { c.trimesh.cache = *(struct TrimeshData**) luaL_checkudata(L, -1, "k3physics"); } - lua_pop(L, 1); + } + lua_pop(L, 1); + + if(!found_shape) { + k3Log(k3_INFO, "Entity with invalid shape. Falling back to sphere."); } setf("speed", c.speed);