prosperon/source/engine/gameobject.h

131 lines
3.4 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
2023-05-12 13:22:05 -05:00
#include "2dphysics.h"
2021-11-30 21:29:18 -06:00
#include "config.h"
2022-01-21 11:26:22 -06:00
#include <chipmunk/chipmunk.h>
2023-05-12 13:22:05 -05:00
#include <stdbool.h>
#include <stdio.h>
2023-05-27 07:01:17 -05:00
#include "quickjs/quickjs.h"
2023-11-03 08:31:06 -05:00
#include "HandmadeMath.h"
2021-11-30 21:29:18 -06:00
2022-11-19 17:13:57 -06:00
struct shader;
struct sprite;
2021-11-30 21:29:18 -06:00
struct component;
2023-11-09 16:44:33 -06:00
typedef struct transform2d {
HMM_Vec2 pos;
HMM_Vec2 scale;
double angle;
} transform2d;
transform2d mat2transform2d(HMM_Mat3 mat);
HMM_Mat3 transform2d2mat(transform2d t);
2023-11-21 17:05:06 -06:00
typedef struct transform3d {
HMM_Vec3 pos;
HMM_Vec3 scale;
HMM_Quat rotation;
} transform3d;
transform3d mat2transform3d(HMM_Mat4 mat);
HMM_Mat4 transform3d2mat(transform3d t);
HMM_Mat4 m4_t(transform3d t);
HMM_Mat4 m4_s(transform3d t);
HMM_Mat4 m4_r(transform3d t);
HMM_Mat4 m4_rt(transform3d t);
HMM_Mat4 m4_st(transform3d t);
HMM_Mat4 m4_rst(transform3d t);
2023-11-09 16:44:33 -06:00
typedef struct gameobject {
2023-05-12 13:22:05 -05:00
cpBodyType bodytype;
int next;
2023-11-11 20:01:42 -06:00
HMM_Vec3 scale;
2023-05-12 13:22:05 -05:00
float mass;
float f; /* friction */
float e; /* elasticity */
2023-11-20 07:49:14 -06:00
float timescale;
float maxvelocity;
float maxangularvelocity;
int gravity;
2023-11-20 07:49:14 -06:00
HMM_Vec2 cgravity;
float damping;
2023-05-12 13:22:05 -05:00
int sensor;
unsigned int layer;
cpShapeFilter filter;
cpBody *body; /* NULL if this object is dead */
int id;
struct phys_cbs cbs;
struct shape_cb *shape_cbs;
2023-05-27 07:01:17 -05:00
JSValue ref;
2023-11-09 16:44:33 -06:00
HMM_Mat3 transform;
struct gameobject *master;
transform2d t; /* The local transformation of this object */
float drawlayer;
2023-11-09 16:44:33 -06:00
} gameobject;
2021-11-30 21:29:18 -06:00
2022-11-19 17:13:57 -06:00
extern struct gameobject *gameobjects;
2022-08-26 09:19:17 -05:00
2022-12-22 16:58:06 -06:00
int MakeGameobject();
2023-01-04 18:09:42 -06:00
void gameobject_apply(struct gameobject *go);
2021-11-30 21:29:18 -06:00
void gameobject_delete(int id);
void gameobjects_cleanup();
2023-02-17 13:15:56 -06:00
void gameobject_set_sensor(int id, int sensor);
2022-12-19 18:15:38 -06:00
2023-11-09 16:44:33 -06:00
HMM_Vec2 go2pos(struct gameobject *go);
float go2angle(struct gameobject *go);
transform2d go2t(gameobject *go);
2023-11-21 17:05:06 -06:00
transform3d go2t3(gameobject *go);
2023-11-11 20:01:42 -06:00
HMM_Vec2 go2world(struct gameobject *go, HMM_Vec2 pos);
HMM_Vec2 world2go(struct gameobject *go, HMM_Vec2 pos);
HMM_Mat3 t_go2world(struct gameobject *go);
HMM_Mat3 t_world2go(struct gameobject *go);
HMM_Vec2 goscale(struct gameobject *go, HMM_Vec2 pos);
HMM_Vec2 gotpos(struct gameobject *go, HMM_Vec2 pos);
2023-11-09 16:44:33 -06:00
2023-11-15 16:42:39 -06:00
HMM_Mat3 mt_rst(transform2d t);
HMM_Mat3 mt_st(transform2d t);
HMM_Mat3 mt_rt(transform2d t);
/* Transform a position via the matrix */
HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos);
/* Transform a direction via the matrix - does not take into account translation of matrix */
HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir);
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos);
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir);
2022-11-19 17:13:57 -06:00
struct gameobject *get_gameobject_from_id(int id);
2023-01-12 17:41:54 -06:00
struct gameobject *id2go(int id);
2022-12-19 18:15:38 -06:00
int id_from_gameobject(struct gameobject *go);
int go2id(struct gameobject *go);
2023-02-05 17:42:36 -06:00
int body2id(cpBody *body);
2023-02-13 08:30:35 -06:00
cpBody *id2body(int id);
int shape2gameobject(cpShape *shape);
2023-11-07 17:24:26 -06:00
struct gameobject *shape2go(cpShape *shape);
uint32_t go2category(struct gameobject *go);
uint32_t go2mask(struct gameobject *go);
2022-12-19 18:15:38 -06:00
void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go);
/* Tries a few methods to select a gameobject; if none is selected returns -1 */
2023-11-14 09:20:09 -06:00
int pos2gameobject(HMM_Vec2 pos);
2023-11-14 09:20:09 -06:00
void gameobject_move(struct gameobject *go, HMM_Vec2 vec);
2022-11-19 17:13:57 -06:00
void gameobject_rotate(struct gameobject *go, float as);
2022-12-19 18:15:38 -06:00
void gameobject_setangle(struct gameobject *go, float angle);
2022-12-26 20:57:45 -06:00
void gameobject_setpos(struct gameobject *go, cpVect vec);
2021-11-30 21:29:18 -06:00
void gameobject_draw_debugs();
2023-03-17 10:25:35 -05:00
void gameobject_draw_debug(int go);
2022-11-19 17:13:57 -06:00
void object_gui(struct gameobject *go);
2022-08-12 14:03:56 -05:00
2022-12-22 16:58:06 -06:00
void gameobject_saveall();
void gameobject_loadall();
int gameobjects_saved();
2021-11-30 21:29:18 -06:00
#endif