collider sensor and enabled query; color swapping functions

This commit is contained in:
John Alanbrook 2023-01-18 16:45:43 +00:00
parent 7639b7a781
commit 1a1884f61a
3 changed files with 83 additions and 42 deletions

View file

@ -18,50 +18,65 @@
cpSpace *space = NULL;
float phys2d_gravity = -50.f;
static float dbg_color[3] = {0.836f, 1.f, 0.45f};
static float trigger_color[3] = {0.278f, 0.953f, 1.f};
static float disabled_color[3] = {0.58f, 0.58f, 0.58f};
float dbg_color[3] = {0.836f, 1.f, 0.45f};
float trigger_color[3] = {0.278f, 0.953f, 1.f};
float disabled_color[3] = {0.58f, 0.58f, 0.58f};
float dynamic_color[3] = {255/255, 70/255, 46/255};
float kinematic_color[3] = {255/255, 206/255,71/255};
float static_color[3] = {0.22f, 0.271f, 1.f};
static struct color static_color = {56, 69, 255};
void color2float(struct color color, float *fcolor)
{
fcolor[0] = (float)color.r/255;
fcolor[1] = (float)color.b/255;
fcolor[2] = (float)color.g/255;
}
struct color float2color(float *fcolor)
{
struct color new;
new.r = fcolor[0]*255;
new.b = fcolor[1]*255;
new.g = fcolor[2]*255;
return new;
}
int cpshape_enabled(cpShape *c)
{
cpShapeFilter filter = cpShapeGetFilter(c);
if (filter.categories == ~CP_ALL_CATEGORIES && filter.mask == ~CP_ALL_CATEGORIES)
return 1;
return 0;
}
float *shape_outline_color(cpShape *shape)
{
switch (cpBodyGetType(cpShapeGetBody(shape))) {
case CP_BODY_TYPE_DYNAMIC:
return dynamic_color;
case CP_BODY_TYPE_KINEMATIC:
return kinematic_color;
case CP_BODY_TYPE_STATIC:
return static_color;
}
return static_color;
}
float *shape_color(cpShape *shape)
{
cpShapeFilter filter = cpShapeGetFilter(shape);
if (filter.categories == ~CP_ALL_CATEGORIES && filter.mask == ~CP_ALL_CATEGORIES)
return disabled_color;
if (cpshape_enabled(shape)) return disabled_color;
if (cpShapeGetSensor(shape)) return trigger_color;
switch (cpBodyGetType(cpShapeGetBody(shape))) {
case CP_BODY_TYPE_DYNAMIC:
return dbg_color;
break;
case CP_BODY_TYPE_KINEMATIC:
return dbg_color;
break;
case CP_BODY_TYPE_STATIC:
return dbg_color;
break;
}
return dbg_color;
}
void set_dbg_color(struct color color)
{
dbg_color[0] = (float)color.r/255;
dbg_color[1] = (float)color.b/255;
dbg_color[2] = (float)color.g/255;
}
void set_trigger_color(struct color color)
{
trigger_color[0] = (float)color.r/255;
trigger_color[1] = (float)color.b/255;
trigger_color[2] = (float)color.g/255;
}
void phys2d_init()
{
space = cpSpaceNew();
@ -495,23 +510,35 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge)
void shape_enabled(struct phys2d_shape *shape, int enabled)
{
YughInfo("Setting shape %p to enabled? %d.", shape, enabled);
if (enabled)
cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_ALL);
else
cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_NONE);
}
int shape_is_enabled(struct phys2d_shape *shape)
{
if (cpshape_enabled(shape->shape))
return 1;
return 0;
}
void shape_set_sensor(struct phys2d_shape *shape, int sensor)
{
YughInfo("Setting shape %p to sensor? %d.", shape, sensor);
cpShapeSetSensor(shape->shape, sensor);
}
int shape_get_sensor(struct phys2d_shape *shape)
{
return cpShapeGetSensor(shape->shape);
}
void phys2d_reindex_body(cpBody *body) {
cpSpaceReindexShapesForBody(space, body);
}
void register_collide(void *sym) {
}

View file

@ -8,6 +8,14 @@ extern float phys2d_gravity;
extern int physOn;
extern cpSpace *space;
extern float dbg_color[3];
extern float trigger_color[3];
extern float disabled_color[3];
extern float dynamic_color[3];
extern float kinematic_color[3];
extern float static_color[3];
struct phys2d_shape {
cpShape *shape;
int go;
@ -98,7 +106,9 @@ void register_collide(void *sym);
void phys2d_set_gravity(cpVect v);
void shape_enabled(struct phys2d_shape *shape, int enabled);
int shape_is_enabled(struct phys2d_shape *shape);
void shape_set_sensor(struct phys2d_shape *shape, int sensor);
int shape_get_sensor(struct phys2d_shape *shape);
struct color {
unsigned char r;
@ -106,8 +116,8 @@ struct color {
unsigned char b;
};
void set_dbg_color(struct color);
void set_trigger_color(struct color);
void color2float(struct color, float *fcolor);
struct color float2color(float *fcolor);
void shape_gui(struct phys2d_shape *shape);

View file

@ -131,7 +131,6 @@ duk_ret_t duk_cmd(duk_context *duk) {
//anim2d_delete(duk_to_int(duk, 1));
break;
case 13:
play_song(duk_to_string(duk, 1), duk_to_string(duk, 2));
break;
@ -145,11 +144,11 @@ duk_ret_t duk_cmd(duk_context *duk) {
break;
case 16:
set_dbg_color(duk2color(duk, 1));
color2float(duk2color(duk, 1), dbg_color);
break;
case 17:
set_trigger_color(duk2color(duk, 1));
color2float(duk2color(duk, 1), trigger_color);
break;
case 18:
@ -165,11 +164,16 @@ duk_ret_t duk_cmd(duk_context *duk) {
break;
case 21:
break;
duk_push_boolean(duk, shape_get_sensor(duk_to_pointer(duk, 1)));
return 1;
case 22:
shape_enabled(duk_to_pointer(duk, 1), duk_to_int(duk, 2));
break;
case 23:
duk_push_boolean(duk, shape_is_enabled(duk_to_pointer(duk, 1)));
return 1;
}
return 0;