summaryrefslogtreecommitdiff
path: root/src/game/object.h
blob: 8191ab795e6d6bdc2e105d2844ee5e4940cccbee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include "../util/allocator.h"
#include "../util/vector.h"
#include "../util/fnv.h"

typedef void ( *OBJECT_DEINIT_FN )( struct OBJECT* obj );
inline const char* obj_classid_to_name( U32 classid );

enum ObjectClass_t {
  OBJCLASS_NONE = 0,
  OBJCLASS_WORLD,
  OBJCLASS_TRIGGER,
  OBJCLASS_PLAYER,
  OBJCLASS_BASENPC,
};

static const char* OBJCLASS_NAMES[] = {
  "none (invalid - FIXME)",
  "world",
  "trigger",
  "player",
  "base_npc",
};

// todo: come up with a good way to make these static
struct OBJECT_PROP {
  OBJECT_PROP( const char* name, void* value, U32 size, LIST<OBJECT_PROP*>* list, void* parent ) {
    this->name = name;
    this->size = size;
    this->hash = fnv1a( name );
    this->offset = (U32)( (U8*)value - (U8*)parent );
    list->push( this );
  }

  const char* name;
  FNV1A hash;
  U32 offset;
  U32 size;
};

#define OBJVAR( type, name ) \
  type name; \
  OBJECT_PROP __##name##_props{ #name, (void*)&name, sizeof(type), &this->props, this }

#define OBJVAR_STR( name, len ) \
  char name##[len]; \
  OBJECT_PROP __##name##_props{ #name, (void*)name, len, &this->props, this }

struct OBJECT {
  static const U32 CLASSID = OBJCLASS_NONE;

  U32 classid;
  U8  keeponlevel{};
  char name[64];

  OBJECT* parent;
  LIST<OBJECT*> children{};
  LIST<OBJECT_PROP*> props{};
  OBJECT_DEINIT_FN deinit_fn{};

  OBJVAR( VEC3, pos );
  OBJVAR( VEC3, rot );
  OBJVAR( I32, idx );
};

inline const char* obj_classid_to_name( U32 classid ) {
  if( classid > (sizeof(OBJCLASS_NAMES) / sizeof(OBJCLASS_NAMES[0])) ) {
    dlog( "obj_classid_to_name() : invalid classid passed (%d)", classid );
    return "INVALIDCLASSID";
  }

  return OBJCLASS_NAMES[classid];
}