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
#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);

View File

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

View File

@ -1,4 +1,5 @@
#include "entity.h"
#include "entity_view.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
@ -8,7 +9,7 @@
#include "modules/systems.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_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, Velocity, {0});
ecs_set(world_ecs(), e, Classify, { .id = class_id });
ecs_add(world_ecs(), e, Walking);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
#if 1
@ -29,9 +31,10 @@ uint64_t entity_spawn(char *name) {
pos->y=88;
#endif
librg_entity_track(world_tracker(), e);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));
if (class_id != EKIND_SERVER) {
librg_entity_track(world_tracker(), e);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));
}
return (uint64_t)e;
}

View File

@ -3,6 +3,7 @@
#include "system.h"
#include "game.h"
#include "entity.h"
#include "entity_view.h"
#include "utils/options.h"
#include "signal_handling.h"
#include "profiler.h"
@ -69,7 +70,7 @@ int main(int argc, char** argv) {
{
ECS_IMPORT(world_ecs(), Components);
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);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
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;
switch (data->kind) {
case EKIND_THING: {
case EKIND_DEMO_NPC: {
float x = data->x;
float y = data->y;
#if 0
@ -207,10 +207,12 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) {
case EKIND_PLAYER: {
float x = data->x;
float y = data->y;
#if 1
const char *title = TextFormat("Player %d", key);
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));
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));
}break;
default:break;

View File

@ -1,5 +1,6 @@
#include "player.h"
#include "entity.h"
#include "entity_view.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
@ -10,7 +11,7 @@
#include "zpl.h"
uint64_t player_spawn(char *name) {
ecs_entity_t e = entity_spawn(NULL);
ecs_entity_t e = entity_spawn(NULL, EKIND_PLAYER);
if (!name) {
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);
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);
if (pos) {
view.kind = ecs_has(world_ecs(), e, EcsClient) ? EKIND_PLAYER : EKIND_THING;
view.x = pos->x;
view.y = pos->y;
}
@ -35,7 +38,6 @@ entity_view world_build_entity_view(int64_t e) {
if (ecs_get(world_ecs(), e, Chunk)) {
Chunk *chpos = ecs_get_mut(world_ecs(), e, Chunk, 0);
view.kind = EKIND_CHUNK;
view.x = chpos->x;
view.y = chpos->y;
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) {
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);
librg_entity_track(world.tracker, e);
librg_entity_chunk_set(world.tracker, e, i);

View File

@ -33,12 +33,28 @@ ECS_STRUCT(ClientInfo, {
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 {
ECS_DECLARE_COMPONENT(Chunk);
ECS_DECLARE_COMPONENT(Position);
ECS_DECLARE_COMPONENT(Vector2D);
ECS_DECLARE_COMPONENT(Drawable);
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(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder);
@ -48,9 +64,7 @@ typedef struct {
ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying);
ECS_DECLARE_COMPONENT(Velocity);
ECS_DECLARE_ENTITY(EcsClient);
ECS_DECLARE_COMPONENT(ClientInfo);
} Components;
#define ComponentsImportHandles(handles)\
@ -59,17 +73,19 @@ ECS_IMPORT_COMPONENT(handles, Vector2D);\
ECS_IMPORT_COMPONENT(handles, Position);\
ECS_IMPORT_COMPONENT(handles, Drawable);\
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, Builder);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\
ECS_IMPORT_COMPONENT(handles, Velocity);\
ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
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, Velocity);
ECS_META(ecs, Input);
ECS_META(ecs, Health);
ECS_META(ecs, Classify);
ECS_TAG(ecs, Walking);
ECS_TAG(ecs, Flying);
@ -33,6 +35,8 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_SET_COMPONENT(Velocity);
ECS_SET_COMPONENT(ClientInfo);
ECS_SET_COMPONENT(Input);
ECS_SET_COMPONENT(Health);
ECS_SET_COMPONENT(Classify);
ECS_SET_ENTITY(EcsClient);
ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying);