Javascript collisions

This commit is contained in:
John Alanbrook 2023-01-11 22:57:34 +00:00
parent 469b955d97
commit f0ce69f957
7 changed files with 141 additions and 79 deletions

View file

@ -464,8 +464,11 @@ void phys2d_reindex_body(cpBody *body) {
cpSpaceReindexShapesForBody(space, body); cpSpaceReindexShapesForBody(space, body);
} }
void register_collide(void *sym) {
static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) { }
static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
struct gameobject *go = data; struct gameobject *go = data;
cpBody *body1; cpBody *body1;
@ -474,9 +477,19 @@ static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
struct gameobject *g2 = cpBodyGetUserData(body2); struct gameobject *g2 = cpBodyGetUserData(body2);
//script_call_sym_args(go->cbs->begin, s7_make_integer(s7, g2->editor.id)); duk_push_heapptr(duk, go->cbs.begin.fn);
//s7_call(s7, go->cbs->begin, s7_list(s7, 2, s7_make_integer(s7, g2->editor.id), cpvec2s7(cpArbiterGetNormal(arb)))); duk_push_heapptr(duk, go->cbs.begin.obj);
int obj = duk_push_object(duk);
vect2duk(cpArbiterGetNormal(arb));
duk_put_prop_literal(duk, obj, "normal");
duk_push_int(duk, g2->editor.id);
duk_put_prop_literal(duk, obj, "hit");
duk_call_method(duk, 1);
duk_pop(duk);
return 1; return 1;
} }
@ -494,18 +507,15 @@ static void s7_phys_cb_separate(cpArbiter *Arb, cpSpace *space, void *data) {
//script_call_sym(go->cbs->separate); //script_call_sym(go->cbs->separate);
} }
void phys2d_add_handler_type(int cmd, struct gameobject *go, void *cb) { void phys2d_add_handler_type(int cmd, struct gameobject *go, struct callee c) {
cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go);
if (!go->cbs)
go->cbs = malloc(sizeof(*go->cbs));
handler->userData = go; handler->userData = go;
switch (cmd) { switch (cmd) {
case 0: case 0:
//handler->beginFunc = s7_phys_cb_begin; handler->beginFunc = script_phys_cb_begin;
//go->cbs->begin = cb; go->cbs.begin = c;
break; break;
case 1: case 1:

View file

@ -2,6 +2,7 @@
#define TWODPHYSICS_H #define TWODPHYSICS_H
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
#include "script.h"
struct gameobject; struct gameobject;
@ -10,11 +11,6 @@ extern float phys2d_gravity;
extern int physOn; extern int physOn;
extern cpSpace *space; extern cpSpace *space;
struct phys_cbs {
void *begin;
void *separate;
};
struct phys2d_shape { struct phys2d_shape {
cpShape *shape; cpShape *shape;
struct gameobject *go; struct gameobject *go;
@ -98,7 +94,13 @@ void edge_gui(struct phys2d_edge *edge);
void phys2d_init(); void phys2d_init();
void phys2d_update(float deltaT); void phys2d_update(float deltaT);
void phys2d_add_handler_type(int cmd, struct gameobject *go, void *cb); struct phys_cbs {
struct callee begin;
struct callee separate;
};
void phys2d_add_handler_type(int cmd, struct gameobject *go, struct callee c);
void register_collide(void *sym);
void phys2d_set_gravity(cpVect v); void phys2d_set_gravity(cpVect v);
void shape_gui(struct phys2d_shape *shape); void shape_gui(struct phys2d_shape *shape);

View file

@ -22,8 +22,6 @@ cpVect duk2vec2(duk_context *duk, int p) {
duk_get_prop_index(duk, p, 0); duk_get_prop_index(duk, p, 0);
pos.x = duk_to_number(duk, -1); pos.x = duk_to_number(duk, -1);
duk_get_prop_index(duk, p, 1); duk_get_prop_index(duk, p, 1);
pos.y = duk_to_number(duk, -1); pos.y = duk_to_number(duk, -1);
return pos; return pos;
@ -70,7 +68,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 2: case 2:
register_gui(duk_get_heapptr(duk, 1));
break; break;
case 3: case 3:
@ -97,11 +95,61 @@ duk_ret_t duk_cmd(duk_context *duk) {
phys2d_set_gravity(duk2vec2(duk, 1)); phys2d_set_gravity(duk2vec2(duk, 1));
break; break;
case 9:
break;
case 10:
break;
case 11:
break;
} }
return 0; return 0;
} }
duk_ret_t duk_register(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
void *fn = duk_get_heapptr(duk, 1);
void *obj = duk_get_heapptr(duk, 2);
struct callee c = {fn, obj};
switch(cmd) {
case 0:
register_update(c);
break;
case 1:
register_physics(c);
break;
case 2:
register_gui(c);
break;
}
return 0;
}
duk_ret_t duk_register_collide(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
void *fn = duk_get_heapptr(duk, 1);
void *obj = duk_get_heapptr(duk, 2);
int go = duk_get_int(duk, 3);
struct callee c = {fn, obj};
YughInfo("Registering ...");
phys2d_add_handler_type(0, get_gameobject_from_id(go), c);
return 0;
}
duk_ret_t duk_sys_cmd(duk_context *duk) { duk_ret_t duk_sys_cmd(duk_context *duk) {
int cmd = duk_to_int(duk, 0); int cmd = duk_to_int(duk, 0);
@ -151,24 +199,6 @@ duk_ret_t duk_sys_cmd(duk_context *duk) {
return 0; return 0;
} }
duk_ret_t duk_register(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
void *obj = duk_get_heapptr(duk, 1);
void *fn = duk_get_heapptr(duk, 2);
switch (cmd) {
case 0:
register_update(obj, fn);
break;
case 1:
register_physics(obj, fn);
break;
}
return 0;
}
duk_ret_t duk_make_gameobject(duk_context *duk) { duk_ret_t duk_make_gameobject(duk_context *duk) {
int g = MakeGameobject(); int g = MakeGameobject();
struct gameobject *go = get_gameobject_from_id(g); struct gameobject *go = get_gameobject_from_id(g);
@ -251,6 +281,8 @@ duk_ret_t duk_q_body(duk_context *duk) {
int q = duk_to_int(duk, 0); int q = duk_to_int(duk, 0);
struct gameobject *go = get_gameobject_from_id(duk_to_int(duk, 1)); struct gameobject *go = get_gameobject_from_id(duk_to_int(duk, 1));
if (!go) return 0;
switch(q) { switch(q) {
case 0: case 0:
duk_push_int(duk, cpBodyGetType(go->body)); duk_push_int(duk, cpBodyGetType(go->body));
@ -263,6 +295,18 @@ duk_ret_t duk_q_body(duk_context *duk) {
case 2: case 2:
duk_push_number(duk, cpBodyGetAngle(go->body)); duk_push_number(duk, cpBodyGetAngle(go->body));
return 1; return 1;
case 3:
vect2duk(cpBodyGetVelocity(go->body));
return 1;
case 4:
duk_push_number(duk, cpBodyGetAngularVelocity(go->body));
return 1;
case 5:
duk_push_number(duk, cpBodyGetMass(go->body));
return 1;
} }
return 0; return 0;
@ -285,7 +329,7 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
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 = duk2vec2(duk, 1); double radius = duk_to_number(duk, 1);
cpVect offset = duk2vec2(duk, 2); cpVect offset = duk2vec2(duk, 2);
struct phys2d_circle *circle = Make2DCircle(get_gameobject_from_id(go)); struct phys2d_circle *circle = Make2DCircle(get_gameobject_from_id(go));
@ -345,7 +389,6 @@ void ffi_load()
DUK_FUNC(make_gameobject, 7); DUK_FUNC(make_gameobject, 7);
DUK_FUNC(set_body, 3); DUK_FUNC(set_body, 3);
DUK_FUNC(q_body, 2); DUK_FUNC(q_body, 2);
DUK_FUNC(register, 3);
DUK_FUNC(sys_cmd, 1); DUK_FUNC(sys_cmd, 1);
DUK_FUNC(win_make, 3); DUK_FUNC(win_make, 3);
@ -353,6 +396,8 @@ void ffi_load()
DUK_FUNC(make_box2d, 3); DUK_FUNC(make_box2d, 3);
DUK_FUNC(make_circle2d, 3); DUK_FUNC(make_circle2d, 3);
DUK_FUNC(cmd, 2); DUK_FUNC(cmd, 2);
DUK_FUNC(register, 3);
DUK_FUNC(register_collide, 4);
DUK_FUNC(gui_text, 3); DUK_FUNC(gui_text, 3);
DUK_FUNC(gui_img, 2); DUK_FUNC(gui_img, 2);

View file

@ -21,7 +21,8 @@ const int nameBuf[MAXNAME] = { 0 };
const int prefabNameBuf[MAXNAME] = { 0 }; const int prefabNameBuf[MAXNAME] = { 0 };
struct gameobject *get_gameobject_from_id(int id) struct gameobject *get_gameobject_from_id(int id)
{ { if (id < 0) return NULL;
return &gameobjects[id]; return &gameobjects[id];
} }
@ -54,7 +55,7 @@ static void gameobject_setpickcolor(struct gameobject *go)
int MakeGameobject() int MakeGameobject()
{ {
if (gameobjects == NULL) arrsetcap(gameobjects, 5000); if (gameobjects == NULL) arrsetcap(gameobjects, 50);
struct gameobject go = { struct gameobject go = {
.editor.id = arrlen(gameobjects), .editor.id = arrlen(gameobjects),

View file

@ -24,10 +24,6 @@ struct editor {
char rootPrefabName[MAXNAME]; char rootPrefabName[MAXNAME];
}; };
struct go_temp {
struct phys_cbs phys_cbs;
};
struct gameobject { struct gameobject {
cpBodyType bodytype; cpBodyType bodytype;
float scale; float scale;
@ -38,7 +34,7 @@ struct gameobject {
int flipy; int flipy;
cpBody *body; cpBody *body;
struct component *components; struct component *components;
struct phys_cbs *cbs; struct phys_cbs cbs;
struct editor editor; struct editor editor;
}; };

View file

@ -111,50 +111,52 @@ void script_call_sym_args(void *sym, void *args)
//s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7))); //s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
} }
struct obupdate {
void *obj;
void *sym;
};
struct obupdate *updates = NULL;
struct obupdate *physics = NULL;
void **guis = NULL;
void register_update(void *obj, void *sym) { struct callee *updates = NULL;
struct obupdate ob = {obj, sym}; struct callee *physics = NULL;
arrput(updates, ob); struct callee *guis = NULL;
void register_update(struct callee c) {
arrput(updates, c);
}
void register_gui(struct callee c) {
arrput(guis, c);
}
void register_physics(struct callee c) {
arrput(physics, c);
}
void call_callee(struct callee c) {
duk_push_heapptr(duk, c.fn);
duk_push_heapptr(duk, c.obj);
duk_call_method(duk, 0);
duk_pop(duk);
}
void callee_dbl(struct callee c, double d) {
duk_push_heapptr(duk, c.fn);
duk_push_heapptr(duk, c.obj);
duk_push_number(duk, d);
duk_call_method(duk, 1);
duk_pop(duk);
} }
void call_updates(double dt) { void call_updates(double dt) {
for (int i = 0; i < arrlen(updates); i++) { for (int i = 0; i < arrlen(updates); i++) {
duk_push_heapptr(duk, updates[i].obj); callee_dbl(updates[i], dt);
duk_push_heapptr(duk, updates[i].sym);
duk_push_number(duk, dt);
duk_call_prop(duk, -3, 1);
duk_pop_2(duk);
} }
} }
void register_gui(void *sym) {
arrput(guis, sym);
}
void call_gui() { void call_gui() {
for (int i = 0; i < arrlen(guis); i++) for (int i = 0; i < arrlen(guis); i++)
script_call_sym(guis[i]); call_callee(guis[i]);
}
void register_physics(void *obj, void *sym) {
struct obupdate ob = {obj, sym};
arrput(physics, ob);
} }
void call_physics(double dt) { void call_physics(double dt) {
for (int i = 0; i < arrlen(physics); i++) { for (int i = 0; i < arrlen(physics); i++) {
duk_push_heapptr(duk, physics[i].obj); callee_dbl(updates[i], dt);
duk_push_heapptr(duk, physics[i].sym);
duk_push_number(duk, dt);
duk_call_prop(duk, -3, 1);
duk_pop_2(duk);
} }
} }

View file

@ -6,6 +6,12 @@
extern duk_context *duk; extern duk_context *duk;
struct callee {
void *fn;
void *obj;
};
void script_init(); void script_init();
void script_run(const char *script); void script_run(const char *script);
int script_dofile(const char *file); int script_dofile(const char *file);
@ -18,13 +24,13 @@ void script_call_sym_args(void *sym, void *args);
int script_has_sym(void *sym); int script_has_sym(void *sym);
void script_eval_w_env(const char *s, void *env); void script_eval_w_env(const char *s, void *env);
void register_update(void *obj, void *sym); void register_update(struct callee c);
void call_updates(double dt); void call_updates(double dt);
void register_gui(void *sym); void register_gui(struct callee c);
void call_gui(); void call_gui();
void register_physics(void *obj, void *sym); void register_physics(struct callee c);
void call_physics(double dt); void call_physics(double dt);
duk_idx_t vec2duk(cpVect v); duk_idx_t vec2duk(cpVect v);