2024-02-29 11:22:52 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2024-03-05 11:40:42 +08:00
|
|
|
#include "types.h"
|
2024-02-29 11:22:52 +08:00
|
|
|
#include "util/vector.h"
|
2024-03-04 14:20:50 +08:00
|
|
|
#include "util/tree.h"
|
2024-03-05 11:40:42 +08:00
|
|
|
#include "physics.h"
|
|
|
|
#include "player.h"
|
2024-02-29 11:22:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2024-03-04 14:20:50 +08:00
|
|
|
typedef struct _Entity Entity;
|
|
|
|
typedef void (*entity_Thinker)(Entity *e, Duration deltaTime); // The Thinker function type assigned to Entities
|
|
|
|
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
// Entity.
|
2024-02-29 16:17:55 +08:00
|
|
|
typedef struct _Entity {
|
2024-03-01 17:09:24 +08:00
|
|
|
uintptr_t id;
|
2024-03-04 14:20:50 +08:00
|
|
|
char *name; // Allocated on the stack & copied
|
2024-02-29 11:22:52 +08:00
|
|
|
|
2024-02-29 16:17:55 +08:00
|
|
|
Component_Position *position;
|
|
|
|
Component_Hitbox *hitbox;
|
2024-03-01 17:09:24 +08:00
|
|
|
Component_Player *player;
|
2024-03-04 14:20:50 +08:00
|
|
|
|
|
|
|
entity_Thinker thinker; // Called by System_Entity each frame if not NULL.
|
|
|
|
void *thinkerData; // Data managed by the Thinker, if exists.
|
2024-02-29 16:17:55 +08:00
|
|
|
} Entity;
|
2024-02-29 11:22:52 +08:00
|
|
|
|
2024-03-04 16:13:43 +08:00
|
|
|
#define ADD_COMPONENT(entity, component, value) \
|
|
|
|
do { \
|
|
|
|
entity->component = value; \
|
|
|
|
entity->component->super = entity; \
|
|
|
|
} while (false)
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
|
2024-03-04 14:20:50 +08:00
|
|
|
typedef struct _App App;
|
|
|
|
|
|
|
|
// Entity manager.
|
|
|
|
typedef struct {
|
|
|
|
App *super;
|
|
|
|
tree_Tree *entities; // Contains Entity objects on the nodes
|
|
|
|
uintptr_t maxID;
|
|
|
|
|
|
|
|
vector_Vector *flaggedDelete;
|
|
|
|
} System_Entity;
|
|
|
|
|
|
|
|
System_Entity *entity_NewSystem(App *super);
|
|
|
|
void entity_DeleteSystem(System_Entity *sys);
|
|
|
|
|
|
|
|
Entity *entity_Create(System_Entity *sys, const char *name);
|
2024-03-04 16:13:43 +08:00
|
|
|
// After the Entity from Create is assigned its components,
|
|
|
|
// it should be commited into the system via this function.
|
|
|
|
void entity_Commit(System_Entity *sys, Entity *e);
|
|
|
|
void entity_Delete(System_Entity *sys, uintptr_t id);
|
2024-03-04 14:20:50 +08:00
|
|
|
|
|
|
|
void entity_Advance(System_Entity *sys, Duration deltaTime);
|
|
|
|
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|