3dtoys/ecs_comps.h

116 lines
3.9 KiB
C
Raw Normal View History

2024-08-18 04:30:29 +00:00
// A bunch of component definitions that might not
// be useful in general, but who knows?
#ifndef __HALOO3D_ECSCOMP_H
#define __HALOO3D_ECSCOMP_H
#include "ecs.h"
#include "haloo3d/haloo3d.h"
// A component that simply points back to an object instance. If combined
// with movement, will pull pos from objin at start of frame, then write
// pos back to obj at end of frame.
typedef haloo3d_obj_instance *ecs_objin;
// Like objin, this component simply points to a camera. It will set the
// position AND rotation at the start of the frame, and pull the pos/
// rotation back into the camera at the end.
typedef haloo3d_camera *ecs_camera;
typedef struct {
struct vec3 pos;
struct vec3 dst;
int timer;
} ecs_moveto;
typedef struct {
struct vec2 rot;
struct vec2 dstrot;
int timer;
} ecs_rotateto;
// Move object position into moveto
2024-08-18 08:33:01 +00:00
// static void sys_ecs_objin_moveto(haloo_ecs *ecs, hecs_eidt id, hecs_cidt
// oiid,
// hecs_cidt mtid) {
// eprintf("Objin moveto %ld, %ld\n", oiid, mtid);
// ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
// ecs_objin *oi = HECS_ENTITYCOMPONENT(ecs_objin *, id, oiid, ecs);
// eprintf("Objin moveto END\n");
// mt->pos = (*oi)->pos;
// }
// Move camera position into moveto
static void sys_ecs_camera_moveto(haloo_ecs *ecs, hecs_eidt id, hecs_cidt camid,
hecs_cidt mtid) {
// eprintf("Camera moveto %ld, %ld\n", camid, mtid);
2024-08-18 04:30:29 +00:00
ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
2024-08-18 08:33:01 +00:00
ecs_camera *cam = HECS_ENTITYCOMPONENT(ecs_camera *, id, camid, ecs);
// eprintf("Camera moveto END\n");
mt->pos = (*cam)->pos;
}
static void sys_ecs_camera_rotateto(haloo_ecs *ecs, hecs_eidt id,
hecs_cidt camid, hecs_cidt rtid) {
ecs_rotateto *rt = HECS_ENTITYCOMPONENT(ecs_rotateto *, id, rtid, ecs);
ecs_camera *cam = HECS_ENTITYCOMPONENT(ecs_camera *, id, camid, ecs);
rt->rot.x = (*cam)->yaw;
rt->rot.y = (*cam)->pitch;
2024-08-18 04:30:29 +00:00
}
static void sys_ecs_moveto(haloo_ecs *ecs, hecs_eidt id, hecs_cidt mtid) {
ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
if (mt->timer <= 0) {
mt->pos = mt->dst;
return;
}
mfloat_t xdiff = mt->dst.x - mt->pos.x;
mfloat_t ydiff = mt->dst.y - mt->pos.y;
2024-08-18 08:33:01 +00:00
mfloat_t zdiff = mt->dst.z - mt->pos.z;
mt->pos.x += xdiff / mt->timer;
mt->pos.y += ydiff / mt->timer;
mt->pos.z += zdiff / mt->timer;
2024-08-18 04:30:29 +00:00
mt->timer--;
}
2024-08-18 08:33:01 +00:00
static void sys_ecs_rotateto(haloo_ecs *ecs, hecs_eidt id, hecs_cidt rtid) {
ecs_rotateto *rt = HECS_ENTITYCOMPONENT(ecs_rotateto *, id, rtid, ecs);
if (rt->timer <= 0) {
rt->rot = rt->dstrot;
return;
}
mfloat_t xdiff = rt->dstrot.x - rt->rot.x;
mfloat_t ydiff = rt->dstrot.y - rt->rot.y;
rt->rot.x += xdiff / rt->timer;
rt->rot.y += ydiff / rt->timer;
rt->timer--;
}
// // Move movement pos back into object
// static void sys_ecs_moveto_objin(haloo_ecs *ecs, hecs_eidt id, hecs_cidt
// mtid,
// hecs_cidt oiid) {
// ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
// ecs_objin *oi = HECS_ENTITYCOMPONENT(ecs_objin *, id, oiid, ecs);
// (*oi)->pos = mt->pos;
// }
// Move movement pos back into camera
static void sys_ecs_moveto_camera(haloo_ecs *ecs, hecs_eidt id, hecs_cidt mtid,
hecs_cidt camid) {
2024-08-18 04:30:29 +00:00
ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
2024-08-18 08:33:01 +00:00
ecs_camera *cam = HECS_ENTITYCOMPONENT(ecs_camera *, id, camid, ecs);
(*cam)->pos = mt->pos;
}
// Move rotation back into camera
static void sys_ecs_rotateto_camera(haloo_ecs *ecs, hecs_eidt id,
hecs_cidt rtid, hecs_cidt camid) {
ecs_rotateto *rt = HECS_ENTITYCOMPONENT(ecs_rotateto *, id, rtid, ecs);
ecs_camera *cam = HECS_ENTITYCOMPONENT(ecs_camera *, id, camid, ecs);
(*cam)->yaw = rt->rot.x;
(*cam)->pitch = rt->rot.y;
2024-08-18 04:30:29 +00:00
}
#endif