prosperon/source/engine/gameobject.c

383 lines
8.7 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "gameobject.h"
#include "shader.h"
#include "sprite.h"
#include "2dphysics.h"
#include "script.h"
#include "input.h"
#include <string.h>
2022-01-21 11:26:22 -06:00
#include <chipmunk/chipmunk.h>
2022-02-06 10:14:57 -06:00
#include "resources.h"
2022-08-12 14:03:56 -05:00
#include "nuke.h"
#include "log.h"
#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;
static int first = -1;
2021-11-30 21:29:18 -06:00
const int nameBuf[MAXNAME] = { 0 };
const int prefabNameBuf[MAXNAME] = { 0 };
2022-11-19 17:13:57 -06:00
struct gameobject *get_gameobject_from_id(int id)
2023-01-11 16:57:34 -06:00
{ if (id < 0) return NULL;
2022-08-26 09:19:17 -05:00
return &gameobjects[id];
2021-11-30 21:29:18 -06:00
}
2023-02-05 17:42:36 -06:00
struct gameobject *id2go(int id)
{
2023-01-12 17:41:54 -06:00
if (id < 0) return NULL;
return &gameobjects[id];
}
2023-02-05 17:42:36 -06:00
int body2id(cpBody *body)
{
2023-04-19 15:16:35 -05:00
return (int)cpBodyGetUserData(body);
2023-02-05 17:42:36 -06:00
}
2023-02-13 08:30:35 -06:00
cpBody *id2body(int id)
{
2023-04-28 20:55:24 -05:00
struct gameobject *go = id2go(id);
2023-02-13 08:30:35 -06:00
2023-04-28 20:55:24 -05:00
if (go)
2023-02-13 08:30:35 -06:00
return go->body;
return NULL;
}
2023-02-05 17:42:36 -06:00
int shape2gameobject(cpShape *shape)
{
struct phys2d_shape *s = cpShapeGetUserData(shape);
return s->go;
}
int pos2gameobject(cpVect pos)
{
cpShape *hit = phys2d_query_pos(pos);
if (hit) {
2023-02-05 17:42:36 -06:00
return shape2gameobject(hit);
}
for (int i = 0; i < arrlen(gameobjects); i++) {
2023-02-10 14:31:58 -06:00
if (!gameobjects[i].body) continue;
cpVect gpos = cpBodyGetPosition(gameobjects[i].body);
float dist = cpvlength(cpvsub(gpos, pos));
if (dist <= 25) return i;
}
return -1;
}
2022-12-19 18:15:38 -06:00
int id_from_gameobject(struct gameobject *go) {
for (int i = 0; i < arrlen(gameobjects); i++) {
2023-01-03 17:13:31 -06:00
if (&gameobjects[i] == go) return i;
2022-12-19 18:15:38 -06:00
}
return -1;
}
2023-02-17 13:15:56 -06:00
void gameobject_set_sensor(int id, int sensor)
{
id2go(id)->sensor = sensor;
gameobject_apply(id2go(id));
}
int go2id(struct gameobject *go)
{
2023-04-28 20:55:24 -05:00
return id_from_gameobject(go);
}
void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go)
{
cpShapeSetFriction(shape, go->f);
cpShapeSetElasticity(shape, go->e);
cpShapeSetCollisionType(shape, go2id(go));
2023-04-21 16:57:30 -05:00
2023-02-28 09:40:53 -06:00
2023-04-21 16:57:30 -05:00
/* cpShapeFilter filter;
2023-03-10 13:13:48 -06:00
filter.group = go2id(go);
filter.categories = 1<<go->layer;
filter.mask = category_masks[go->layer];
cpShapeSetFilter(shape, filter);
2023-04-21 16:57:30 -05:00
*/
2023-02-28 09:40:53 -06: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-02-28 09:40:53 -06:00
cpBodySetMoment(go->body, moment);
}
2023-01-04 18:09:42 -06:00
void gameobject_apply(struct gameobject *go)
{
cpBodySetType(go->body, go->bodytype);
cpBodyEachShape(go->body, go_shape_apply, go);
2023-02-28 09:40:53 -06:00
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) {
2023-01-04 18:09:42 -06:00
cpBodySetMass(go->body, go->mass);
2023-02-28 09:40:53 -06:00
cpBodySetMoment(go->body, 0.f);
cpBodyEachShape(go->body, go_shape_moi, go);
2023-03-17 10:25:35 -05:00
if (cpBodyGetMoment(go->body) <= 0.f)
2023-02-28 09:40:53 -06:00
cpBodySetMoment(go->body, 1.f);
return;
2023-02-27 08:50:36 -06:00
}
2023-01-04 18:09:42 -06:00
}
2022-11-19 17:13:57 -06:00
static void gameobject_setpickcolor(struct gameobject *go)
2021-11-30 21:29:18 -06:00
{
2023-01-12 17:41:54 -06:00
/*
2021-11-30 21:29:18 -06:00
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;
2023-01-12 17:41:54 -06:00
*/
2021-11-30 21:29:18 -06:00
}
2022-12-22 16:58:06 -06:00
int MakeGameobject()
2021-11-30 21:29:18 -06:00
{
2022-11-19 17:13:57 -06:00
struct gameobject go = {
2022-08-26 09:19:17 -05:00
.scale = 1.f,
.bodytype = CP_BODY_TYPE_STATIC,
.mass = 1.f,
2023-02-17 13:15:56 -06:00
.next = -1,
.sensor = 0,
2023-03-17 10:25:35 -05:00
.shape_cbs = NULL,
2022-08-26 09:19:17 -05:00
};
2023-04-18 14:49:17 -05:00
go.cbs.begin.obj = JS_NULL;
go.cbs.separate.obj = JS_NULL;
2023-03-17 10:25:35 -05:00
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
2023-01-12 17:41:54 -06:00
int retid;
2021-11-30 21:29:18 -06:00
if (first<0) {
2023-01-12 17:41:54 -06:00
arrput(gameobjects, go);
retid = arrlast(gameobjects).id = arrlen(gameobjects)-1;
2023-01-12 17:41:54 -06:00
} else {
retid = first;
first = id2go(first)->next;
*id2go(retid) = go;
2022-08-26 09:19:17 -05:00
}
2023-02-06 16:41:47 -06:00
2023-04-19 15:16:35 -05:00
cpBodySetUserData(go.body, (void*)retid);
2023-03-10 13:13:48 -06:00
phys2d_setup_handlers(retid);
2023-01-12 17:41:54 -06:00
return retid;
2021-11-30 21:29:18 -06:00
}
void rm_body_shapes(cpBody *body, cpShape *shape, void *data) {
struct phys2d_shape *s = cpShapeGetUserData(shape);
if (s->data) {
free(s->data);
s->data = NULL;
}
cpSpaceRemoveShape(space, shape);
2023-01-13 13:07:44 -06:00
cpShapeFree(shape);
}
int *go_toclean = NULL;
/* Free this gameobject */
void gameobject_clean(int id) {
2023-01-12 17:41:54 -06:00
struct gameobject *go = id2go(id);
arrfree(go->shape_cbs);
cpBodyEachShape(go->body, rm_body_shapes, NULL);
2023-01-12 17:41:54 -06:00
cpSpaceRemoveBody(space, go->body);
2023-01-13 13:07:44 -06:00
cpBodyFree(go->body);
go->body = NULL;
}
2023-01-12 17:41:54 -06:00
/* Really more of a "mark for deletion" ... */
void gameobject_delete(int id)
2021-11-30 21:29:18 -06:00
{
id2go(id)->next = first;
first = id;
2022-08-28 22:34:33 -05:00
2023-03-10 13:13:48 -06:00
if (cpSpaceIsLocked(space))
arrpush(go_toclean, id);
else
gameobject_clean(id);
2021-11-30 21:29:18 -06:00
}
void gameobjects_cleanup() {
2023-03-10 13:13:48 -06:00
for (int i = 0; i < arrlen(go_toclean); i++)
gameobject_clean(go_toclean[i]);
2022-08-26 09:19:17 -05:00
arrsetlen(go_toclean, 0);
return;
2022-08-28 22:34:33 -05:00
int clean = first;
2022-08-28 22:34:33 -05:00
while (clean >= 0 && id2go(clean)->body) {
gameobject_clean(clean);
clean = id2go(clean)->next;
2021-11-30 21:29:18 -06:00
}
}
2022-12-26 20:57:45 -06:00
void gameobject_move(struct gameobject *go, cpVect vec)
2021-11-30 21:29:18 -06:00
{
cpVect p = cpBodyGetPosition(go->body);
2022-12-26 20:57:45 -06:00
p.x += vec.x;
p.y += vec.y;
2021-11-30 21:29:18 -06:00
cpBodySetPosition(go->body, p);
phys2d_reindex_body(go->body);
2021-11-30 21:29:18 -06:00
}
2022-11-19 17:13:57 -06:00
void gameobject_rotate(struct gameobject *go, float as)
2021-11-30 21:29:18 -06:00
{
cpFloat a = cpBodyGetAngle(go->body);
a += as * deltaT;
cpBodySetAngle(go->body, a);
phys2d_reindex_body(go->body);
2021-11-30 21:29:18 -06:00
}
2022-02-04 11:36:24 -06:00
2022-12-19 18:15:38 -06:00
void gameobject_setangle(struct gameobject *go, float angle) {
cpBodySetAngle(go->body, angle);
phys2d_reindex_body(go->body);
2022-12-19 18:15:38 -06:00
}
2022-12-26 20:57:45 -06:00
void gameobject_setpos(struct gameobject *go, cpVect vec) {
2022-12-19 18:15:38 -06:00
if (!go || !go->body) return;
2022-12-26 20:57:45 -06:00
cpBodySetPosition(go->body, vec);
2022-12-19 18:15:38 -06:00
phys2d_reindex_body(go->body);
2022-02-06 10:14:57 -06:00
}
2022-08-12 14:03:56 -05:00
2022-11-19 17:13:57 -06:00
void object_gui(struct gameobject *go)
2022-08-12 14:03:56 -05:00
{
2023-01-12 17:41:54 -06:00
/*
2022-08-12 14:03:56 -05:00
float temp_pos[2];
temp_pos[0] = cpBodyGetPosition(go->body).x;
temp_pos[1] = cpBodyGetPosition(go->body).y;
draw_point(temp_pos[0], temp_pos[1], 3);
2022-12-23 13:48:29 -06:00
nuke_property_float2("Position", -1000000.f, temp_pos, 1000000.f, 1.f, 0.5f);
2022-08-12 14:03:56 -05:00
cpVect tvect = { temp_pos[0], temp_pos[1] };
cpBodySetPosition(go->body, tvect);
float mtry = cpBodyGetAngle(go->body);
float modtry = fmodf(mtry * RAD2DEGS, 360.f);
2022-08-17 00:01:51 -05:00
if (modtry < 0.f)
modtry += 360.f;
2022-08-12 14:03:56 -05:00
float modtry2 = modtry;
2022-12-23 13:48:29 -06:00
nuke_property_float("Angle", -1000.f, &modtry, 1000.f, 0.5f, 0.5f);
2022-08-12 14:03:56 -05:00
modtry -= modtry2;
cpBodySetAngle(go->body, mtry + (modtry * DEG2RADS));
2022-12-23 13:48:29 -06:00
nuke_property_float("Scale", 0.f, &go->scale, 1000.f, 0.01f, go->scale * 0.01f);
2022-08-12 14:03:56 -05:00
2022-12-23 13:48:29 -06:00
nuke_nel(3);
nuke_radio_btn("Static", &go->bodytype, CP_BODY_TYPE_STATIC);
nuke_radio_btn("Dynamic", &go->bodytype, CP_BODY_TYPE_DYNAMIC);
nuke_radio_btn("Kinematic", &go->bodytype, CP_BODY_TYPE_KINEMATIC);
2022-08-12 14:03:56 -05:00
cpBodySetType(go->body, go->bodytype);
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) {
2022-12-23 13:48:29 -06:00
nuke_property_float("Mass", 0.01f, &go->mass, 1000.f, 0.01f, 0.01f);
2022-08-12 14:03:56 -05:00
cpBodySetMass(go->body, go->mass);
}
2022-12-23 13:48:29 -06:00
nuke_property_float("Friction", 0.f, &go->f, 10.f, 0.01f, 0.01f);
nuke_property_float("Elasticity", 0.f, &go->e, 2.f, 0.01f, 0.01f);
2022-08-12 14:03:56 -05:00
int n = -1;
2022-08-25 15:48:15 -05:00
2022-08-26 09:19:17 -05:00
for (int i = 0; i < arrlen(go->components); i++) {
struct component *c = &go->components[i];
2022-08-12 14:03:56 -05:00
2023-01-03 17:13:31 -06:00
comp_draw_debug(c);
2022-08-25 15:48:15 -05:00
2022-12-23 13:48:29 -06:00
nuke_nel(5);
if (nuke_btn("Del")) n = i;
2022-08-12 14:03:56 -05:00
2023-01-03 17:13:31 -06:00
if (nuke_push_tree_id(c->ref->name, i)) {
comp_draw_gui(c);
2022-12-23 13:48:29 -06:00
nuke_tree_pop();
2022-08-12 14:03:56 -05:00
}
2022-08-25 15:48:15 -05:00
2022-08-12 14:03:56 -05:00
}
if (n >= 0)
gameobject_delcomponent(go, n);
2023-01-12 17:41:54 -06:00
*/
}
void body_draw_shapes_dbg(cpBody *body, cpShape *shape, void *data) {
struct phys2d_shape *s = cpShapeGetUserData(shape);
s->debugdraw(s->data);
}
2023-03-17 10:25:35 -05:00
void gameobject_draw_debug(int go)
{
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);
float color[3] = {0.76f, 0.38f, 1.f};
draw_point(pos.x, pos.y, 3.f, color);
cpBodyEachShape(g->body, body_draw_shapes_dbg, NULL);
}
2023-03-17 10:25:35 -05:00
void gameobject_draw_debugs() {
for (int i = 0; i < arrlen(gameobjects); i++)
gameobject_draw_debug(i);
2022-12-22 16:58:06 -06:00
}
static struct {struct gameobject go; cpVect pos; float angle; } *saveobjects = NULL;
void gameobject_saveall() {
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);
}
}
void gameobject_loadall() {
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);
}
int gameobjects_saved() {
return arrlen(saveobjects);
}