2021-11-30 21:29:18 -06:00
|
|
|
#include "gameobject.h"
|
|
|
|
|
|
|
|
#include "2dphysics.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "debugdraw.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "input.h"
|
2022-11-18 12:03:07 -06:00
|
|
|
#include "log.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "resources.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include <chipmunk/chipmunk.h>
|
|
|
|
#include <string.h>
|
2022-12-12 01:35:42 -06:00
|
|
|
#include "debugdraw.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
struct gameobject *gameobjects = NULL;
|
2023-01-13 08:05:36 -06:00
|
|
|
static int first = -1;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
const int nameBuf[MAXNAME] = {0};
|
|
|
|
const int prefabNameBuf[MAXNAME] = {0};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct gameobject *get_gameobject_from_id(int id) {
|
|
|
|
if (id < 0) return NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return &gameobjects[id];
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct gameobject *id2go(int id) {
|
|
|
|
if (id < 0) return NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return &gameobjects[id];
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int body2id(cpBody *body) {
|
2023-09-11 17:09:21 -05:00
|
|
|
return (int)cpBodyGetUserData(body);
|
2023-02-05 17:42:36 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
cpBody *id2body(int id) {
|
2023-04-28 20:55:24 -05:00
|
|
|
struct gameobject *go = id2go(id);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-04-28 20:55:24 -05:00
|
|
|
if (go)
|
2023-02-13 08:30:35 -06:00
|
|
|
return go->body;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-02-13 08:30:35 -06:00
|
|
|
return NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int shape2gameobject(cpShape *shape) {
|
2023-02-05 17:42:36 -06:00
|
|
|
struct phys2d_shape *s = cpShapeGetUserData(shape);
|
|
|
|
return s->go;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-11-07 17:24:26 -06:00
|
|
|
struct gameobject *shape2go(cpShape *shape)
|
|
|
|
{
|
|
|
|
return id2go(shape2gameobject(shape));
|
|
|
|
}
|
|
|
|
|
2023-11-09 16:44:33 -06:00
|
|
|
HMM_Vec2 go2pos(struct gameobject *go)
|
|
|
|
{
|
|
|
|
cpVect p = cpBodyGetPosition(go->body);
|
|
|
|
return (HMM_Vec2){p.x, p.y};
|
|
|
|
}
|
|
|
|
|
|
|
|
float go2angle(struct gameobject *go)
|
|
|
|
{
|
|
|
|
return cpBodyGetAngle(go->body);
|
|
|
|
}
|
|
|
|
|
|
|
|
transform2d mat2transform2d(HMM_Mat3 mat)
|
|
|
|
{
|
2023-11-15 16:42:39 -06:00
|
|
|
|
2023-11-09 16:44:33 -06:00
|
|
|
}
|
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Mat3 mt_t(transform2d t)
|
2023-11-09 16:44:33 -06:00
|
|
|
{
|
|
|
|
HMM_Mat3 p = HMM_M3D(1);
|
|
|
|
p.Columns[2].X = t.pos.X;
|
|
|
|
p.Columns[2].Y = t.pos.Y;
|
2023-11-15 16:42:39 -06:00
|
|
|
return p;
|
|
|
|
}
|
2023-11-09 16:44:33 -06:00
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Mat3 mt_s(transform2d t)
|
|
|
|
{
|
2023-11-09 16:44:33 -06:00
|
|
|
HMM_Mat3 s = HMM_M3D(1);
|
|
|
|
s.Columns[0].X = t.scale.X;
|
|
|
|
s.Columns[1].Y = t.scale.Y;
|
2023-11-15 16:42:39 -06:00
|
|
|
return s;
|
|
|
|
}
|
2023-11-09 16:44:33 -06:00
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Mat3 mt_r(transform2d t)
|
|
|
|
{
|
2023-11-09 16:44:33 -06:00
|
|
|
HMM_Mat3 r = HMM_M3D(1);
|
|
|
|
r.Columns[0] = (HMM_Vec3){cos(t.angle), sin(t.angle), 0};
|
|
|
|
r.Columns[1] = (HMM_Vec3){-sin(t.angle), cos(t.angle), 0};
|
2023-11-15 16:42:39 -06:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat3 transform2d2mat(transform2d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM3(mt_t(t), HMM_MulM3(mt_r(t), mt_s(t)));
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:05:06 -06:00
|
|
|
transform3d mat2transform3d(HMM_Mat4 mat)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
transform3d go2t3(gameobject *go)
|
|
|
|
{
|
|
|
|
transform3d t;
|
|
|
|
HMM_Vec2 p = go2pos(go);
|
|
|
|
t.pos.Y = p.Y;
|
|
|
|
t.pos.X = p.X;
|
|
|
|
t.scale = go->scale;
|
|
|
|
t.scale.Z = go->scale.X;
|
|
|
|
t.rotation = HMM_QFromAxisAngle_RH(vFWD, go2angle(go));
|
|
|
|
t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vRIGHT, -t.pos.Y/70), t.rotation);
|
|
|
|
t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vUP, t.pos.X/70), t.rotation);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_t(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_Translate(t.pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_s(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_Scale(t.scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_r(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_QToM4(t.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_st(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM4(m4_t(t), m4_s(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_rt(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM4(m4_t(t), m4_r(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 m4_rst(transform3d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM4(m4_st(t), m4_r(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat4 transform3d2mat(transform3d t)
|
|
|
|
{
|
|
|
|
return m4_rst(t);
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Mat3 mt_rst(transform2d t)
|
|
|
|
{
|
|
|
|
return transform2d2mat(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat3 mt_st(transform2d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM3(mt_t(t), mt_s(t));
|
|
|
|
}
|
2023-11-09 16:44:33 -06:00
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Mat3 mt_rt(transform2d t)
|
|
|
|
{
|
|
|
|
return HMM_MulM3(mt_t(t), mt_r(t));
|
2023-11-09 16:44:33 -06:00
|
|
|
}
|
|
|
|
|
2023-11-11 20:01:42 -06:00
|
|
|
HMM_Vec2 go2world(struct gameobject *go, HMM_Vec2 pos)
|
|
|
|
{
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Vec2 v = HMM_MulM3V3(t_go2world(go), (HMM_Vec3){pos.X, pos.Y, 1.0}).XY;
|
|
|
|
return v;
|
2023-11-11 20:01:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Vec2 world2go(struct gameobject *go, HMM_Vec2 pos)
|
|
|
|
{
|
|
|
|
return HMM_MulM3V3(t_world2go(go), (HMM_Vec3){pos.X, pos.Y, 1.0}).XY;
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:42:39 -06:00
|
|
|
HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos)
|
|
|
|
{
|
|
|
|
return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 1}).XY;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir)
|
|
|
|
{
|
|
|
|
m.Columns[2] = (HMM_Vec3){0,0,1};
|
|
|
|
return HMM_MulM3V3(m, (HMM_Vec3){dir.x, dir.y, 1}).XY;
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:07:50 -06:00
|
|
|
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos)
|
|
|
|
{
|
|
|
|
return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir)
|
|
|
|
{
|
|
|
|
m.Columns[4] = (HMM_Vec4){0,0,0,1};
|
|
|
|
return mat3_t_pos(m, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-14 09:20:09 -06:00
|
|
|
HMM_Vec2 goscale(struct gameobject *go, HMM_Vec2 pos)
|
2023-11-11 20:01:42 -06:00
|
|
|
{
|
|
|
|
return HMM_MulV2(go->scale.XY, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat3 t_go2world(struct gameobject *go)
|
|
|
|
{
|
|
|
|
return transform2d2mat(go2t(go));
|
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Mat3 t_world2go(struct gameobject *go)
|
|
|
|
{
|
|
|
|
return HMM_InvGeneralM3(transform2d2mat(go2t(go)));
|
|
|
|
}
|
|
|
|
|
2023-11-09 16:44:33 -06:00
|
|
|
|
2023-11-14 09:20:09 -06:00
|
|
|
int pos2gameobject(HMM_Vec2 pos) {
|
|
|
|
cpShape *hit = phys2d_query_pos(pos.cp);
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
if (hit) {
|
2023-02-05 17:42:36 -06:00
|
|
|
return shape2gameobject(hit);
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
2022-11-19 17:13:57 -06:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++) {
|
2023-02-10 14:31:58 -06:00
|
|
|
if (!gameobjects[i].body) continue;
|
2023-02-02 17:52:15 -06:00
|
|
|
cpVect gpos = cpBodyGetPosition(gameobjects[i].body);
|
2023-11-14 09:20:09 -06:00
|
|
|
float dist = cpvlength(cpvsub(gpos, pos.cp));
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
if (dist <= 25) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-12-19 18:15:38 -06:00
|
|
|
int id_from_gameobject(struct gameobject *go) {
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++) {
|
|
|
|
if (&gameobjects[i] == go) return i;
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return -1;
|
2022-12-19 18:15:38 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void gameobject_set_sensor(int id, int sensor) {
|
2023-02-17 13:15:56 -06:00
|
|
|
id2go(id)->sensor = sensor;
|
|
|
|
gameobject_apply(id2go(id));
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-11-09 16:44:33 -06:00
|
|
|
transform2d go2t(gameobject *go)
|
|
|
|
{
|
|
|
|
transform2d t;
|
2023-11-15 16:42:39 -06:00
|
|
|
t.pos.cp = cpBodyGetPosition(go->body);
|
2023-11-09 16:44:33 -06:00
|
|
|
t.angle = cpBodyGetAngle(go->body);
|
2023-11-11 20:01:42 -06:00
|
|
|
t.scale = go->scale.XY;
|
2023-11-15 16:42:39 -06:00
|
|
|
if (isnan(t.scale.X)) t.scale.X = 1;
|
|
|
|
if (isnan(t.scale.Y)) t.scale.Y = 1;
|
2023-11-09 16:44:33 -06:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int go2id(struct gameobject *go) {
|
2023-04-28 20:55:24 -05:00
|
|
|
return id_from_gameobject(go);
|
2023-02-19 11:16:35 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-11-07 17:24:26 -06:00
|
|
|
uint32_t go2category(struct gameobject *go)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t go2mask(struct gameobject *go)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-20 07:49:14 -06:00
|
|
|
unsigned int editor_cat = 1<<31;
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go) {
|
|
|
|
cpShapeSetFriction(shape, go->f);
|
|
|
|
cpShapeSetElasticity(shape, go->e);
|
|
|
|
cpShapeSetCollisionType(shape, go2id(go));
|
2022-08-26 11:38:35 -05:00
|
|
|
|
2023-05-24 20:45:50 -05:00
|
|
|
cpShapeFilter filter;
|
|
|
|
filter.group = go2id(go);
|
2023-11-20 07:49:14 -06:00
|
|
|
filter.categories = 1<<go->layer | editor_cat;
|
|
|
|
// filter.mask = CP_ALL_CATEGORIES;
|
|
|
|
filter.mask = category_masks[go->layer] | editor_cat;
|
|
|
|
// filter.mask = CP_ALL_CATEGORIES;
|
2023-05-24 20:45:50 -05:00
|
|
|
cpShapeSetFilter(shape, filter);
|
2023-09-26 17:07:51 -05:00
|
|
|
|
|
|
|
struct phys2d_shape *ape = cpShapeGetUserData(shape);
|
2023-09-27 17:40:04 -05:00
|
|
|
if (ape && ape->apply)
|
2023-09-26 17:07:51 -05:00
|
|
|
ape->apply(ape->data);
|
2023-02-28 09:40:53 -06:00
|
|
|
}
|
2022-08-28 22:34:33 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void go_shape_moi(cpBody *body, cpShape *shape, struct gameobject *go) {
|
|
|
|
float moment = cpBodyGetMoment(go->body);
|
|
|
|
struct phys2d_shape *s = cpShapeGetUserData(shape);
|
|
|
|
if (!s) {
|
|
|
|
cpBodySetMoment(go->body, moment + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
moment += s->moi(s->data, go->mass);
|
2023-05-27 07:01:17 -05:00
|
|
|
if (moment < 0) moment = 1;
|
2023-09-14 17:37:04 -05:00
|
|
|
cpBodySetMoment(go->body, 1);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void gameobject_apply(struct gameobject *go) {
|
|
|
|
cpBodySetType(go->body, go->bodytype);
|
|
|
|
cpBodyEachShape(go->body, go_shape_apply, go);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) {
|
|
|
|
cpBodySetMass(go->body, go->mass);
|
|
|
|
cpBodySetMoment(go->body, 0.f);
|
|
|
|
cpBodyEachShape(go->body, go_shape_moi, go);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (cpBodyGetMoment(go->body) <= 0.f)
|
|
|
|
cpBodySetMoment(go->body, 1.f);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
static void gameobject_setpickcolor(struct gameobject *go) {
|
|
|
|
/*
|
|
|
|
float r = ((go->editor.id & 0x000000FF) >> 0) / 255.f;
|
|
|
|
float g = ((go->editor.id & 0x0000FF00) >> 8) / 255.f;
|
|
|
|
float b = ((go->editor.id & 0x00FF0000) >> 16) / 255.f;
|
|
|
|
|
|
|
|
go->editor.color[0] = r;
|
|
|
|
go->editor.color[1] = g;
|
|
|
|
go->editor.color[2] = b;
|
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-11-06 07:05:27 -06:00
|
|
|
static void velocityFn(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
|
|
|
|
{
|
2023-11-09 16:44:33 -06:00
|
|
|
struct gameobject *go = id2go((int)cpBodyGetUserData(body));
|
2023-11-20 07:49:14 -06:00
|
|
|
if (!go) {
|
2023-11-06 07:05:27 -06:00
|
|
|
cpBodyUpdateVelocity(body,gravity,damping,dt);
|
2023-11-20 07:49:14 -06:00
|
|
|
return;
|
|
|
|
}
|
2023-11-06 07:05:27 -06:00
|
|
|
|
2023-11-20 07:49:14 -06:00
|
|
|
cpFloat d = isnan(go->damping) ? damping : d;
|
|
|
|
cpVect g = go->gravity ? gravity : go->cgravity.cp;
|
|
|
|
|
|
|
|
cpBodyUpdateVelocity(body,g,d,dt*go->timescale);
|
2023-11-06 07:05:27 -06:00
|
|
|
|
|
|
|
if (!isinf(go->maxvelocity))
|
|
|
|
cpBodySetVelocity(body, cpvclamp(cpBodyGetVelocity(body), go->maxvelocity));
|
2023-11-20 07:49:14 -06:00
|
|
|
|
2023-11-06 07:05:27 -06:00
|
|
|
if (!isinf(go->maxangularvelocity)) {
|
|
|
|
float av = cpBodyGetAngularVelocity(body);
|
|
|
|
if (fabs(av) > go->maxangularvelocity)
|
|
|
|
cpBodySetAngularVelocity(body, copysignf(go->maxangularvelocity, av));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int MakeGameobject() {
|
|
|
|
struct gameobject go = {
|
2023-11-11 20:01:42 -06:00
|
|
|
.scale = (HMM_Vec3){1.f,1.f,1.f},
|
2023-05-12 13:22:05 -05:00
|
|
|
.bodytype = CP_BODY_TYPE_STATIC,
|
2023-11-06 07:05:27 -06:00
|
|
|
.maxvelocity = INFINITY,
|
|
|
|
.maxangularvelocity = INFINITY,
|
2023-05-12 13:22:05 -05:00
|
|
|
.mass = 1.f,
|
|
|
|
.next = -1,
|
|
|
|
.sensor = 0,
|
2023-11-21 01:07:50 -06:00
|
|
|
.drawlayer = 0,
|
2023-05-12 13:22:05 -05:00
|
|
|
.shape_cbs = NULL,
|
2023-11-20 07:49:14 -06:00
|
|
|
.gravity = 1,
|
|
|
|
.cgravity = (HMM_Vec2){0,0},
|
|
|
|
.damping = NAN,
|
|
|
|
.timescale = 1.0,
|
2023-05-27 07:01:17 -05:00
|
|
|
.ref = JS_NULL,
|
2023-05-12 13:22:05 -05:00
|
|
|
};
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
go.cbs.begin.obj = JS_NULL;
|
|
|
|
go.cbs.separate.obj = JS_NULL;
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
|
2023-11-06 07:05:27 -06:00
|
|
|
cpBodySetVelocityUpdateFunc(go.body, velocityFn);
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int retid;
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (first < 0) {
|
|
|
|
arrput(gameobjects, go);
|
|
|
|
retid = arrlast(gameobjects).id = arrlen(gameobjects) - 1;
|
|
|
|
} else {
|
|
|
|
retid = first;
|
|
|
|
first = id2go(first)->next;
|
|
|
|
*id2go(retid) = go;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpBodySetUserData(go.body, (void *)retid);
|
|
|
|
phys2d_setup_handlers(retid);
|
|
|
|
return retid;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-01-13 08:05:36 -06:00
|
|
|
void rm_body_shapes(cpBody *body, cpShape *shape, void *data) {
|
2023-05-12 13:22:05 -05:00
|
|
|
struct phys2d_shape *s = cpShapeGetUserData(shape);
|
|
|
|
if (s->data) {
|
|
|
|
free(s->data);
|
|
|
|
s->data = NULL;
|
|
|
|
}
|
|
|
|
cpSpaceRemoveShape(space, shape);
|
|
|
|
cpShapeFree(shape);
|
2023-01-13 08:05:36 -06:00
|
|
|
}
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
int *go_toclean = NULL;
|
2022-08-15 23:46:06 -05:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
/* Free this gameobject */
|
2023-01-13 08:05:36 -06:00
|
|
|
void gameobject_clean(int id) {
|
2023-05-12 13:22:05 -05:00
|
|
|
struct gameobject *go = id2go(id);
|
|
|
|
arrfree(go->shape_cbs);
|
|
|
|
cpBodyEachShape(go->body, rm_body_shapes, NULL);
|
|
|
|
cpSpaceRemoveBody(space, go->body);
|
|
|
|
cpBodyFree(go->body);
|
|
|
|
go->body = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
/* Really more of a "mark for deletion" ... */
|
2023-05-12 13:22:05 -05:00
|
|
|
void gameobject_delete(int id) {
|
|
|
|
id2go(id)->next = first;
|
2023-10-04 08:18:09 -05:00
|
|
|
JS_FreeValue(js, id2go(id)->ref);
|
2023-05-12 13:22:05 -05:00
|
|
|
first = id;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (cpSpaceIsLocked(space))
|
|
|
|
arrpush(go_toclean, id);
|
|
|
|
else
|
|
|
|
gameobject_clean(id);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
void gameobjects_cleanup() {
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < arrlen(go_toclean); i++)
|
|
|
|
gameobject_clean(go_toclean[i]);
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
arrsetlen(go_toclean, 0);
|
|
|
|
|
|
|
|
return;
|
2022-08-28 22:34:33 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int clean = first;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
while (clean >= 0 && id2go(clean)->body) {
|
|
|
|
gameobject_clean(clean);
|
|
|
|
clean = id2go(clean)->next;
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-11-14 09:20:09 -06:00
|
|
|
void gameobject_move(struct gameobject *go, HMM_Vec2 vec) {
|
2023-05-12 13:22:05 -05:00
|
|
|
cpVect p = cpBodyGetPosition(go->body);
|
|
|
|
p.x += vec.x;
|
|
|
|
p.y += vec.y;
|
|
|
|
cpBodySetPosition(go->body, p);
|
2022-12-21 19:24:59 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
phys2d_reindex_body(go->body);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void gameobject_rotate(struct gameobject *go, float as) {
|
|
|
|
cpFloat a = cpBodyGetAngle(go->body);
|
|
|
|
a += as * deltaT;
|
|
|
|
cpBodySetAngle(go->body, a);
|
2022-12-21 19:24:59 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
phys2d_reindex_body(go->body);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-12-19 18:15:38 -06:00
|
|
|
void gameobject_setangle(struct gameobject *go, float angle) {
|
2023-05-12 13:22:05 -05:00
|
|
|
cpBodySetAngle(go->body, angle);
|
|
|
|
phys2d_reindex_body(go->body);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-02-04 11:36:24 -06:00
|
|
|
|
2022-12-26 20:57:45 -06:00
|
|
|
void gameobject_setpos(struct gameobject *go, cpVect vec) {
|
2023-05-12 13:22:05 -05:00
|
|
|
if (!go || !go->body) return;
|
|
|
|
cpBodySetPosition(go->body, vec);
|
2022-12-19 18:15:38 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
phys2d_reindex_body(go->body);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2023-01-13 08:05:36 -06:00
|
|
|
void body_draw_shapes_dbg(cpBody *body, cpShape *shape, void *data) {
|
2023-05-12 13:22:05 -05:00
|
|
|
struct phys2d_shape *s = cpShapeGetUserData(shape);
|
|
|
|
s->debugdraw(s->data);
|
2023-01-13 08:05:36 -06:00
|
|
|
}
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void gameobject_draw_debug(int go) {
|
2023-03-17 10:25:35 -05:00
|
|
|
struct gameobject *g = id2go(go);
|
|
|
|
if (!g || !g->body) return;
|
2023-01-19 10:44:29 -06:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
cpVect pos = cpBodyGetPosition(g->body);
|
2023-05-16 01:31:13 -05:00
|
|
|
struct rgba color = {
|
|
|
|
.r = 0.76*255,
|
|
|
|
.b = 0.38*255,
|
|
|
|
.g = 255,
|
|
|
|
.a = 255
|
|
|
|
};
|
2023-09-13 16:49:22 -05:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
cpBodyEachShape(g->body, body_draw_shapes_dbg, NULL);
|
|
|
|
}
|
2022-08-25 15:48:15 -05:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
void gameobject_draw_debugs() {
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++)
|
|
|
|
gameobject_draw_debug(i);
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2022-08-25 15:48:15 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
static struct {
|
|
|
|
struct gameobject go;
|
|
|
|
cpVect pos;
|
|
|
|
float angle;
|
|
|
|
} *saveobjects = NULL;
|
2022-12-22 16:58:06 -06:00
|
|
|
|
|
|
|
void gameobject_saveall() {
|
2023-05-12 13:22:05 -05:00
|
|
|
arrfree(saveobjects);
|
|
|
|
arrsetlen(saveobjects, arrlen(gameobjects));
|
|
|
|
|
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++) {
|
|
|
|
saveobjects[i].go = gameobjects[i];
|
|
|
|
saveobjects[i].pos = cpBodyGetPosition(gameobjects[i].body);
|
|
|
|
saveobjects[i].angle = cpBodyGetAngle(gameobjects[i].body);
|
|
|
|
}
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void gameobject_loadall() {
|
2023-05-12 13:22:05 -05:00
|
|
|
YughInfo("N gameobjects: %d, N saved: %d", arrlen(gameobjects), arrlen(saveobjects));
|
|
|
|
for (int i = 0; i < arrlen(saveobjects); i++) {
|
|
|
|
gameobjects[i] = saveobjects[i].go;
|
|
|
|
cpBodySetPosition(gameobjects[i].body, saveobjects[i].pos);
|
|
|
|
cpBodySetAngle(gameobjects[i].body, saveobjects[i].angle);
|
|
|
|
cpBodySetVelocity(gameobjects[i].body, cpvzero);
|
|
|
|
cpBodySetAngularVelocity(gameobjects[i].body, 0.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
arrfree(saveobjects);
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
int gameobjects_saved() {
|
2023-05-12 13:22:05 -05:00
|
|
|
return arrlen(saveobjects);
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|