Sprite and collider enabling

This commit is contained in:
John Alanbrook 2023-01-17 19:04:08 +00:00
parent db0a099509
commit 16ad36d4ce
5 changed files with 50 additions and 3 deletions

View file

@ -20,6 +20,7 @@ float phys2d_gravity = -50.f;
static float dbg_color[3] = {0.836f, 1.f, 0.45f}; static float dbg_color[3] = {0.836f, 1.f, 0.45f};
static float trigger_color[3] = {0.278f, 0.953f, 1.f}; static float trigger_color[3] = {0.278f, 0.953f, 1.f};
static float disabled_color[3] = {0.58f, 0.58f, 0.58f};
static struct color static_color = {56, 69, 255}; static struct color static_color = {56, 69, 255};
void set_dbg_color(struct color color) void set_dbg_color(struct color color)
@ -472,6 +473,16 @@ cpShape *id2shape(int id)
return NULL; return NULL;
} }
void shape_enable(struct phys2d_shape *shape)
{
cpSpaceAddShape(space, shape->shape);
}
void shape_disable(struct phys2d_shape *shape)
{
cpSpaceRemoveShape(space, shape->shape);
}
void phys2d_reindex_body(cpBody *body) { void phys2d_reindex_body(cpBody *body) {
cpSpaceReindexShapesForBody(space, body); cpSpaceReindexShapesForBody(space, body);
} }

View file

@ -97,6 +97,9 @@ void phys2d_add_handler_type(int cmd, int go, struct callee c);
void register_collide(void *sym); void register_collide(void *sym);
void phys2d_set_gravity(cpVect v); void phys2d_set_gravity(cpVect v);
void shape_enable(struct phys2d_shape *shape);
void shape_disable(struct phys2d_shape *shape);
cpShape *id2shape(int id); cpShape *id2shape(int id);
struct color { struct color {

View file

@ -163,6 +163,20 @@ duk_ret_t duk_cmd(duk_context *duk) {
duk_push_undefined(duk); duk_push_undefined(duk);
return 1; return 1;
case 20:
sprite_enabled(duk_to_int(duk, 1), duk_to_boolean(duk, 2));
break;
case 21:
duk_push_boolean(duk, id2sprite(duk_to_int(duk, 1))->enabled);
return 1;
case 22:
return 0;
case 23:
return 0;
} }
return 0; return 0;
@ -418,6 +432,17 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
phys2d_applybox(box); phys2d_applybox(box);
duk_push_pointer(duk, box);
return 1;
}
duk_ret_t duk_box2d_cmd(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
struct phys2d_box *box = duk_to_pointer(duk, 1);
YughInfo("Issuing command %d with box %p.", cmd, box);
return 0; return 0;
} }
@ -492,6 +517,7 @@ void ffi_load()
DUK_FUNC(make_sprite, 3); DUK_FUNC(make_sprite, 3);
DUK_FUNC(make_anim2d, 4); DUK_FUNC(make_anim2d, 4);
DUK_FUNC(make_box2d, 3); DUK_FUNC(make_box2d, 3);
DUK_FUNC(box2d_cmd, DUK_VARARGS);
DUK_FUNC(make_circle2d, 3); DUK_FUNC(make_circle2d, 3);
DUK_FUNC(cmd, DUK_VARARGS); DUK_FUNC(cmd, DUK_VARARGS);
DUK_FUNC(register, 3); DUK_FUNC(register, 3);

View file

@ -26,7 +26,8 @@ int make_sprite(int go)
.size = {1.f, 1.f}, .size = {1.f, 1.f},
.tex = texture_loadfromfile("ph.png"), .tex = texture_loadfromfile("ph.png"),
.go = go, .go = go,
.next = -1 }; .next = -1,
.enabled = 1 };
int ret; int ret;
@ -51,6 +52,11 @@ void sprite_delete(int id)
first = id; first = id;
} }
void sprite_enabled(int id, int e)
{
sprites[id].enabled = e;
}
struct sprite *id2sprite(int id) { struct sprite *id2sprite(int id) {
return &sprites[id]; return &sprites[id];
} }
@ -78,9 +84,8 @@ void sprite_io(struct sprite *sprite, FILE *f, int read)
void sprite_draw_all() void sprite_draw_all()
{ {
//shader_use(spriteShader);
for (int i = 0; i < arrlen(sprites); i++) { for (int i = 0; i < arrlen(sprites); i++) {
if (sprites[i].go >= 0) sprite_draw(&sprites[i]); if (sprites[i].go >= 0 && sprites[i].enabled) sprite_draw(&sprites[i]);
} }
} }

View file

@ -19,12 +19,14 @@ struct sprite {
struct anim2d anim; struct anim2d anim;
struct Texture *tex; struct Texture *tex;
int next; int next;
int enabled;
}; };
int make_sprite(int go); int make_sprite(int go);
struct sprite *id2sprite(int id); struct sprite *id2sprite(int id);
void sprite_delete(int id); void sprite_delete(int id);
void sprite_enabled(int id, int e);
void sprite_io(struct sprite *sprite, FILE *f, int read); void sprite_io(struct sprite *sprite, FILE *f, int read);
void sprite_loadtex(struct sprite *sprite, const char *path); void sprite_loadtex(struct sprite *sprite, const char *path);
void sprite_settex(struct sprite *sprite, struct Texture *tex); void sprite_settex(struct sprite *sprite, struct Texture *tex);