2021-11-30 21:29:18 -06:00
|
|
|
#include "gameobject.h"
|
|
|
|
|
|
|
|
#include "shader.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "registry.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"
|
2022-11-18 12:03:07 -06:00
|
|
|
#include "log.h"
|
2022-12-12 10:48:21 -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;
|
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)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-08-26 09:19:17 -05:00
|
|
|
return &gameobjects[id];
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-12-19 18:15:38 -06:00
|
|
|
int id_from_gameobject(struct gameobject *go) {
|
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++) {
|
|
|
|
if (&gameobjects[i] == i) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
static void gameobject_setpickcolor(struct gameobject *go)
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
int MakeGameobject()
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-12-22 16:58:06 -06:00
|
|
|
if (gameobjects == NULL) arrsetcap(gameobjects, 100);
|
|
|
|
|
2022-11-18 12:03:07 -06:00
|
|
|
YughInfo("Making new gameobject");
|
2022-11-19 17:13:57 -06:00
|
|
|
struct gameobject go = {
|
2022-08-26 09:19:17 -05:00
|
|
|
.editor.id = arrlen(gameobjects),
|
|
|
|
.transform.scale = 1.f,
|
|
|
|
.scale = 1.f,
|
|
|
|
.bodytype = CP_BODY_TYPE_STATIC,
|
|
|
|
.mass = 1.f
|
|
|
|
};
|
|
|
|
|
|
|
|
gameobject_setpickcolor(&go);
|
|
|
|
strncpy(go.editor.mname, "New object", MAXNAME);
|
2022-12-21 19:24:59 -06:00
|
|
|
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
arrput(gameobjects, go);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
return arrlen(gameobjects)-1;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_addcomponent(struct gameobject *go, struct component *c)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-08-26 09:19:17 -05:00
|
|
|
arrput(go->components, *c);
|
|
|
|
struct component *newc = &arrlast(go->components);
|
2021-11-30 21:29:18 -06:00
|
|
|
newc->go = go;
|
|
|
|
newc->data = newc->make(newc->go);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject_delete(int id)
|
|
|
|
{
|
2022-11-19 17:13:57 -06:00
|
|
|
YughInfo("Deleting gameobject with id %d.", id);
|
|
|
|
struct gameobject *go = &gameobjects[id];
|
2022-08-26 09:19:17 -05:00
|
|
|
for (int i = 0; i < arrlen(go->components); i++) {
|
2022-11-19 17:13:57 -06:00
|
|
|
go->components[i].delete(go->components[i].data);
|
|
|
|
arrdel(go->components, i);
|
2022-08-26 09:19:17 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
|
|
|
|
arrdelswap(gameobjects, id);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_delcomponent(struct gameobject *go, int n)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-08-28 22:34:33 -05:00
|
|
|
go->components[n].delete(go->components[n].data);
|
2022-08-26 09:19:17 -05:00
|
|
|
arrdel(go->components, n);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void setup_model_transform(struct mTransform *t, struct shader *s, float scale)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
mfloat_t modelT[16] = { 0.f };
|
|
|
|
mfloat_t matbuff[16] = { 0.f };
|
|
|
|
memcpy(modelT, UNITMAT4, sizeof(modelT));
|
|
|
|
mat4_translate_vec3(modelT, t->position);
|
2022-08-15 23:46:06 -05:00
|
|
|
mat4_multiply(modelT, modelT, mat4_rotation_quat(matbuff, t->rotation));
|
2021-11-30 21:29:18 -06:00
|
|
|
mat4_scale_vec3f(modelT, scale);
|
|
|
|
shader_setmat4(s, "model", modelT);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_save(struct gameobject *go, FILE * file)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
fwrite(go, sizeof(*go), 1, file);
|
|
|
|
|
2022-11-18 12:03:07 -06:00
|
|
|
YughInfo("Number of components is %d.", arrlen(go->components));
|
2022-08-26 11:38:35 -05:00
|
|
|
|
|
|
|
int n = arrlen(go->components);
|
|
|
|
fwrite(&n, sizeof(n), 1, file);
|
2022-08-28 22:34:33 -05:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
fwrite(&go->components[i].id, sizeof(int), 1, file);
|
|
|
|
|
|
|
|
if (go->components[i].io == NULL)
|
|
|
|
fwrite(go->components[i].data, go->components[i].datasize, 1, file);
|
|
|
|
else
|
|
|
|
go->components[i].io(go->components[i].data, file, 0);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:15:38 -06:00
|
|
|
int gameobject_makefromprefab(char *path)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-08-15 23:46:06 -05:00
|
|
|
FILE *fprefab = fopen(path, "rb");
|
2021-11-30 21:29:18 -06:00
|
|
|
if (fprefab == NULL) {
|
2022-12-16 13:54:08 -06:00
|
|
|
YughError("Could not find prefab %s.", path);
|
2021-11-30 21:29:18 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
struct gameobject *new = get_gameobject_from_id(MakeGameobject());
|
2021-11-30 21:29:18 -06:00
|
|
|
fread(new, sizeof(*new), 1, fprefab);
|
2022-08-26 09:19:17 -05:00
|
|
|
new->components = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
gameobject_init(new, fprefab);
|
|
|
|
|
|
|
|
fclose(fprefab);
|
2022-12-19 18:15:38 -06:00
|
|
|
|
|
|
|
return arrlen(gameobjects)-1;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_init(struct gameobject *go, FILE * fprefab)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f));
|
2022-12-22 03:50:40 -06:00
|
|
|
cpBodySetType(go->body, go->bodytype);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
int comp_n;
|
|
|
|
fread(&comp_n, sizeof(int), 1, fprefab);
|
2022-11-19 17:13:57 -06:00
|
|
|
arrfree(go->components);
|
2022-08-26 09:19:17 -05:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (int i = 0; i < comp_n; i++) {
|
2022-11-19 17:13:57 -06:00
|
|
|
fread(&n, sizeof(int), 1, fprefab);
|
|
|
|
arrput(go->components, components[n]);
|
|
|
|
struct component *newc = &arrlast(go->components);
|
|
|
|
newc->go = go;
|
|
|
|
newc->data = newc->make(newc->go);
|
2022-08-28 22:34:33 -05:00
|
|
|
|
|
|
|
if (newc->io == NULL)
|
|
|
|
fread(newc->data, newc->datasize, 1, fprefab);
|
|
|
|
else
|
|
|
|
newc->io(newc->data, fprefab, 1);
|
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
newc->init(newc->data, go);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-12-22 03:50:40 -06:00
|
|
|
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_saveprefab(struct gameobject *go)
|
2022-08-15 23:46:06 -05:00
|
|
|
{
|
|
|
|
char prefabfname[60] = { '\0' };
|
|
|
|
strncat(prefabfname, go->editor.prefabName, MAXNAME);
|
|
|
|
strncat(prefabfname, EXT_PREFAB, 10);
|
|
|
|
FILE *pfile = fopen(prefabfname, "wb+");
|
|
|
|
gameobject_save(go, pfile);
|
|
|
|
fclose(pfile);
|
|
|
|
|
|
|
|
findPrefabs();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
void gameobject_syncprefabs(char *revertPath)
|
|
|
|
{
|
|
|
|
/*
|
2022-11-19 17:13:57 -06:00
|
|
|
struct gameobject **go = objects;
|
2021-11-30 21:29:18 -06:00
|
|
|
int i = 0;
|
|
|
|
while(i != nobjects) {
|
|
|
|
if ((*go)->editor.curPrefabPath && !strcmp((*go)->editor.curPrefabPath, revertPath)) { ; }//objectRevertPrefab(go); //TODO: revertprefab
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_revertprefab(struct gameobject *go)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void toggleprefab(struct gameobject *go)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
go->editor.prefabSync = !go->editor.prefabSync;
|
|
|
|
|
|
|
|
if (go->editor.prefabSync) {
|
|
|
|
strcpy(go->editor.prefabName, go->editor.rootPrefabName);
|
|
|
|
gameobject_revertprefab(go); //TODO: object revert prefab
|
|
|
|
} else {
|
|
|
|
go->editor.prefabName[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void gameobject_move(struct gameobject *go, float xs, float ys)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
cpVect p = cpBodyGetPosition(go->body);
|
2022-12-22 16:58:06 -06:00
|
|
|
p.x += xs;
|
|
|
|
p.y += ys;
|
2021-11-30 21:29:18 -06:00
|
|
|
cpBodySetPosition(go->body, p);
|
2022-12-21 19:24:59 -06:00
|
|
|
|
|
|
|
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);
|
2022-12-21 19:24:59 -06:00
|
|
|
|
|
|
|
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);
|
2022-12-21 19:24:59 -06:00
|
|
|
|
|
|
|
phys2d_reindex_body(go->body);
|
2022-12-19 18:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject_setpos(struct gameobject *go, float x, float y) {
|
|
|
|
if (!go || !go->body) return;
|
|
|
|
cpVect p;
|
|
|
|
p.x = x;
|
|
|
|
p.y = y;
|
|
|
|
cpBodySetPosition(go->body, p);
|
|
|
|
|
2022-12-21 19:24:59 -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
|
|
|
{
|
|
|
|
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-08-17 00:01:51 -05:00
|
|
|
nk_property_float2(ctx, "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;
|
|
|
|
nk_property_float(ctx, "Angle", -1000.f, &modtry, 1000.f, 0.5f, 0.5f);
|
|
|
|
modtry -= modtry2;
|
|
|
|
cpBodySetAngle(go->body, mtry + (modtry * DEG2RADS));
|
|
|
|
|
2022-08-17 00:01:51 -05:00
|
|
|
nk_property_float(ctx, "Scale", 0.f, &go->scale, 1000.f, 0.01f, go->scale * 0.01f);
|
2022-08-12 14:03:56 -05:00
|
|
|
|
|
|
|
nk_layout_row_dynamic(ctx, 25, 3);
|
2022-12-14 13:01:42 -06:00
|
|
|
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) {
|
|
|
|
nk_property_float(ctx, "Mass", 0.01f, &go->mass, 1000.f, 0.01f, 0.01f);
|
|
|
|
cpBodySetMass(go->body, go->mass);
|
|
|
|
}
|
|
|
|
|
|
|
|
nk_property_float(ctx, "Friction", 0.f, &go->f, 10.f, 0.01f, 0.01f);
|
|
|
|
nk_property_float(ctx, "Elasticity", 0.f, &go->e, 2.f, 0.01f, 0.01f);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (c->draw_debug)
|
|
|
|
c->draw_debug(c->data);
|
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
|
|
|
|
nuke_nel(5);
|
|
|
|
if (nk_button_label(ctx, "Del")) n = i;
|
|
|
|
if (nk_tree_push_id(ctx, NK_TREE_NODE, c->name, NK_MINIMIZED, i)) {
|
2022-08-12 14:03:56 -05:00
|
|
|
|
|
|
|
c->draw_gui(c->data);
|
|
|
|
|
|
|
|
nk_tree_pop(ctx);
|
|
|
|
}
|
2022-08-25 15:48:15 -05:00
|
|
|
|
|
|
|
|
2022-08-12 14:03:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n >= 0)
|
|
|
|
gameobject_delcomponent(go, n);
|
|
|
|
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
2022-12-21 19:24:59 -06:00
|
|
|
|
|
|
|
void gameobject_draw_debugs() {
|
|
|
|
for (int i = 0; i < arrlen(gameobjects); i++) {
|
|
|
|
for (int j = 0; j < arrlen(gameobjects[i].components); j++) {
|
|
|
|
struct component *c = &gameobjects[i].components[j];
|
|
|
|
|
|
|
|
if (c->draw_debug) c->draw_debug(c->data);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|