Prefer strings over magic ints
This commit is contained in:
parent
49c44fe87c
commit
53a3f0af71
70
src/luaapi.c
70
src/luaapi.c
@ -193,32 +193,74 @@ static int game_addentity(lua_State *L) {
|
|||||||
c.entity = id;
|
c.entity = id;
|
||||||
|
|
||||||
seti("trigger", c.trigger);
|
seti("trigger", c.trigger);
|
||||||
seti("type", c.type);
|
|
||||||
seti("dynamics", c.dynamics);
|
|
||||||
|
|
||||||
if(c.type == CPHYSICS_CAPSULE) {
|
c.dynamics = CPHYSICS_STATIC;
|
||||||
lua_getfield(L, -1, "capsule");
|
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("radius", c.capsule.radius);
|
||||||
setf("length", c.capsule.length);
|
setf("length", c.capsule.length);
|
||||||
lua_pop(L, 1);
|
}
|
||||||
} else if(c.type == CPHYSICS_BOX) {
|
lua_pop(L, 1);
|
||||||
lua_getfield(L, -1, "box");
|
|
||||||
|
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("w", c.box.w);
|
||||||
setf("h", c.box.h);
|
setf("h", c.box.h);
|
||||||
setf("l", c.box.l);
|
setf("l", c.box.l);
|
||||||
lua_pop(L, 1);
|
}
|
||||||
} else if(c.type == CPHYSICS_SPHERE) {
|
lua_pop(L, 1);
|
||||||
lua_getfield(L, -1, "sphere");
|
|
||||||
|
lua_getfield(L, -1, "sphere");
|
||||||
|
if(lua_type(L, -1) == LUA_TTABLE) {
|
||||||
|
found_shape = true;
|
||||||
|
c.type = CPHYSICS_SPHERE;
|
||||||
setf("radius", c.sphere.radius);
|
setf("radius", c.sphere.radius);
|
||||||
lua_pop(L, 1);
|
}
|
||||||
} else if(c.type == CPHYSICS_TRIMESH) {
|
lua_pop(L, 1);
|
||||||
lua_getfield(L, -1, "trimesh");
|
|
||||||
|
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) {
|
if(lua_type(L, -1) == LUA_TSTRING) {
|
||||||
strncpy(c.trimesh.name, lua_tostring(L, -1), sizeof(c.trimesh.name) - 1);
|
strncpy(c.trimesh.name, lua_tostring(L, -1), sizeof(c.trimesh.name) - 1);
|
||||||
} else {
|
} else {
|
||||||
c.trimesh.cache = *(struct TrimeshData**) luaL_checkudata(L, -1, "k3physics");
|
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);
|
setf("speed", c.speed);
|
||||||
|
Loading…
Reference in New Issue
Block a user