feat: reworked typeclasses to forward-declare then define

forward declarations are simplified with decl_typeclass_impl
impl_Typeclass_for now instead only define
static inline impl_Typeclass_for can be used to achieve the old behaviour
This commit is contained in:
Sara 2024-01-12 09:02:42 +01:00
parent 760d9f2879
commit 0c6f1dd8cf
24 changed files with 174 additions and 119 deletions

View file

@ -20,7 +20,7 @@ typedef struct {
} Damagable;
#define impl_Damagable_for(T, damage_f)\
static inline Damagable T##_as_Damagable(T* x) {\
Damagable T##_as_Damagable(T* x) {\
TC_FN_TYPECHECK(int, damage_f, T*, DamageEventData*);\
static const IDamagable tc = {\
.damage = (int(*const)(void*, DamageEventData*)) damage_f,\

View file

@ -13,6 +13,31 @@ START_REFLECT(Enemy)
REFLECT_TYPECLASS(Enemy, Damagable)
END_REFLECT(Enemy)
impl_Transformable_for(Enemy,
EnemyGetTransform
)
impl_Drop_for(Enemy,
EnemyDestroy
)
impl_BehaviourEntity_for(Enemy,
EnemyStart,
EnemyUpdate,
EnemyDraw,
EnemyGetDepth
)
impl_PhysicsEntity_for(Enemy,
EnemyGetRigidBody,
EnemyOnCollision,
EnemyOnOverlap
)
impl_Damagable_for(Enemy,
EnemyDamage
)
Enemy* MakeEnemy() {
Enemy* self = malloc(sizeof(Enemy));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate Enemy");

View file

@ -13,6 +13,7 @@
#include "Damagable.h"
#include "EnemyStates.h"
#include "typeclass_helpers.h"
typedef struct Enemy {
Transform transform;
@ -56,29 +57,10 @@ extern int EnemyDamage(Enemy* self, DamageEventData* data);
DECL_REFLECT(Enemy)
impl_Transformable_for(Enemy,
EnemyGetTransform
)
impl_Drop_for(Enemy,
EnemyDestroy
)
impl_BehaviourEntity_for(Enemy,
EnemyStart,
EnemyUpdate,
EnemyDraw,
EnemyGetDepth
)
impl_PhysicsEntity_for(Enemy,
EnemyGetRigidBody,
EnemyOnCollision,
EnemyOnOverlap
)
impl_Damagable_for(Enemy,
EnemyDamage
)
decl_typeclass_impl(Transformable, Enemy)
decl_typeclass_impl(Drop, Enemy)
decl_typeclass_impl(BehaviourEntity, Enemy)
decl_typeclass_impl(PhysicsEntity, Enemy)
decl_typeclass_impl(Damagable, Enemy)
#endif // !FIGHT_ENEMY_H

View file

@ -15,6 +15,27 @@ START_REFLECT(Player)
REFLECT_TYPECLASS(Player, BehaviourEntity)
END_REFLECT(Player)
impl_Drop_for(Player,
DestroyPlayer
)
impl_BehaviourEntity_for(Player,
PlayerStart,
PlayerUpdate,
PlayerDraw,
PlayerGetDepth
)
impl_Transformable_for(Player,
PlayerGetTransform
)
impl_PhysicsEntity_for(Player,
PlayerGetRigidBody,
PlayerOnCollision,
PlayerOnOverlap
)
static inline
void Internal_PlayerInitInput(Player* self) {
playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new(

View file

@ -10,6 +10,7 @@
#include "player_input.h"
#include "rigidbody.h"
#include "collider.h"
#include "typeclass_helpers.h"
extern const Vector PLAYER_SPEED;
@ -61,25 +62,9 @@ static long PlayerGetDepth(Player* self) { return (int)(-10-self->transform.posi
DECL_REFLECT(Player);
impl_Drop_for(Player,
DestroyPlayer
)
impl_BehaviourEntity_for(Player,
PlayerStart,
PlayerUpdate,
PlayerDraw,
PlayerGetDepth
)
impl_Transformable_for(Player,
PlayerGetTransform
)
impl_PhysicsEntity_for(Player,
PlayerGetRigidBody,
PlayerOnCollision,
PlayerOnOverlap
)
decl_typeclass_impl(BehaviourEntity, Player)
decl_typeclass_impl(Drop, Player)
decl_typeclass_impl(Transformable, Player)
decl_typeclass_impl(PhysicsEntity, Player)
#endif // !FIGHT_PLAYER_H

View file

@ -10,6 +10,27 @@ START_REFLECT(Prop)
REFLECT_TYPECLASS(Prop, BehaviourEntity)
END_REFLECT(Prop)
impl_Transformable_for(Prop,
PropGetTransform
)
impl_PhysicsEntity_for(Prop,
PropGetRigidBody,
PropOnCollision,
PropOnOverlap
)
impl_Drop_for(Prop,
DestroyProp
)
impl_BehaviourEntity_for(Prop,
PropStart,
PropUpdate,
PropDraw,
PropGetDepth
)
Prop* MakeProp(Sprite* sprite, Shape* shape) {
Prop* self = malloc(sizeof(Prop));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for Prop instance");

View file

@ -9,6 +9,7 @@
#include "sprite.h"
#include "rigidbody.h"
#include "collider.h"
#include "typeclass_helpers.h"
typedef struct Prop {
Transform transform;
@ -35,25 +36,9 @@ static long PropGetDepth(Prop* self) { return -(int)(self->transform.position.y
DECL_REFLECT(Prop)
impl_Transformable_for(Prop,
PropGetTransform
)
impl_PhysicsEntity_for(Prop,
PropGetRigidBody,
PropOnCollision,
PropOnOverlap
)
impl_Drop_for(Prop,
DestroyProp
)
impl_BehaviourEntity_for(Prop,
PropStart,
PropUpdate,
PropDraw,
PropGetDepth
)
decl_typeclass_impl(Transformable, Prop)
decl_typeclass_impl(PhysicsEntity, Prop)
decl_typeclass_impl(Drop, Prop)
decl_typeclass_impl(BehaviourEntity, Prop)
#endif // !FIGHT_PROP_H