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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#ifndef ENT_H
#define ENT_H
#include "common.h"
#include "physics.h"
#define ENTITY_INVALID_IDX UINT32_MAX
// typedef enum {
// ENT_COMP_TRANSFORM,
// ENT_COMP_MODEL,
// ENT_COMP_PHYSICS,
// ENT_COMP_EDITOR,
// ENT_COMP_CNT,
// } EntityComponentType;
typedef struct {
u64 parent;
u64 first_child;
u64 prev, next; // siblings
/* parent-local */
Vec3 pos, scale;
Quaternion rot;
} TransformCmp;
typedef struct {
char *key;
Model *mdl;
bool has, draw;
} ModelCmp;
typedef struct {
PhysicsBody *bodies;
PhysicsJoint *joints;
u32 body_cnt, joint_cnt;
u32 root_body;
struct {
PhysicsBodyRuntime *bodies;
b3JointId *joints;
Matrix world;
Quaternion world_rot;
} runtime;
bool has, debug_draw;
} PhysicsCmp;
typedef struct {
bool selected;
} EditorCmp; // transient data for non transient entities ^___^
typedef struct {
TransformCmp tr;
ModelCmp mdl;
PhysicsCmp phys;
EditorCmp ed;
u64 pid; // persistent serialized id, not runtime entity id
char *name;
} Entity;
void entity_system_init(void);
void entity_system_exit(void);
void entity_system_clear(void);
void entity_system_update(void);
void entity_system_draw_3d(void);
void entity_system_set_next_pid(u64);
/* clears all entities of their selected flag */
void entities_clear_selected(void);
u64 entity_create(bool transient); // transient means no pid
u64 entity_restore(u64 pid);
void entity_delete(u64 id);
void entity_clean(Entity*); // clean entity data without removing
Entity entity_extract_now(u64 id); // removes, returns ownership
bool entity_valid(u64 id);
Entity* entity_get(u64 id);
u64 entity_id_at(u32 dense_idx);
u64 entity_id_from_pid(u64 pid);
Entity* entity_data(void);
u32 entity_count(void);
u64 entity_next_pid(void);
bool entity_set_parent(u64 id, u64 parent_id);
/* transform component data */
Matrix entity_world_matrix(u64 id);
/* space identifies the reference entity, 0 means world space. */
Vec3 entity_point_to_world(u64 space, Vec3 point);
bool entity_point_to_local(u64 space, Vec3 point, Vec3 *out);
Vec3 entity_world_pos(u64 id);
Quaternion entity_world_rot(u64 id);
void entity_set_world_pos(Entity*, Vec3);
void entity_set_world_rot(Entity*, Quaternion);
/* physics component data */
PhysicsCmp entity_physics_copy(const PhysicsCmp*);
void entity_physics_build(u64 id);
void entity_physics_clear(PhysicsCmp*);
void entity_physics_free(PhysicsCmp*);
void entity_physics_transform_changed(u64 id);
#endif
|