JacksEscape/entity.h

79 lines
2.1 KiB
C
Raw Permalink Normal View History

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"
#include "util/tree.h"
2024-03-05 11:40:42 +08:00
#include "physics.h"
#include "player.h"
2024-03-30 21:25:39 +08:00
#include "render_component.h"
#include "mapper_misc.h"
2024-02-29 11:22:52 +08:00
#ifdef __cplusplus
extern "C" {
#endif
2024-03-30 21:25:39 +08:00
typedef struct _App App;
typedef struct _Entity Entity;
2024-03-30 21:25:39 +08:00
typedef void (*entity_Thinker)(App *app, 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;
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-30 21:25:39 +08:00
Component_Render *render;
Component_Misc *misc;
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-05 11:55:26 +08:00
// https://en.cppreference.com/w/c/language/sizeof
// The expression in sizeof is not evaluated
#define ADD_COMPONENT(entity, component) \
do { \
entity->component = zero_malloc(sizeof(*entity->component)); \
entity->component->super = entity; \
2024-03-04 16:13:43 +08:00
} while (false)
2024-02-29 11:22:52 +08:00
// 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);
void entity_Advance(System_Entity *sys, Duration deltaTime);
// Clears all the entities. Also calls other systems
// to update these removals.
//
// Should load all other new entities and the player
// before any game logic after this, or this probably
// will crash. Sketchy.
void entity_Clear(System_Entity *sys);
2024-02-29 11:22:52 +08:00
#ifdef __cplusplus
}
#endif