prefab syncing

This commit is contained in:
John Alanbrook 2023-01-20 00:30:23 +00:00
parent 5ecab600e0
commit 9303fb63cf
5 changed files with 52 additions and 13 deletions

View file

@ -119,8 +119,7 @@ struct phys2d_circle *Make2DCircle(int go)
struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle)); struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle));
new->radius = 10.f; new->radius = 10.f;
new->offset[0] = 0.f; new->offset = cpvzero;
new->offset[1] = 0.f;
new->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(id2go(go)->body, new->radius, cpvzero)); new->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(id2go(go)->body, new->radius, cpvzero));
new->shape.debugdraw = phys2d_dbgdrawcircle; new->shape.debugdraw = phys2d_dbgdrawcircle;
@ -137,7 +136,7 @@ void phys2d_circledel(struct phys2d_circle *c)
void circle_gui(struct phys2d_circle *circle) void circle_gui(struct phys2d_circle *circle)
{ {
nuke_property_float("Radius", 1.f, &circle->radius, 10000.f, 1.f, 1.f); nuke_property_float("Radius", 1.f, &circle->radius, 10000.f, 1.f, 1.f);
nuke_property_float2("Offset", 0.f, circle->offset, 1.f, 0.01f, 0.01f); //nuke_property_float2("Offset", 0.f, circle->offset, 1.f, 0.01f, 0.01f);
phys2d_applycircle(circle); phys2d_applycircle(circle);
} }
@ -366,7 +365,7 @@ void phys2d_applycircle(struct phys2d_circle *circle)
float radius = circle->radius * go->scale; float radius = circle->radius * go->scale;
float s = go->scale; float s = go->scale;
cpVect offset = { circle->offset[0] * s, circle->offset[1] * s }; cpVect offset = { circle->offset.x * s, circle->offset.y * s };
cpCircleShapeSetRadius(circle->shape.shape, radius); cpCircleShapeSetRadius(circle->shape.shape, radius);
cpCircleShapeSetOffset(circle->shape.shape, offset); cpCircleShapeSetOffset(circle->shape.shape, offset);

View file

@ -25,7 +25,7 @@ struct phys2d_shape {
struct phys2d_circle { struct phys2d_circle {
float radius; float radius;
float offset[2]; cpVect offset;
struct phys2d_shape shape; struct phys2d_shape shape;
}; };

View file

@ -45,6 +45,12 @@ cpVect duk2vec2(duk_context *duk, int p) {
return pos; return pos;
} }
void vec2float(cpVect v, float *f)
{
f[0] = v.x;
f[1] = v.y;
}
duk_ret_t duk_gui_text(duk_context *duk) { duk_ret_t duk_gui_text(duk_context *duk) {
const char *s = duk_to_string(duk, 0); const char *s = duk_to_string(duk, 0);
cpVect pos = duk2vec2(duk, 1); cpVect pos = duk2vec2(duk, 1);
@ -80,7 +86,7 @@ duk_ret_t duk_nuke(duk_context *duk)
case 2: case 2:
editnum = duk_to_number(duk, 2); editnum = duk_to_number(duk, 2);
nuke_property_float(duk_to_string(duk, 1), 0.f, &editnum, 100.f, 0.01f, 0.01f); nuke_property_float(duk_to_string(duk, 1), duk_to_number(duk, 3), &editnum, duk_to_number(duk, 4), duk_to_number(duk, 5), duk_to_number(duk, 5));
duk_push_number(duk, editnum); duk_push_number(duk, editnum);
return 1; return 1;
@ -261,6 +267,15 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 35: case 35:
((struct timer*)duk_to_pointer(duk, 1))->repeat = duk_to_boolean(duk, 2); ((struct timer*)duk_to_pointer(duk, 1))->repeat = duk_to_boolean(duk, 2);
return 0; return 0;
case 36:
id2go(duk_to_int(duk, 1))->scale = duk_to_number(duk, 2);
return 0;
case 37:
if (!id2sprite(duk_to_int(duk, 1))) return 0;
vec2float(duk2vec2(duk, 2), id2sprite(duk_to_int(duk, 1))->pos);
break;
} }
return 0; return 0;
@ -515,7 +530,11 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
phys2d_applybox(box); phys2d_applybox(box);
int idx = duk_push_object(duk);
duk_push_pointer(duk, &box->shape); duk_push_pointer(duk, &box->shape);
duk_put_prop_string(duk, idx, "id");
duk_push_pointer(duk, box);
duk_put_prop_string(duk, idx, "shape");
return 1; return 1;
} }
@ -524,19 +543,33 @@ duk_ret_t duk_cmd_box2d(duk_context *duk)
{ {
int cmd = duk_to_int(duk, 0); int cmd = duk_to_int(duk, 0);
struct phys2d_box *box = duk_to_pointer(duk, 1); struct phys2d_box *box = duk_to_pointer(duk, 1);
cpVect arg = duk2vec2(duk, 2);
if (!box) return 0;
switch(cmd) {
case 0:
box->w = arg.x;
box->h = arg.y;
break;
case 1:
box->offset[0] = arg.x;
box->offset[1] = arg.y;
break;
}
phys2d_applybox(box);
return 0; return 0;
} }
duk_ret_t duk_make_circle2d(duk_context *duk) { duk_ret_t duk_make_circle2d(duk_context *duk) {
int go = duk_to_int(duk, 0); int go = duk_to_int(duk, 0);
double radius = duk_to_number(duk, 1); double radius = duk_to_number(duk, 1);
cpVect offset = duk2vec2(duk, 2);
struct phys2d_circle *circle = Make2DCircle(go); struct phys2d_circle *circle = Make2DCircle(go);
circle->radius = radius; circle->radius = radius;
circle->offset[0] = offset.x; circle->offset = duk2vec2(duk, 2);
circle->offset[1] = offset.y;
phys2d_applycircle(circle); phys2d_applycircle(circle);
@ -559,12 +592,14 @@ duk_ret_t duk_cmd_circle2d(duk_context *duk)
switch(cmd) { switch(cmd) {
case 0: case 0:
circle->radius = duk_to_number(duk, 2); circle->radius = duk_to_number(duk, 2);
phys2d_applycircle(circle); break;
return 0;
case 1:
circle->offset = duk2vec2(duk, 2);
break;
} }
phys2d_applycircle(circle);
return 0; return 0;
} }

View file

@ -124,7 +124,10 @@ void add_zoom(float val) { zoom = val; }
void openglRender(struct window *window) void openglRender(struct window *window)
{ {
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//////////// 2D projection //////////// 2D projection
mfloat_t projection[16] = { 0.f }; mfloat_t projection[16] = { 0.f };
cpVect pos = cam_pos(); cpVect pos = cam_pos();

View file

@ -58,6 +58,7 @@ void sprite_enabled(int id, int e)
} }
struct sprite *id2sprite(int id) { struct sprite *id2sprite(int id) {
if (id < 0) return NULL;
return &sprites[id]; return &sprites[id];
} }
@ -180,6 +181,7 @@ void sprite_draw(struct sprite *sprite)
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame) void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame)
{ {
if (!sprite) return;
sprite->tex = anim->tex; sprite->tex = anim->tex;
sprite->frame = &anim->st_frames[frame]; sprite->frame = &anim->st_frames[frame];
} }