From 39625789e2a22f4a05536522ac582810566d1f17 Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 6 Oct 2023 23:46:27 +0200 Subject: [PATCH] added PhysicsEntity trait describing an object containing a rigidbody, shape and transform --- src/physics_entity.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/physics_entity.h diff --git a/src/physics_entity.h b/src/physics_entity.h new file mode 100644 index 0000000..ea0abf9 --- /dev/null +++ b/src/physics_entity.h @@ -0,0 +1,33 @@ +#ifndef _fencer_collidable_h +#define _fencer_collidable_h + +#include "vmath.h" +#include "typeclass_helpers.h" + +#include "rigidbody.h" +#include "shape.h" + +typedef struct { + RigidBody* (*const get_rigidbody)(void* self); + Shape* (*const get_shape)(void* self); + Transformable (*const as_transformable)(void* self); +} IPhysicsEntity; + +typedef struct { + void* self; + IPhysicsEntity const* tc; +} PhysicsEntity; + +#define impl_PhysicsEntity_for(T, get_rigidbody_f, get_shape_f)\ +static inline PhysicsEntity T##_as_PhysicsEntity(T* x) {\ + TC_FN_TYPECHECK(RigidBody*, get_rigidbody_f, T* e);\ + TC_FN_TYPECHECK(Shape*, get_shape_f, T* e);\ + static IPhysicsEntity const tc = {\ + .get_rigidbody = (RigidBody*(*const)(void*)) get_transform_f,\ + .get_shape = (Shape*(*const)(void*)) get_shape_f,\ + .as_transformable = T##_as_Transformable\ + };\ + return (PhysicsEntity){.tc = &interface, .self = x};\ +} + +#endif // !_fencer_collidable_h