Skip to content

Instantly share code, notes, and snippets.

@prenaux
Created July 26, 2022 12:21
Show Gist options
  • Save prenaux/3359128e61b0914cfc3ffc981617aa71 to your computer and use it in GitHub Desktop.
Save prenaux/3359128e61b0914cfc3ffc981617aa71 to your computer and use it in GitHub Desktop.
Flecs: Register niLang math types & component members for interop.
#define _TMEMBER(T,NAME) member<decltype(T::NAME)>(#NAME)
#define _MEMBER(NAME) member<decltype(NAME)>(#NAME)
void RegisterBaseTypes(flecs::world& aWorld) {
aWorld.component<sVec2f>()
._TMEMBER(sVec2f,x)
._TMEMBER(sVec2f,y);
aWorld.component<sVec3f>()
._TMEMBER(sVec3f,x)
._TMEMBER(sVec3f,y)
._TMEMBER(sVec3f,z);
aWorld.component<sVec4f>()
._TMEMBER(sVec4f,x)
._TMEMBER(sVec4f,y)
._TMEMBER(sVec4f,z)
._TMEMBER(sVec4f,w);
aWorld.component<sVec2i>()
._TMEMBER(sVec2i,x)
._TMEMBER(sVec2i,y);
aWorld.component<sVec3i>()
._TMEMBER(sVec3i,x)
._TMEMBER(sVec3i,y)
._TMEMBER(sVec3i,z);
aWorld.component<sVec4i>()
._TMEMBER(sVec4i,x)
._TMEMBER(sVec4i,y)
._TMEMBER(sVec4i,z)
._TMEMBER(sVec4i,w);
aWorld.component<sQuatf>()
._TMEMBER(sQuatf,x)
._TMEMBER(sQuatf,y)
._TMEMBER(sQuatf,z)
._TMEMBER(sQuatf,w);
aWorld.component<sMatrixf>()
._TMEMBER(sMatrixf,_11)
._TMEMBER(sMatrixf,_12)
._TMEMBER(sMatrixf,_13)
._TMEMBER(sMatrixf,_14)
._TMEMBER(sMatrixf,_21)
._TMEMBER(sMatrixf,_22)
._TMEMBER(sMatrixf,_23)
._TMEMBER(sMatrixf,_24)
._TMEMBER(sMatrixf,_31)
._TMEMBER(sMatrixf,_32)
._TMEMBER(sMatrixf,_33)
._TMEMBER(sMatrixf,_34)
._TMEMBER(sMatrixf,_41)
._TMEMBER(sMatrixf,_42)
._TMEMBER(sMatrixf,_43)
._TMEMBER(sMatrixf,_44);
}
struct cSprite {
sVec2f pos = Vec2f(0,0);
sVec2f size = Vec2f(1,1);
sVec2f lookAt = Vec2f(1,0);
static void Register(flecs::world& aWorld) {
aWorld.component<cSprite>()
._MEMBER(pos)
._MEMBER(size)
._MEMBER(lookAt);
}
};
struct cBadGuy {
tF32 radius = 10;
tF32 speed = 1;
tF32 health = 1;
tF32 hitPower = 10;
static void Register(flecs::world& aWorld) {
aWorld.component<cBadGuy>()
._MEMBER(radius)
._MEMBER(speed)
._MEMBER(health)
._MEMBER(hitPower);
}
static flecs::entity CreateEntity(flecs::world& aWorld) {
static tInt _count = 0;
return aWorld.entity(niFmt("BadGuy%d",++_count))
.set<cSprite>({})
.set<cBadGuy>({});
}
};
struct cPlayer {
tF32 speed = 1.0f;
tF32 health = 100;
tF32 shield = 10;
static void Register(flecs::world& aWorld) {
aWorld.component<cPlayer>()
._MEMBER(speed)
._MEMBER(health)
._MEMBER(shield);
}
static flecs::entity CreateEntity(flecs::world& aWorld) {
static tInt _count = 0;
return aWorld.entity(niFmt("Player%d",++_count))
.set<cSprite>({})
.set<cPlayer>({});
}
};
void InitWorld(flecs::world& aWorld) {
RegisterBaseTypes(aWorld);
cSprite::Register(aWorld);
cBadGuy::Register(aWorld);
cPlayer::Register(aWorld);
niLoop(i,10) {
cBadGuy::CreateEntity(aWorld);
}
cPlayer::CreateEntity(aWorld);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment