Javascript collisions
This commit is contained in:
parent
469b955d97
commit
f0ce69f957
|
@ -464,8 +464,11 @@ void phys2d_reindex_body(cpBody *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;
|
||||
|
||||
cpBody *body1;
|
||||
|
@ -474,9 +477,19 @@ static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
|
|||
|
||||
struct gameobject *g2 = cpBodyGetUserData(body2);
|
||||
|
||||
//script_call_sym_args(go->cbs->begin, s7_make_integer(s7, g2->editor.id));
|
||||
//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.fn);
|
||||
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;
|
||||
}
|
||||
|
@ -494,18 +507,15 @@ static void s7_phys_cb_separate(cpArbiter *Arb, cpSpace *space, void *data) {
|
|||
//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);
|
||||
|
||||
if (!go->cbs)
|
||||
go->cbs = malloc(sizeof(*go->cbs));
|
||||
|
||||
handler->userData = go;
|
||||
|
||||
switch (cmd) {
|
||||
case 0:
|
||||
//handler->beginFunc = s7_phys_cb_begin;
|
||||
//go->cbs->begin = cb;
|
||||
handler->beginFunc = script_phys_cb_begin;
|
||||
go->cbs.begin = c;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define TWODPHYSICS_H
|
||||
|
||||
#include <chipmunk/chipmunk.h>
|
||||
#include "script.h"
|
||||
|
||||
struct gameobject;
|
||||
|
||||
|
@ -10,11 +11,6 @@ extern float phys2d_gravity;
|
|||
extern int physOn;
|
||||
extern cpSpace *space;
|
||||
|
||||
struct phys_cbs {
|
||||
void *begin;
|
||||
void *separate;
|
||||
};
|
||||
|
||||
struct phys2d_shape {
|
||||
cpShape *shape;
|
||||
struct gameobject *go;
|
||||
|
@ -98,7 +94,13 @@ void edge_gui(struct phys2d_edge *edge);
|
|||
void phys2d_init();
|
||||
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 shape_gui(struct phys2d_shape *shape);
|
||||
|
|
|
@ -22,8 +22,6 @@ cpVect duk2vec2(duk_context *duk, int p) {
|
|||
duk_get_prop_index(duk, p, 0);
|
||||
pos.x = duk_to_number(duk, -1);
|
||||
duk_get_prop_index(duk, p, 1);
|
||||
|
||||
|
||||
pos.y = duk_to_number(duk, -1);
|
||||
|
||||
return pos;
|
||||
|
@ -70,7 +68,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
|||
break;
|
||||
|
||||
case 2:
|
||||
register_gui(duk_get_heapptr(duk, 1));
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
|
@ -97,11 +95,61 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
|||
phys2d_set_gravity(duk2vec2(duk, 1));
|
||||
break;
|
||||
|
||||
case 9:
|
||||
|
||||
break;
|
||||
|
||||
case 10:
|
||||
|
||||
break;
|
||||
|
||||
case 11:
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
int cmd = duk_to_int(duk, 0);
|
||||
|
||||
|
@ -151,24 +199,6 @@ duk_ret_t duk_sys_cmd(duk_context *duk) {
|
|||
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) {
|
||||
int g = MakeGameobject();
|
||||
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);
|
||||
struct gameobject *go = get_gameobject_from_id(duk_to_int(duk, 1));
|
||||
|
||||
if (!go) return 0;
|
||||
|
||||
switch(q) {
|
||||
case 0:
|
||||
duk_push_int(duk, cpBodyGetType(go->body));
|
||||
|
@ -263,6 +295,18 @@ duk_ret_t duk_q_body(duk_context *duk) {
|
|||
case 2:
|
||||
duk_push_number(duk, cpBodyGetAngle(go->body));
|
||||
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;
|
||||
|
@ -284,8 +328,8 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
|
|||
}
|
||||
|
||||
duk_ret_t duk_make_circle2d(duk_context *duk) {
|
||||
int go = duk_to_int(duk, 0);
|
||||
double radius = duk2vec2(duk, 1);
|
||||
int go = duk_to_int(duk, 0);
|
||||
double radius = duk_to_number(duk, 1);
|
||||
cpVect offset = duk2vec2(duk, 2);
|
||||
|
||||
struct phys2d_circle *circle = Make2DCircle(get_gameobject_from_id(go));
|
||||
|
@ -345,7 +389,6 @@ void ffi_load()
|
|||
DUK_FUNC(make_gameobject, 7);
|
||||
DUK_FUNC(set_body, 3);
|
||||
DUK_FUNC(q_body, 2);
|
||||
DUK_FUNC(register, 3);
|
||||
DUK_FUNC(sys_cmd, 1);
|
||||
DUK_FUNC(win_make, 3);
|
||||
|
||||
|
@ -353,6 +396,8 @@ void ffi_load()
|
|||
DUK_FUNC(make_box2d, 3);
|
||||
DUK_FUNC(make_circle2d, 3);
|
||||
DUK_FUNC(cmd, 2);
|
||||
DUK_FUNC(register, 3);
|
||||
DUK_FUNC(register_collide, 4);
|
||||
|
||||
DUK_FUNC(gui_text, 3);
|
||||
DUK_FUNC(gui_img, 2);
|
||||
|
|
|
@ -21,7 +21,8 @@ const int nameBuf[MAXNAME] = { 0 };
|
|||
const int prefabNameBuf[MAXNAME] = { 0 };
|
||||
|
||||
struct gameobject *get_gameobject_from_id(int id)
|
||||
{
|
||||
{ if (id < 0) return NULL;
|
||||
|
||||
return &gameobjects[id];
|
||||
}
|
||||
|
||||
|
@ -54,7 +55,7 @@ static void gameobject_setpickcolor(struct gameobject *go)
|
|||
|
||||
int MakeGameobject()
|
||||
{
|
||||
if (gameobjects == NULL) arrsetcap(gameobjects, 5000);
|
||||
if (gameobjects == NULL) arrsetcap(gameobjects, 50);
|
||||
|
||||
struct gameobject go = {
|
||||
.editor.id = arrlen(gameobjects),
|
||||
|
|
|
@ -24,10 +24,6 @@ struct editor {
|
|||
char rootPrefabName[MAXNAME];
|
||||
};
|
||||
|
||||
struct go_temp {
|
||||
struct phys_cbs phys_cbs;
|
||||
};
|
||||
|
||||
struct gameobject {
|
||||
cpBodyType bodytype;
|
||||
float scale;
|
||||
|
@ -38,7 +34,7 @@ struct gameobject {
|
|||
int flipy;
|
||||
cpBody *body;
|
||||
struct component *components;
|
||||
struct phys_cbs *cbs;
|
||||
struct phys_cbs cbs;
|
||||
struct editor editor;
|
||||
};
|
||||
|
||||
|
|
|
@ -111,50 +111,52 @@ void script_call_sym_args(void *sym, void *args)
|
|||
//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 obupdate ob = {obj, sym};
|
||||
arrput(updates, ob);
|
||||
struct callee *updates = NULL;
|
||||
struct callee *physics = NULL;
|
||||
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) {
|
||||
for (int i = 0; i < arrlen(updates); i++) {
|
||||
duk_push_heapptr(duk, updates[i].obj);
|
||||
duk_push_heapptr(duk, updates[i].sym);
|
||||
duk_push_number(duk, dt);
|
||||
duk_call_prop(duk, -3, 1);
|
||||
duk_pop_2(duk);
|
||||
callee_dbl(updates[i], dt);
|
||||
}
|
||||
}
|
||||
|
||||
void register_gui(void *sym) {
|
||||
arrput(guis, sym);
|
||||
}
|
||||
|
||||
void call_gui() {
|
||||
for (int i = 0; i < arrlen(guis); i++)
|
||||
script_call_sym(guis[i]);
|
||||
}
|
||||
|
||||
void register_physics(void *obj, void *sym) {
|
||||
struct obupdate ob = {obj, sym};
|
||||
arrput(physics, ob);
|
||||
call_callee(guis[i]);
|
||||
}
|
||||
|
||||
void call_physics(double dt) {
|
||||
for (int i = 0; i < arrlen(physics); i++) {
|
||||
duk_push_heapptr(duk, physics[i].obj);
|
||||
duk_push_heapptr(duk, physics[i].sym);
|
||||
duk_push_number(duk, dt);
|
||||
duk_call_prop(duk, -3, 1);
|
||||
duk_pop_2(duk);
|
||||
callee_dbl(updates[i], dt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,12 @@
|
|||
|
||||
extern duk_context *duk;
|
||||
|
||||
|
||||
struct callee {
|
||||
void *fn;
|
||||
void *obj;
|
||||
};
|
||||
|
||||
void script_init();
|
||||
void script_run(const char *script);
|
||||
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);
|
||||
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 register_gui(void *sym);
|
||||
void register_gui(struct callee c);
|
||||
void call_gui();
|
||||
|
||||
void register_physics(void *obj, void *sym);
|
||||
void register_physics(struct callee c);
|
||||
void call_physics(double dt);
|
||||
|
||||
duk_idx_t vec2duk(cpVect v);
|
||||
|
|
Loading…
Reference in a new issue