JacksEscape/Physics_Component.h

77 lines
1.5 KiB
C
Raw Normal View History

2024-02-29 11:22:52 +08:00
#pragma once
2024-02-29 16:17:55 +08:00
#include "util/tree.h"
#include "util/vector.h"
2024-02-29 11:22:52 +08:00
#include "Types.h"
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
2024-02-29 16:17:55 +08:00
typedef struct _Entity Entity;
extern double physics_Gravity;
2024-02-29 11:22:52 +08:00
2024-03-01 14:37:59 +08:00
// Position component.
//
// If not exist, it's like it's at (0,0)
2024-02-29 11:22:52 +08:00
typedef struct {
Entity *super;
2024-02-29 16:17:55 +08:00
Vec2 position;
Vec2 velocity;
2024-02-29 11:22:52 +08:00
} Component_Position;
2024-03-01 14:37:59 +08:00
// Handler called when hitboxes are hit
2024-03-01 17:09:24 +08:00
typedef void (*physics_HitHandler)(Entity *me, Entity *other, Vec2 triedDelta, void *data);
2024-03-01 14:37:59 +08:00
2024-02-29 11:22:52 +08:00
// Box is relative to Position if exists
// if not, Box is absolute
//
// Moving hitboxes only hits fixed hitboxes
typedef struct {
Entity *super;
2024-02-29 16:17:55 +08:00
Box2 box;
bool fixed;
2024-03-01 14:37:59 +08:00
physics_HitHandler onHit;
2024-03-01 17:09:24 +08:00
void *onHitData;
2024-02-29 11:22:52 +08:00
} Component_Hitbox;
2024-02-29 16:51:46 +08:00
// Returns the absolute version of the hitbox.
Box2 physics_HitboxAbsolute(Component_Hitbox *hitbox);
2024-02-29 11:22:52 +08:00
2024-03-01 17:09:24 +08:00
typedef struct _App App;
2024-02-29 16:17:55 +08:00
// Physics manager
typedef struct {
2024-03-01 17:09:24 +08:00
App *super;
2024-02-29 16:17:55 +08:00
// Every Position & Hitbox components
tree_Tree *pos, *hit;
vector_Vector *flaggedDelete;
} System_Physics;
// Returns an empty physics system.
2024-03-01 17:09:24 +08:00
System_Physics *physics_NewSystem(App *super);
2024-02-29 16:17:55 +08:00
// Frees a physics system.
void physics_DeleteSystem(System_Physics *sys);
// Adds an entity.
void physics_AddEntity(System_Physics *sys, Entity *e);
// Deletes an entity.
void physics_DeleteEntity(System_Physics *sys, uintptr_t id);
// Advance is called on every frame.
void physics_Advance(System_Physics *sys, Duration deltaTime);
2024-02-29 11:22:52 +08:00
#ifdef __cplusplus
}
#endif