Untitled
Loading...
Searching...
No Matches
entity.c
Go to the documentation of this file.
1
2#include "ECS/entity.h"
3#include "core/engine_types.h"
4#include "physics/physics.h"
5#include "utilities/types.h"
6#include "load/init.h"
7#include <stdio.h>
8#include "utilities/mathf.h"
10#include "core/mesh.h"
11#include "ECS/entity_states.h"
12
13
16
17
18static State * find_state_by_name(const char * name);
19static State * find_default_state(const char * name);
20
21typedef void (*func_ptr_t)(void*);
22
24{
25
26 sprintf(STATE_REGISTRY[STATE_COUNT].name, "chair_default");
29 // sprintf(STATE_REGISTRY[STATE_COUNT].name, "chair_combat");
30 // STATE_REGISTRY[STATE_COUNT].fn[ESTATE_COMBAT] = ESTATE_chair_combat;
31 // STATE_COUNT++;
32
33
34}
35
36static State * find_state_by_name(const char * name)
37{
38
39 for (int i = 0; i < STATE_COUNT; i++)
40 {
41 if (strcmp(STATE_REGISTRY[i].name, name) == 0)
42 {
43 return &STATE_REGISTRY[i];
44 }
45 }
46
47 return NULL;
48}
49
50
51static State * find_default_state(const char * name)
52{
53
54 char default_name[256];
55
56 sprintf(default_name, "%s_default", name);
57
58 State * s = find_state_by_name(default_name);
59 if (!s)
60 {
61 printf("NO DEFAULT STATE FOUND: %s\n", name);
62 }
63
64 return s;
65}
66
67
68// TODO entity loop
70{
71
72 for (int i = 0; i < MAX_ENTITIES; i++)
73 {
74 if (ENTITIES[i].free) continue;
75 Entity * ent = &ENTITIES[i];
76
77
78 if (ent->pc)
79 {
80
81
82
83 }
84
85 }
86
87}
89{
91
92 PhysicsComponent * pc = NULL;
93 for (int i = 0; i < MAX_PHYSICS_COMPONENTS; i++)
94 {
95 if (PHYSICS_COMPONENTS[i].free)
96 {
97 pc = &PHYSICS_COMPONENTS[i];
98 PHYSICS_COMPONENTS[i].free = false;
99 PHYSICS_COMPONENTS[i].ent = ent;
100
101 break;
102
103 }
104 }
105 if (!pc)
106 {
107
108 printf("NO AC SLOT FOUND\n");
109 ent->pc = NULL;
110 }
111 else
112 {
113 ent->pc = pc;
114 mns[ent->mesh_node].pc = pc;
115 init_PC(ent->mesh_node, pc, ent->aabb, collision_type, physics_type);
116 }
117
118 return pc;
119}
120
122{
123
124 AnimationComponent * ac = NULL;
125 for (int i = 0; i < MAX_ANIMATION_COMPONENTS; i++)
126 {
127 if (ANIMATION_COMPONENTS[i].free)
128 {
129 ac = &ANIMATION_COMPONENTS[i];
130 ANIMATION_COMPONENTS[i].free = false;
131 ANIMATION_COMPONENTS[i].ent = ent;
132
133 break;
134
135 }
136 }
137 if (!ac)
138 {
139
140 printf("NO AC SLOT FOUND\n");
141 ent->ac = NULL;
142 }
143 else
144 {
145 ent->ac = ac;
146 }
147
148 return ac;
149}
150
151
152
153Entity * create_entity(AssetInstance * asset_instance, int mn_id, bool physics_component, bool animation_component)
154{
155
156 bool found_entity = false;
157 Entity * ent = NULL;
158 for (int i = 0; i < MAX_ENTITIES; i++)
159 {
160 if (ENTITIES[i].free)
161 {
162
163 found_entity = true;
164 ent = &ENTITIES[i];
165 ENTITIES[i].free = false;
166 break;
167
168 }
169 }
170 if (!ent)
171 {
172 printf("NO ENT SLOT FOUND\n");
173 return NULL;
174 }
175
176 ent->mesh_node = mn_id;
177 ent->state = NULL;
179 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
180 ent->free = false;
181 if (asset_instance && mn_id != -1)
182 {
183 MeshNode * mn = &mns[mn_id];
184 ent->state = find_default_state(mn->name);
185 ent->mesh_node = mn_id;
186 mn->ent = ent;
187 ent->skin = mns[ent->mesh_node].skin;
188 ent->instance = asset_instance;
189 if (mn->mesh != -1)
190 {
191 Mesh * m = &meshes[mn->mesh];
192 ent->aabb = m->aabb;
193 }
194 else
195 {
196 ent->aabb = asset_instance->aabb;
197 }
198 if (physics_component)
199 {
201 }
202 else
203 {
204 ent->pc = NULL;
205
206 }
207
208 }
209 if (animation_component)
210 {
212
213 }
214 else
215 {
216 ent->ac = NULL;
217 }
218
219 return ent;
220
221}
222
223
225{
226
227 ent->free = true;
228 if (ent->pc)
229 {
230 ent->pc->free = true;
231 }
232 if (ent->ac)
233 {
234 ent->ac->free = true;
235 }
236
237
238}
239
Engine specific types and defines.
Vector mesh_nodes
MeshNodes.
int skin
The GLTFSkin to be modified by this Entity (skeletal animations).
@ ESTATE_PLAYER_INTERACT
void(* fn[ESTATE_MAX])(void *)
AnimationComponent * ac
The AnimationComponent inside of ANIMATION_COMPONENTS.
int mesh_node
The top-most MeshNode that belongs to this Entity.
Vector meshes
Meshes.
AABB aabb
The AABB over each MeshNode via Asset.
Globals GLOBALS
Engine Globals.
Definition main.c:310
bool free
If the component is empty.
bool free
If the Entity is empty.
struct AssetInstance * instance
The AssetInstance that spawned this Entity.
#define MAX_ENTITY_STATES
Maximim Entity states for behaviors.
Entity * ent
The pointer to the Entity.
State * state
The current State that describes actions for this Entity.
PhysicsComponent * pc
The PhysicsComponent inside of PHYSICS_COMPONENTS.
AABB aabb
The bounding box over all MeshNodes for this Entity.
The animation component for interpolation between AnimationKeyframes.
A instance of an Asset that retains its node structure but can be duplicated into multiple instances ...
Entity structure that holds components and references to the data it owns.
Entity States that control what functions are available for an Entity.
int STATE_COUNT
The amount of states inside of STATE_REGISTRY.
Definition entity.c:15
void update_entities()
TODO: Entity update loop.
Definition entity.c:69
State * find_default_state(const char *name)
Definition entity.c:51
void destroy_entity(Entity *ent)
Deletes an entity by simply freeing it and its components.
Definition entity.c:224
AnimationComponent * create_animation_component(Entity *ent)
Creates an animation component for an entity.
Definition entity.c:121
State STATE_REGISTRY[MAX_ENTITY_STATES]
Registry for entity states used for entity events and behaviors.
Definition entity.c:14
PhysicsComponent * create_physics_component(Entity *ent, CollisionType collision_type, PhysicsType physics_type)
Creates an physics component for an entity.
Definition entity.c:88
State * find_state_by_name(const char *name)
Definition entity.c:36
void STATES_INIT()
Initializes entity states, which are a registry of function pointers used for entity events and behav...
Definition entity.c:23
Entity * create_entity(AssetInstance *asset_instance, int mn_id, bool physics_component, bool animation_component)
Creates an entity using the free list of ENTITIES.
Definition entity.c:153
void(*) func_ptr_t(void *)
Definition entity.c:21
ECS creation and maintenance.
void ESTATE_chair_interact(void *data)
ECS registry for behaviors.
Entity * ent
Which Entity this component belongs to.
bool free
If this is an empty component.
void init_PC(int mesh_node, PhysicsComponent *pc, AABB ai_aabb, CollisionType collision_type, PhysicsType physics_type)
Initializes a PhysicsComponent.
Definition physics.c:2141
CollisionType
Defines collision types with different behaviors and mesh setup and validation.
PhysicsType
Defines physics types with different behaviors in impulse resolution.
Entity Component used for physics resolution.
int skin
The GLTFSkin it controls.
Entity * ent
The Entity this MeshNode refers to (all children point to the same Entity as parent).
PhysicsComponent * pc
The physics controller initialized based on data from this MeshNode.
CollisionType collision_type
The type of collision to apply for the PhysicsComponent that may not have been created yet.
char name[256]
The name generated on loading from file.
int mesh
The Mesh it references.
PhysicsType physics_mode
The type of physics to apply for the PhysicsComponent that may not have been created yet.
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Important globals for the entire engine and initialization functions that are mainly called on engine...
#define MAX_ENTITIES
Definition init.h:439
#define MAX_PHYSICS_COMPONENTS
Definition init.h:445
#define MAX_ANIMATION_COMPONENTS
Definition init.h:451
AABB aabb
The AABB of the mesh.
A Mesh with geometry data that is either initialized as static or dynamic and split into SubMeshes.
PhysicsComponent PHYSICS_COMPONENTS[MAX_PHYSICS_COMPONENTS]
Definition main.c:314
Entity ENTITIES[MAX_ENTITIES]
Definition main.c:312
AnimationComponent ANIMATION_COMPONENTS[MAX_ANIMATION_COMPONENTS]
Definition main.c:315
Mesh functions that modify and initialize Mesh data.
PhysicsComponent impulse resolution and collision detection and acceleration structure traversal.
#define CAST_VEC(vec, type)
Definition utilities.h:134