world: add Classify component

isolation_bkp/dynres
Dominik Madarász 2021-07-27 17:57:50 +02:00
parent a43360161c
commit 5212aedf92
9 changed files with 50 additions and 18 deletions

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
uint64_t entity_spawn(char *name); uint64_t entity_spawn(char *name, uint16_t class_id /* 0 = no streaming */);
void entity_despawn(uint64_t ent_id); void entity_despawn(uint64_t ent_id);

View File

@ -5,8 +5,10 @@
#include "zpl.h" #include "zpl.h"
typedef enum { typedef enum {
EKIND_PLAYER = 0, EKIND_SERVER = 0,
EKIND_THING, EKIND_PLAYER,
EKIND_DEMO_NPC,
EKIND_MONSTER,
EKIND_CHUNK, EKIND_CHUNK,
FORCE_EKIND_UINT16 = UINT16_MAX FORCE_EKIND_UINT16 = UINT16_MAX
} entity_kind; } entity_kind;

View File

@ -1,4 +1,5 @@
#include "entity.h" #include "entity.h"
#include "entity_view.h"
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "flecs/flecs_meta.h" #include "flecs/flecs_meta.h"
#include "librg.h" #include "librg.h"
@ -8,7 +9,7 @@
#include "modules/systems.h" #include "modules/systems.h"
#include "zpl.h" #include "zpl.h"
uint64_t entity_spawn(char *name) { uint64_t entity_spawn(char *name, uint16_t class_id) {
ECS_IMPORT(world_ecs(), Components); ECS_IMPORT(world_ecs(), Components);
ecs_entity_t e = ecs_new(world_ecs(), 0); ecs_entity_t e = ecs_new(world_ecs(), 0);
@ -19,6 +20,7 @@ uint64_t entity_spawn(char *name) {
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name }); ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_set(world_ecs(), e, Velocity, {0}); ecs_set(world_ecs(), e, Velocity, {0});
ecs_set(world_ecs(), e, Classify, { .id = class_id });
ecs_add(world_ecs(), e, Walking); ecs_add(world_ecs(), e, Walking);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL); Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
#if 1 #if 1
@ -29,9 +31,10 @@ uint64_t entity_spawn(char *name) {
pos->y=88; pos->y=88;
#endif #endif
if (class_id != EKIND_SERVER) {
librg_entity_track(world_tracker(), e); librg_entity_track(world_tracker(), e);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0)); librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));
}
return (uint64_t)e; return (uint64_t)e;
} }

View File

@ -3,6 +3,7 @@
#include "system.h" #include "system.h"
#include "game.h" #include "game.h"
#include "entity.h" #include "entity.h"
#include "entity_view.h"
#include "utils/options.h" #include "utils/options.h"
#include "signal_handling.h" #include "signal_handling.h"
#include "profiler.h" #include "profiler.h"
@ -69,7 +70,7 @@ int main(int argc, char** argv) {
{ {
ECS_IMPORT(world_ecs(), Components); ECS_IMPORT(world_ecs(), Components);
for (uint32_t i = 0; i < npc_count; i++) { for (uint32_t i = 0; i < npc_count; i++) {
uint64_t e = entity_spawn(NULL); uint64_t e = entity_spawn(NULL, EKIND_DEMO_NPC);
ecs_add(world_ecs(), e, EcsDemoNPC); ecs_add(world_ecs(), e, EcsDemoNPC);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL); Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
pos->x=rand() % world_dim(); pos->x=rand() % world_dim();

View File

@ -193,7 +193,7 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) {
float fixed_title_offset = 2; float fixed_title_offset = 2;
switch (data->kind) { switch (data->kind) {
case EKIND_THING: { case EKIND_DEMO_NPC: {
float x = data->x; float x = data->x;
float y = data->y; float y = data->y;
#if 0 #if 0
@ -207,10 +207,12 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) {
case EKIND_PLAYER: { case EKIND_PLAYER: {
float x = data->x; float x = data->x;
float y = data->y; float y = data->y;
#if 1
const char *title = TextFormat("Player %d", key); const char *title = TextFormat("Player %d", key);
int title_w = MeasureTextEco(title, font_size, font_spacing); int title_w = MeasureTextEco(title, font_size, font_spacing);
DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time)); DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time));
DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing); DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing);
#endif
DrawCircleEco(x, y, size, ColorAlpha(RED, data->tran_time)); DrawCircleEco(x, y, size, ColorAlpha(RED, data->tran_time));
}break; }break;
default:break; default:break;

View File

@ -1,5 +1,6 @@
#include "player.h" #include "player.h"
#include "entity.h" #include "entity.h"
#include "entity_view.h"
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "flecs/flecs_meta.h" #include "flecs/flecs_meta.h"
#include "librg.h" #include "librg.h"
@ -10,7 +11,7 @@
#include "zpl.h" #include "zpl.h"
uint64_t player_spawn(char *name) { uint64_t player_spawn(char *name) {
ecs_entity_t e = entity_spawn(NULL); ecs_entity_t e = entity_spawn(NULL, EKIND_PLAYER);
if (!name) { if (!name) {
name = zpl_bprintf("player_%d", e); name = zpl_bprintf("player_%d", e);

View File

@ -17,10 +17,13 @@ entity_view world_build_entity_view(int64_t e) {
ECS_IMPORT(world_ecs(), Components); ECS_IMPORT(world_ecs(), Components);
entity_view view = {0}; entity_view view = {0};
// TODO(zaklaus): branch out based on ECS tags const Classify *classify = ecs_get(world_ecs(), e, Classify);
assert(classify);
view.kind = classify->id;
const Position *pos = ecs_get(world_ecs(), e, Position); const Position *pos = ecs_get(world_ecs(), e, Position);
if (pos) { if (pos) {
view.kind = ecs_has(world_ecs(), e, EcsClient) ? EKIND_PLAYER : EKIND_THING;
view.x = pos->x; view.x = pos->x;
view.y = pos->y; view.y = pos->y;
} }
@ -35,7 +38,6 @@ entity_view world_build_entity_view(int64_t e) {
if (ecs_get(world_ecs(), e, Chunk)) { if (ecs_get(world_ecs(), e, Chunk)) {
Chunk *chpos = ecs_get_mut(world_ecs(), e, Chunk, 0); Chunk *chpos = ecs_get_mut(world_ecs(), e, Chunk, 0);
view.kind = EKIND_CHUNK;
view.x = chpos->x; view.x = chpos->x;
view.y = chpos->y; view.y = chpos->y;
view.blocks_used = 1; view.blocks_used = 1;
@ -149,6 +151,7 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
for (int i = 0; i < world.chunk_amount * world.chunk_amount; ++i) { for (int i = 0; i < world.chunk_amount * world.chunk_amount; ++i) {
ecs_entity_t e = ecs_new(world.ecs, 0); ecs_entity_t e = ecs_new(world.ecs, 0);
ecs_set(world.ecs, e, Classify, {.id = EKIND_CHUNK });
Chunk *chunk = ecs_get_mut(world.ecs, e, Chunk, NULL); Chunk *chunk = ecs_get_mut(world.ecs, e, Chunk, NULL);
librg_entity_track(world.tracker, e); librg_entity_track(world.tracker, e);
librg_entity_chunk_set(world.tracker, e, i); librg_entity_chunk_set(world.tracker, e, i);

View File

@ -33,12 +33,28 @@ ECS_STRUCT(ClientInfo, {
uint16_t view_id; uint16_t view_id;
}); });
ECS_STRUCT(Health, {
float hp;
float max_hp;
// NOTE(zaklaus): Intentionally global, to allow for creative use of damage combos
float pain_time;
});
ECS_STRUCT(Classify, {
uint16_t id;
});
typedef struct { typedef struct {
ECS_DECLARE_COMPONENT(Chunk); ECS_DECLARE_COMPONENT(Chunk);
ECS_DECLARE_COMPONENT(Position); ECS_DECLARE_COMPONENT(Position);
ECS_DECLARE_COMPONENT(Vector2D); ECS_DECLARE_COMPONENT(Vector2D);
ECS_DECLARE_COMPONENT(Drawable); ECS_DECLARE_COMPONENT(Drawable);
ECS_DECLARE_COMPONENT(Input); ECS_DECLARE_COMPONENT(Input);
ECS_DECLARE_COMPONENT(Velocity);
ECS_DECLARE_COMPONENT(ClientInfo);
ECS_DECLARE_COMPONENT(Health);
ECS_DECLARE_COMPONENT(Classify);
ECS_DECLARE_ENTITY(EcsActor); ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsPlayer); ECS_DECLARE_ENTITY(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder); ECS_DECLARE_ENTITY(EcsBuilder);
@ -48,9 +64,7 @@ typedef struct {
ECS_DECLARE_TYPE(Movement); ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking); ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying); ECS_DECLARE_ENTITY(Flying);
ECS_DECLARE_COMPONENT(Velocity);
ECS_DECLARE_ENTITY(EcsClient); ECS_DECLARE_ENTITY(EcsClient);
ECS_DECLARE_COMPONENT(ClientInfo);
} Components; } Components;
#define ComponentsImportHandles(handles)\ #define ComponentsImportHandles(handles)\
@ -59,17 +73,19 @@ ECS_IMPORT_COMPONENT(handles, Vector2D);\
ECS_IMPORT_COMPONENT(handles, Position);\ ECS_IMPORT_COMPONENT(handles, Position);\
ECS_IMPORT_COMPONENT(handles, Drawable);\ ECS_IMPORT_COMPONENT(handles, Drawable);\
ECS_IMPORT_COMPONENT(handles, Input);\ ECS_IMPORT_COMPONENT(handles, Input);\
ECS_IMPORT_COMPONENT(handles, Velocity);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
ECS_IMPORT_COMPONENT(handles, Health);\
ECS_IMPORT_COMPONENT(handles, Classify);\
ECS_IMPORT_TYPE(handles, Player);\ ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\ ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, EcsActor);\ ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\ ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\ ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\ ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, Walking);\ ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\ ECS_IMPORT_ENTITY(handles, Flying);\
ECS_IMPORT_COMPONENT(handles, Velocity);\
ECS_IMPORT_ENTITY(handles, EcsClient);\ ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
void ComponentsImport(ecs_world_t *ecs); void ComponentsImport(ecs_world_t *ecs);

View File

@ -11,6 +11,8 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_META(ecs, ClientInfo); ECS_META(ecs, ClientInfo);
ECS_META(ecs, Velocity); ECS_META(ecs, Velocity);
ECS_META(ecs, Input); ECS_META(ecs, Input);
ECS_META(ecs, Health);
ECS_META(ecs, Classify);
ECS_TAG(ecs, Walking); ECS_TAG(ecs, Walking);
ECS_TAG(ecs, Flying); ECS_TAG(ecs, Flying);
@ -33,6 +35,8 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_SET_COMPONENT(Velocity); ECS_SET_COMPONENT(Velocity);
ECS_SET_COMPONENT(ClientInfo); ECS_SET_COMPONENT(ClientInfo);
ECS_SET_COMPONENT(Input); ECS_SET_COMPONENT(Input);
ECS_SET_COMPONENT(Health);
ECS_SET_COMPONENT(Classify);
ECS_SET_ENTITY(EcsClient); ECS_SET_ENTITY(EcsClient);
ECS_SET_ENTITY(Walking); ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying); ECS_SET_ENTITY(Flying);