Shape:getBody; Shape:setBody;

This commit is contained in:
bjorn 2017-05-16 12:23:13 -06:00
parent 43ac25a61d
commit c7515a1f7e
3 changed files with 45 additions and 0 deletions

33
src/api/types/shapes.c Normal file
View File

@ -0,0 +1,33 @@
#include "api/lovr.h"
#include "physics/physics.h"
int l_lovrShapeGetType(lua_State* L) {
Shape* shape = luax_checktypeof(L, 1, Shape);
luax_pushenum(L, &ShapeTypes, lovrShapeGetType(shape));
return 1;
}
int l_lovrShapeGetBody(lua_State* L) {
Shape* shape = luax_checktypeof(L, 1, Shape);
Body* body = lovrShapeGetBody(shape);
if (body) {
luax_pushtype(L, Body, body);
} else {
lua_pushnil(L);
}
return 1;
}
int l_lovrShapeSetBody(lua_State* L) {
Shape* shape = luax_checktypeof(L, 1, Shape);
if (lua_isnoneornil(L, 2)) {
lovrShapeSetBody(shape, NULL);
} else {
Body* body = luax_checktype(L, 2, Body);
lovrShapeSetBody(shape, body);
}
return 0;
}

View File

@ -269,6 +269,15 @@ ShapeType lovrShapeGetType(Shape* shape) {
return shape->type;
}
Body* lovrShapeGetBody(Shape* shape) {
return shape->body;
}
void lovrShapeSetBody(Shape* shape, Body* body) {
shape->body = body;
dGeomSetBody(shape->id, body ? body->id : 0);
}
void lovrShapeDestroy(const Ref* ref) {
Shape* shape = containerof(ref, Shape);
dGeomDestroy(shape->id);

View File

@ -23,6 +23,7 @@ typedef struct {
Ref ref;
ShapeType type;
dGeomID id;
Body* body;
} Shape;
void lovrPhysicsInit();
@ -74,4 +75,6 @@ void lovrBodySetUserData(Body* body, void* data);
World* lovrBodyGetWorld(Body* body);
ShapeType lovrShapeGetType(Shape* shape);
Body* lovrShapeGetBody(Shape* shape);
void lovrShapeSetBody(Shape* shape, Body* body);
void lovrShapeDestroy(const Ref* ref);