Gameobject level sensor setting

This commit is contained in:
John Alanbrook 2023-02-17 19:15:56 +00:00
parent afa9f963ef
commit a401dbb9d4
5 changed files with 32 additions and 38 deletions

View file

@ -488,6 +488,7 @@ void phys2d_applyedge(struct phys2d_edge *edge)
cpShapeSetCollisionType(edge->shapes[i], edge->shape.go);
cpShapeSetFriction(edge->shapes[i], id2go(edge->shape.go)->f);
cpShapeSetElasticity(edge->shapes[i], id2go(edge->shape.go)->e);
cpShapeSetSensor(edge->shapes[i], id2go(edge->shape.go)->sensor);
}
cpSpaceReindexShapesForBody(space, id2go(edge->shape.go)->body);
@ -542,11 +543,22 @@ int shape_is_enabled(struct phys2d_shape *shape)
void shape_set_sensor(struct phys2d_shape *shape, int sensor)
{
cpShapeSetSensor(shape->shape, sensor);
if (!shape->shape) {
struct phys2d_edge *edge = shape->data;
for (int i = 0; i < arrlen(edge->shapes); i++) {
cpShapeSetSensor(edge->shapes[i], sensor);
YughInfo("Setting shape %d sensor to %d", i, sensor);
}
} else
cpShapeSetSensor(shape->shape, sensor);
}
int shape_get_sensor(struct phys2d_shape *shape)
{
if (!shape->shape) {
return cpShapeGetSensor(((struct phys2d_edge *)(shape->data))->shapes[0]);
}
return cpShapeGetSensor(shape->shape);
}
@ -590,8 +602,6 @@ static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
int g2 = cpBodyGetUserData(body2);
struct gameobject *go = id2go(g1);
for (int i = 0; i < arrlen(go->shape_cbs); i++) {
if (go->shape_cbs[i].shape != shape1) continue;
duk_call_phys_cb(arb, go->shape_cbs[i].cbs.begin, g2);

View file

@ -54,7 +54,7 @@ struct phys2d_box {
struct phys2d_shape shape;
};
/* An edge with no volume. Cannot collide with each other. Join to make levels. */
/* An edge with no volume. Cannot collide with each other. Join to make levels. Static only. */
struct phys2d_edge {
cpVect *points;
float thickness;

View file

@ -709,6 +709,10 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 68:
opengl_rendermode(WIREFRAME);
break;
case 69:
gameobject_set_sensor(duk_to_int(duk, 1), duk_to_boolean(duk,2));
break;
}
return 0;

View file

@ -80,10 +80,19 @@ int id_from_gameobject(struct gameobject *go) {
return -1;
}
void gameobject_set_sensor(int id, int sensor)
{
id2go(id)->sensor = sensor;
gameobject_apply(id2go(id));
}
void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go)
{
cpShapeSetFriction(shape, go->f);
cpShapeSetElasticity(shape, go->e);
cpShapeSetSensor(shape, go->sensor);
if (go->sensor)
YughInfo("Enabled a sensor ...");
// cpShapeSetFilter(shape, go->filter);
}
@ -116,7 +125,8 @@ int MakeGameobject()
.scale = 1.f,
.bodytype = CP_BODY_TYPE_STATIC,
.mass = 1.f,
.next = -1
.next = -1,
.sensor = 0,
};
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
@ -239,36 +249,6 @@ void gameobject_saveprefab(struct gameobject *go)
*/
}
void gameobject_syncprefabs(char *revertPath)
{
/*
struct gameobject **go = objects;
int i = 0;
while(i != nobjects) {
if ((*go)->editor.curPrefabPath && !strcmp((*go)->editor.curPrefabPath, revertPath)) { ; }//objectRevertPrefab(go); //TODO: revertprefab
}
*/
}
void gameobject_revertprefab(struct gameobject *go)
{
}
void toggleprefab(struct gameobject *go)
{
/*
go->editor.prefabSync = !go->editor.prefabSync;
if (go->editor.prefabSync) {
strcpy(go->editor.prefabName, go->editor.rootPrefabName);
gameobject_revertprefab(go); //TODO: object revert prefab
} else {
go->editor.prefabName[0] = '\0';
}
*/
}
void gameobject_move(struct gameobject *go, cpVect vec)
{
cpVect p = cpBodyGetPosition(go->body);

View file

@ -21,6 +21,7 @@ struct gameobject {
float e; /* elasticity */
int flipx; /* 1 or -1 */
int flipy;
int sensor;
cpShapeFilter filter;
cpBody *body; /* NULL if this object is dead */
int id;
@ -34,7 +35,8 @@ int MakeGameobject();
void gameobject_apply(struct gameobject *go);
void gameobject_delete(int id);
void gameobjects_cleanup();
void toggleprefab(struct gameobject *go);
void gameobject_set_sensor(int id, int sensor);
struct gameobject *get_gameobject_from_id(int id);
struct gameobject *id2go(int id);
@ -47,8 +49,6 @@ void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go);
void gameobject_save(struct gameobject *go, FILE * file);
void gameobject_saveprefab(struct gameobject *go);
void gameobject_syncprefabs(char *revertPath);
void gameobject_revertprefab(struct gameobject *go);
/* Tries a few methods to select a gameobject; if none is selected returns -1 */
int pos2gameobject(cpVect pos);