code: structure out flecs code
parent
5000a11dd1
commit
0f7179cb62
|
@ -13,8 +13,11 @@ add_executable(eco2d-server
|
|||
header/world/world.h
|
||||
header/world/blocks.h
|
||||
header/world/blocks_info.h
|
||||
header/components/general.h
|
||||
|
||||
header/components/net.h
|
||||
header/components/physics.h
|
||||
header/components/general.h
|
||||
header/components/controllers.h
|
||||
|
||||
../../vendors/cwpack/cwpack.c
|
||||
../../vendors/cwpack/cwpack.h
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
#include "flecs/flecs.h"
|
||||
#include "flecs/flecs_meta.h"
|
||||
|
||||
#include "components/general.h"
|
||||
#include "components/physics.h"
|
||||
|
||||
ECS_STRUCT(Input, {
|
||||
double x;
|
||||
double y;
|
||||
uint8_t use;
|
||||
});
|
||||
|
||||
typedef struct {
|
||||
ECS_DECLARE_COMPONENT(Input);
|
||||
ECS_DECLARE_ENTITY(EcsPlayer);
|
||||
ECS_DECLARE_ENTITY(EcsBuilder);
|
||||
ECS_DECLARE_TYPE(Player);
|
||||
ECS_DECLARE_TYPE(Builder);
|
||||
} Controllers;
|
||||
|
||||
#define ControllersImportHandles(handles)\
|
||||
ECS_IMPORT_COMPONENT(handles, Input);\
|
||||
ECS_IMPORT_TYPE(handles, Player);\
|
||||
ECS_IMPORT_TYPE(handles, Builder);\
|
||||
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
|
||||
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
|
||||
|
||||
static inline void ControllersImport(ecs_world_t *ecs) {
|
||||
ECS_MODULE(ecs, Controllers);
|
||||
ecs_set_name_prefix(ecs, "Controllers");
|
||||
|
||||
ECS_IMPORT(ecs, General);
|
||||
ECS_IMPORT(ecs, Physics);
|
||||
|
||||
ECS_IMPORT(ecs, FlecsMeta);
|
||||
ECS_META(ecs, Input);
|
||||
|
||||
ECS_TAG(ecs, EcsPlayer);
|
||||
ECS_TAG(ecs, EcsBuilder);
|
||||
|
||||
ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input);
|
||||
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsPlayer);
|
||||
ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsBuilder);
|
||||
|
||||
ECS_SET_COMPONENT(Input);
|
||||
ECS_SET_ENTITY(EcsPlayer);
|
||||
ECS_SET_ENTITY(EcsBuilder);
|
||||
ECS_SET_TYPE(Builder);
|
||||
ECS_SET_TYPE(Player);
|
||||
}
|
|
@ -2,35 +2,37 @@
|
|||
#include "flecs/flecs.h"
|
||||
#include "flecs/flecs_meta.h"
|
||||
|
||||
ECS_STRUCT(Chunk, {
|
||||
ECS_STRUCT(Vector2D, {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
});
|
||||
|
||||
ECS_STRUCT(Position, {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
});
|
||||
typedef Vector2D Chunk;
|
||||
typedef Vector2D Position;
|
||||
|
||||
typedef struct {
|
||||
ECS_DECLARE_COMPONENT(Chunk);
|
||||
ECS_DECLARE_COMPONENT(Position);
|
||||
} Common;
|
||||
ECS_DECLARE_COMPONENT(Vector2D);
|
||||
} General;
|
||||
|
||||
#define CommonImportHandles(handles)\
|
||||
#define GeneralImportHandles(handles)\
|
||||
ECS_IMPORT_COMPONENT(handles, Chunk);\
|
||||
ECS_IMPORT_COMPONENT(handles, Vector2D);\
|
||||
ECS_IMPORT_COMPONENT(handles, Position);
|
||||
|
||||
static inline void CommonImport(ecs_world_t *ecs) {
|
||||
ECS_MODULE(ecs, Common);
|
||||
static inline void GeneralImport(ecs_world_t *ecs) {
|
||||
ECS_MODULE(ecs, General);
|
||||
ecs_set_name_prefix(ecs, "General");
|
||||
|
||||
ecs_set_name_prefix(ecs, "Common");
|
||||
ECS_COMPONENT(ecs, Chunk);
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
|
||||
ECS_IMPORT(ecs, FlecsMeta);
|
||||
|
||||
ECS_META(ecs, Chunk);
|
||||
ECS_META(ecs, Position);
|
||||
ECS_META(ecs, Vector2D);
|
||||
|
||||
ECS_EXPORT_COMPONENT(Chunk);
|
||||
ECS_EXPORT_COMPONENT(Position);
|
||||
ECS_SET_COMPONENT(Chunk);
|
||||
ECS_SET_COMPONENT(Vector2D);
|
||||
ECS_SET_COMPONENT(Position);
|
||||
}
|
||||
|
|
|
@ -2,25 +2,30 @@
|
|||
#include "flecs/flecs.h"
|
||||
#include "flecs/flecs_meta.h"
|
||||
|
||||
ECS_STRUCT(NetClient, {
|
||||
ECS_STRUCT(ClientInfo, {
|
||||
uint16_t peer_id;
|
||||
});
|
||||
|
||||
typedef struct {
|
||||
ECS_DECLARE_COMPONENT(NetClient);
|
||||
ECS_DECLARE_ENTITY(EcsClient);
|
||||
ECS_DECLARE_COMPONENT(ClientInfo);
|
||||
} Net;
|
||||
|
||||
#define NetImportHandles(handles)\
|
||||
ECS_IMPORT_COMPONENT(handles, NetClient);\
|
||||
ECS_IMPORT_ENTITY(handles, EcsClient);\
|
||||
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
|
||||
|
||||
static inline void NetImport(ecs_world_t *ecs) {
|
||||
ECS_MODULE(ecs, Net);
|
||||
|
||||
ecs_set_name_prefix(ecs, "Net");
|
||||
|
||||
ECS_TAG(ecs, EcsClient);
|
||||
|
||||
ECS_IMPORT(ecs, FlecsMeta);
|
||||
|
||||
ECS_META(ecs, NetClient);
|
||||
ECS_META(ecs, ClientInfo);
|
||||
|
||||
ECS_EXPORT_COMPONENT(NetClient);
|
||||
ECS_EXPORT_ENTITY(EcsClient);
|
||||
ECS_EXPORT_COMPONENT(ClientInfo);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
#include "flecs/flecs.h"
|
||||
#include "components/general.h"
|
||||
|
||||
typedef Vector2D Velocity;
|
||||
|
||||
typedef struct {
|
||||
ECS_DECLARE_TYPE(Movement);
|
||||
ECS_DECLARE_ENTITY(Walking);
|
||||
ECS_DECLARE_ENTITY(Flying);
|
||||
ECS_DECLARE_COMPONENT(Velocity);
|
||||
} Physics;
|
||||
|
||||
#define PhysicsImportHandles(handles)\
|
||||
ECS_IMPORT_TYPE(handles, Movement);\
|
||||
ECS_IMPORT_ENTITY(handles, Walking);\
|
||||
ECS_IMPORT_ENTITY(handles, Flying);\
|
||||
ECS_IMPORT_COMPONENT(handles, Velocity);\
|
||||
|
||||
static inline void PhysicsImport(ecs_world_t *ecs) {
|
||||
ECS_MODULE(ecs, Physics);
|
||||
ecs_set_name_prefix(ecs, "Physics");
|
||||
|
||||
ECS_TAG(ecs, Walking);
|
||||
ECS_TAG(ecs, Flying);
|
||||
ECS_TYPE(ecs, Movement, Walking, Flying);
|
||||
|
||||
ECS_COMPONENT(ecs, Velocity);
|
||||
|
||||
ECS_SET_TYPE(Movement);
|
||||
ECS_SET_ENTITY(Walking);
|
||||
ECS_SET_ENTITY(Flying);
|
||||
ECS_SET_COMPONENT(Velocity);
|
||||
}
|
|
@ -8,5 +8,5 @@ int32_t network_server_stop(void);
|
|||
int32_t network_server_tick(void);
|
||||
void network_server_update(void *data);
|
||||
|
||||
uint64_t network_player_create(uint16_t peer_id);
|
||||
void network_player_destroy(uint64_t ent_id);
|
||||
uint64_t network_client_create(uint16_t peer_id);
|
||||
void network_client_destroy(uint64_t ent_id);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "world/world.h"
|
||||
|
||||
#include "components/general.h"
|
||||
#include "components/controllers.h"
|
||||
#include "components/net.h"
|
||||
|
||||
#define NETWORK_UPDATE_DELAY 0.100
|
||||
|
@ -67,14 +68,15 @@ int32_t network_server_tick(void) {
|
|||
case ENET_EVENT_TYPE_CONNECT: {
|
||||
zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID);
|
||||
uint16_t peer_id = event.peer->incomingPeerID;
|
||||
uint64_t ent_id = network_player_create(peer_id);
|
||||
uint64_t ent_id = network_client_create(peer_id);
|
||||
// TODO: Make sure ent_id does not get truncated with large entity numbers.
|
||||
event.peer->data = (void*)((uint32_t)ent_id);
|
||||
} break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: {
|
||||
zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID);
|
||||
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
|
||||
network_player_destroy(e);
|
||||
network_client_destroy(e);
|
||||
} break;
|
||||
|
||||
case ENET_EVENT_TYPE_RECEIVE: {
|
||||
|
@ -126,13 +128,14 @@ void network_server_update(void *data) {
|
|||
// }
|
||||
}
|
||||
|
||||
uint64_t network_player_create(uint16_t peer_id) {
|
||||
ECS_IMPORT(world_ecs(), Common);
|
||||
uint64_t network_client_create(uint16_t peer_id) {
|
||||
ECS_IMPORT(world_ecs(), General);
|
||||
ECS_IMPORT(world_ecs(), Controllers);
|
||||
ECS_IMPORT(world_ecs(), Net);
|
||||
|
||||
ecs_entity_t e = ecs_new(world_ecs(), 0);
|
||||
ecs_set(world_ecs(), e, Position, {0, 0});
|
||||
ecs_set(world_ecs(), e, NetClient, {peer_id});
|
||||
ecs_entity_t e = ecs_new(world_ecs(), Player);
|
||||
ecs_add(world_ecs(), e, EcsClient);
|
||||
ecs_set(world_ecs(), e, ClientInfo, {peer_id});
|
||||
|
||||
librg_entity_track(world_tracker(), e);
|
||||
librg_entity_owner_set(world_tracker(), e, peer_id);
|
||||
|
@ -142,7 +145,7 @@ uint64_t network_player_create(uint16_t peer_id) {
|
|||
return (uint64_t)e;
|
||||
}
|
||||
|
||||
void network_player_destroy(uint64_t ent_id) {
|
||||
void network_client_destroy(uint64_t ent_id) {
|
||||
librg_entity_untrack(world_tracker(), ent_id);
|
||||
ecs_delete(world_ecs(), ent_id);
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1
|
|||
librg_config_chunkamount_set(world.tracker, world_size, world_size, 1);
|
||||
librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID);
|
||||
|
||||
ECS_IMPORT(world.ecs, Common);
|
||||
ECS_IMPORT(world.ecs, General);
|
||||
|
||||
for (int i = 0; i < chunk_size * chunk_size; ++i) {
|
||||
ecs_entity_t e = ecs_new(world.ecs, 0);
|
||||
|
|
Loading…
Reference in New Issue