polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

entity.c (3078B)


      1 
      2 #include "entity.h"
      3 #include "node.h"
      4 #include "model.h"
      5 #include "resource.h"
      6 #include "debug.h"
      7 #include <assert.h>
      8 
      9 #define DEF_NUM_ENTITIES 1024
     10 
     11 static struct resource_manager esys;
     12 
     13 struct resource_manager *_internal_get_entity_system()
     14 {
     15     return &esys;
     16 }
     17 
     18 struct entity *get_all_entities(u32 *count, entity_id **ids) {
     19     return get_all_resources(&esys, count, ids);
     20 }
     21 
     22 struct entity *init_entity(struct entity *ent, node_id *id) {
     23     node_id new_id;
     24     init_id(&ent->model_id);
     25     if (id == NULL) {
     26         init_id(&new_id);
     27         new_node(&new_id);
     28         ent->node_id = new_id;
     29         /* debug("init_entity with new node_id %llu\n", new_id.uuid); */
     30     }
     31     else {
     32         ent->node_id = *id;
     33     }
     34     ent->flags &= ~ENT_CASTS_SHADOWS;
     35     return ent;
     36 }
     37 
     38 struct entity *get_entity(entity_id *ent_id) {
     39     return get_resource(&esys, ent_id);
     40 }
     41 
     42 static inline struct entity *new_uninitialized_entity(entity_id *id) {
     43     return new_resource(&esys, id);
     44 }
     45 
     46 struct entity *new_entity_(entity_id *id) {
     47     return new_entity_with_node(id, NULL);
     48 }
     49 
     50 
     51 
     52 struct entity *new_entity_with_node(entity_id *id, node_id *node)
     53 {
     54     entity_id new_id;
     55 
     56     if (id == NULL) {
     57         id = &new_id;
     58         init_id(&new_id);
     59     }
     60 
     61     return init_entity(new_uninitialized_entity(id), node);
     62 }
     63 
     64 struct entity *add_entity(struct entity *e, entity_id *id) {
     65     struct entity *new = new_uninitialized_entity(id);
     66     *new = *e;
     67     return new;
     68 }
     69 
     70 void destroy_entities() {
     71     entity_id *ids;
     72     u32 count;
     73     get_all_resources(&esys, &count, &ids);
     74     for (u32 i = RESERVED_ENTITIES; i < count; i++) {
     75         destroy_entity(&ids[i]);
     76     }
     77 };
     78 
     79 void destroy_entity(entity_id *id)
     80 {
     81     struct entity *ent = get_entity(id);
     82     if (ent == NULL)
     83         return;
     84 
     85     struct node *node = get_node(&ent->node_id);
     86     assert(node);
     87     if (node == NULL)
     88         return;
     89 
     90     if (id->index < RESERVED_ENTITIES) {
     91         unusual("trying to destroy reserved entity with node %s\n", node->label);
     92         return;
     93     }
     94 
     95     node_detach_from_parent(node);
     96     destroy_resource(&esys, id);
     97 }
     98 
     99 const char *entity_label(struct entity *ent)
    100 {
    101     struct node *node = get_node(&ent->node_id); assert(node);
    102     return node->label;
    103 }
    104 
    105 void destroy_entity_system() {
    106     destroy_resource_manager(&esys);
    107 }
    108 
    109 void init_entity_system() {
    110     init_resource_manager(&esys, sizeof(struct entity), DEF_NUM_ENTITIES,
    111                           MAX_ENTITIES, "entity", RESERVED_ENTITIES);
    112 
    113     for (int i = 0; i < RESERVED_ENTITIES; i++) {
    114         node_id node_id = make_static_id(i);
    115         struct entity *ent = &static_entities()[i];
    116         init_entity(ent, &node_id);
    117     }
    118 }
    119 
    120 struct entity *new_debug_entity(entity_id *ent_id, float *pos)
    121 {
    122     init_id(ent_id);
    123     struct entity *ent = new_entity(ent_id);
    124     ent->model_id = get_static_model(model_barrel, NULL);
    125     struct node *enode = get_node(&ent->node_id);
    126     node_set_label(enode, "debug");
    127     vec3_copy(pos, enode->pos);
    128     node_scale(enode, 5.0);
    129     node_mark_for_recalc(enode);
    130     node_recalc(enode);
    131     return ent;
    132 }
    133