2021-11-30 21:29:18 -06:00
|
|
|
#ifndef GAMEOBJECT_H
|
|
|
|
#define GAMEOBJECT_H
|
|
|
|
|
2022-01-21 11:26:22 -06:00
|
|
|
#include <chipmunk/chipmunk.h>
|
2023-05-27 07:01:17 -05:00
|
|
|
#include "quickjs/quickjs.h"
|
2023-11-03 08:31:06 -05:00
|
|
|
#include "HandmadeMath.h"
|
2023-11-30 10:47:59 -06:00
|
|
|
#include "transform.h"
|
2023-12-11 08:36:45 -06:00
|
|
|
#include "script.h"
|
2023-11-30 10:47:59 -06:00
|
|
|
|
|
|
|
#define dag_rm(p,c) do{\
|
|
|
|
for (int i = arrlen(p->children)-1; i--; i >=0) {\
|
|
|
|
if (p->children[i] == c) { \
|
|
|
|
arrdelswap(p->children,i);\
|
|
|
|
c->parent=NULL;\
|
|
|
|
break;\
|
|
|
|
}}}while(0)
|
|
|
|
|
|
|
|
#define dag_set(p,c) do{\
|
|
|
|
arrpush(p->children,c);\
|
|
|
|
if(c->parent) dag_rm(c->parent,c);\
|
|
|
|
c->parent=p;\
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
#define dag_clip(p) do{\
|
|
|
|
if (p->parent)\
|
|
|
|
dag_rm(p->parent,p);\
|
|
|
|
}while(0)
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
struct gameobject {
|
2023-05-12 13:22:05 -05:00
|
|
|
cpBodyType bodytype;
|
2023-11-30 16:24:26 -06:00
|
|
|
cpBody *body; /* NULL if this object is dead; has 2d position and rotation, relative to global 0 */
|
|
|
|
HMM_Vec3 scale; /* local */
|
2023-05-12 13:22:05 -05:00
|
|
|
int next;
|
|
|
|
float mass;
|
|
|
|
float f; /* friction */
|
|
|
|
float e; /* elasticity */
|
2023-11-20 07:49:14 -06:00
|
|
|
float timescale;
|
2023-11-06 07:05:27 -06:00
|
|
|
float maxvelocity;
|
|
|
|
float maxangularvelocity;
|
|
|
|
int gravity;
|
2023-11-20 07:49:14 -06:00
|
|
|
HMM_Vec2 cgravity;
|
2023-11-06 07:05:27 -06:00
|
|
|
float damping;
|
2023-05-12 13:22:05 -05:00
|
|
|
unsigned int layer;
|
|
|
|
cpShapeFilter filter;
|
|
|
|
struct phys_cbs cbs;
|
|
|
|
struct shape_cb *shape_cbs;
|
2023-05-27 07:01:17 -05:00
|
|
|
JSValue ref;
|
2023-11-30 10:47:59 -06:00
|
|
|
HMM_Mat4 world;
|
2023-11-09 16:44:33 -06:00
|
|
|
transform2d t; /* The local transformation of this object */
|
2023-11-21 01:07:50 -06:00
|
|
|
float drawlayer;
|
2023-12-11 08:36:45 -06:00
|
|
|
};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
typedef struct gameobject gameobject;
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
gameobject *MakeGameobject();
|
|
|
|
void gameobject_apply(gameobject *go);
|
|
|
|
void gameobject_free(gameobject *go);
|
2023-01-13 08:05:36 -06:00
|
|
|
void gameobjects_cleanup();
|
2023-02-17 13:15:56 -06:00
|
|
|
|
2023-11-09 16:44:33 -06:00
|
|
|
transform2d go2t(gameobject *go);
|
2023-11-21 17:05:06 -06:00
|
|
|
transform3d go2t3(gameobject *go);
|
2023-11-30 10:47:59 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
HMM_Vec2 go2world(gameobject *go, HMM_Vec2 pos);
|
|
|
|
HMM_Vec2 world2go(gameobject *go, HMM_Vec2 pos);
|
2023-11-11 20:01:42 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
HMM_Mat3 t_go2world(gameobject *go);
|
|
|
|
HMM_Mat3 t_world2go(gameobject *go);
|
|
|
|
HMM_Mat4 t3d_go2world(gameobject *go);
|
|
|
|
HMM_Mat4 t3d_world2go(gameobject *go);
|
2023-11-30 10:47:59 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
HMM_Vec2 go_pos(gameobject *go);
|
|
|
|
HMM_Vec2 go_worldpos(gameobject *go);
|
|
|
|
//float go_angle(gameobject *go);
|
|
|
|
float go_worldangle(gameobject *go);
|
2023-11-30 16:24:26 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
float go2angle(gameobject *go);
|
2023-11-30 10:47:59 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
gameobject *body2go(cpBody *body);
|
|
|
|
gameobject *shape2go(cpShape *shape);
|
2022-12-19 18:15:38 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go);
|
2023-02-02 17:52:15 -06:00
|
|
|
|
|
|
|
/* Tries a few methods to select a gameobject; if none is selected returns -1 */
|
2023-12-11 08:36:45 -06:00
|
|
|
gameobject *pos2gameobject(HMM_Vec2 pos);
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-12-11 08:36:45 -06:00
|
|
|
void gameobject_move(gameobject *go, HMM_Vec2 vec);
|
|
|
|
void gameobject_rotate(gameobject *go, float as);
|
|
|
|
void gameobject_setangle(gameobject *go, float angle);
|
|
|
|
void gameobject_setpos(gameobject *go, cpVect vec);
|
|
|
|
void gameobject_draw_debug(gameobject *go);
|
2023-12-12 19:35:34 -06:00
|
|
|
void gameobject_draw_debugs();
|
2021-11-30 21:29:18 -06:00
|
|
|
#endif
|