3dtoys/ecs_comps.h

62 lines
1.8 KiB
C

// 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
static void sys_ecs_objin_moveto(haloo_ecs *ecs, hecs_eidt id, hecs_cidt oiid,
hecs_cidt mtid) {
ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
ecs_objin *oi = HECS_ENTITYCOMPONENT(ecs_objin *, id, oiid, ecs);
mt->pos = (*oi)->pos;
}
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;
mt->pos.x = xdiff / mt->timer;
mt->pos.y = ydiff / mt->timer;
mt->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;
}
#endif