feat: replaced messagereceiver with mirror

This commit is contained in:
Sara 2024-01-11 23:27:45 +01:00
parent be64a55588
commit bf23ff877a
16 changed files with 236 additions and 116 deletions

31
game/src/Damagable.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef FIGHT_DAMAGABLE_H
#define FIGHT_DAMAGABLE_H
#include "typeclass_helpers.h"
#include "vmath.h"
typedef struct DamageEventData {
int damageAmount;
Vector origin;
} DamageEventData;
typedef struct {
int (*const damage)(void*, DamageEventData*);
} IDamagable;
typedef struct {
void* data;
IDamagable const* tc;
} Damagable;
#define impl_Damagable_for(T, damage_f)\
static inline 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,\
};\
return (Damagable){.data = x, .tc = &tc};\
}
#endif // !FIGHT_DAMAGABLE_H

View file

@ -1,11 +0,0 @@
#ifndef FIGHT_DAMAGE_EVENT_H
#define FIGHT_DAMAGE_EVENT_H
#include "vmath.h"
typedef struct DamageEventData {
int damageAmount;
Vector origin;
} DamageEventData;
#endif // !FIGHT_DAMAGE_EVENT_H

View file

@ -1,12 +1,18 @@
#include "Enemy.h"
#include "debug.h"
#include "Messages.h"
#include "DamageEvent.h"
#include "game_world.h"
#include "physics.h"
#include "physics_world.h"
#include "sprite.h"
START_REFLECT(Enemy)
REFLECT_TYPECLASS(Enemy, Transformable)
REFLECT_TYPECLASS(Enemy, Drop)
REFLECT_TYPECLASS(Enemy, PhysicsEntity)
REFLECT_TYPECLASS(Enemy, BehaviourEntity)
REFLECT_TYPECLASS(Enemy, Damagable)
END_REFLECT(Enemy)
Enemy* MakeEnemy() {
Enemy* self = malloc(sizeof(Enemy));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate Enemy");
@ -70,23 +76,6 @@ void EnemyDestroy(Enemy* self) {
void EnemyOnCollision(Enemy* self, Collision collision) {}
void EnemyOnOverlap(Enemy* self, Collider* other) {}
void* EnemyHandleMessage(Enemy* self, MessageID id, uintptr_t data) {
DamageEventData* damage = (DamageEventData*)data;
switch(id) {
case MESSAGE_DEAL_DAMAGE:
self->health -= damage->damageAmount;
self->hurt = 1;
self->facing = ((damage->origin.x - self->transform.position.x) > 0) * 2 - 1;
if(self->health <= 0)
game_world_destroy_entity(Enemy_as_BehaviourEntity(self));
break;
default:
break;
}
return NULL;
}
Transform* EnemyGetTransform(Enemy* self) {
return &self->transform;
}
@ -94,3 +83,16 @@ Transform* EnemyGetTransform(Enemy* self) {
RigidBody* EnemyGetRigidBody(Enemy* self) {
return self->rigidbody;
}
int EnemyDamage(Enemy* self, DamageEventData* data) {
self->health -= data->damageAmount;
LOG_INFO("Damage received");
if(self->health <= 0) {
game_world_destroy_entity(Enemy_as_BehaviourEntity(self));
return 1;
} else {
self->hurt = 1;
self->facing = ((data->origin.x - self->transform.position.x) > 0) * 2 - 1;
return 0;
}
}

View file

@ -1,6 +1,7 @@
#ifndef FIGHT_ENEMY_H
#define FIGHT_ENEMY_H
#include "mirror.h"
#include "transform.h"
#include "state_machine.h"
#include "rigidbody.h"
@ -9,6 +10,8 @@
#include "collider.h"
#include "sprite.h"
#include "animation_sprite.h"
#include "Damagable.h"
#include "EnemyStates.h"
typedef struct Enemy {
@ -44,20 +47,19 @@ extern void EnemyDraw(Enemy* self);
extern void EnemyOnCollision(Enemy* self, Collision collision);
extern void EnemyOnOverlap(Enemy* self, Collider* other);
extern void* EnemyHandleMessage(Enemy* self, MessageID id, uintptr_t data);
extern Transform* EnemyGetTransform(Enemy* self);
extern RigidBody* EnemyGetRigidBody(Enemy* self);
static long EnemyGetDepth(Enemy* self) { return (long)(-self->transform.position.y * 1000); }
extern int EnemyDamage(Enemy* self, DamageEventData* data);
DECL_REFLECT(Enemy)
impl_Transformable_for(Enemy,
EnemyGetTransform
)
impl_MessageReceiver_for(Enemy,
EnemyHandleMessage
)
impl_Drop_for(Enemy,
EnemyDestroy
)
@ -75,4 +77,8 @@ impl_PhysicsEntity_for(Enemy,
EnemyOnOverlap
)
impl_Damagable_for(Enemy,
EnemyDamage
)
#endif // !FIGHT_ENEMY_H

View file

@ -1,24 +1,31 @@
#include "Player.h"
#include "PlayerStates.h"
#include "Layers.h"
#include "debug.h"
#include "game_world.h"
#include "input_axis.h"
#include "physics_world.h"
#include "PlayerStates.h"
#include "Layers.h"
const Vector PLAYER_SPEED = { 1.0f, 0.70f };
START_REFLECT(Player)
REFLECT_TYPECLASS(Player, Drop)
REFLECT_TYPECLASS(Player, PhysicsEntity)
REFLECT_TYPECLASS(Player, BehaviourEntity)
END_REFLECT(Player)
static inline
void Internal_PlayerInitInput(Player* self) {
playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new(
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_A)),
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_D)),
InputEvent_Float)), (InputDelegateFn)PlayerHorizontalInput);
InputEvent_Float)), (InputDelegateFn)PlayerHorizontalInput);
playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new(
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_S)),
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_W)),
InputEvent_Float)), (InputDelegateFn)PlayerVerticalInput);
InputEvent_Float)), (InputDelegateFn)PlayerVerticalInput);
playerinput_add(self->playerInput, KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)), (InputDelegateFn)PlayerAttackInput);
}

View file

@ -2,10 +2,9 @@
#define FIGHT_PLAYER_H
#include "state_machine.h"
#include "state.h"
#include "mirror.h"
#include "behaviour_entity.h"
#include "animation_sprite.h"
#include "input.h"
#include "vmath.h"
#include "transform.h"
#include "player_input.h"
@ -60,7 +59,7 @@ void PlayerOnOverlap(Player* self, Collider* other);
static long PlayerGetDepth(Player* self) { return (int)(-10-self->transform.position.y * 1000); }
impl_no_MessageReceiver_for(Player)
DECL_REFLECT(Player);
impl_Drop_for(Player,
DestroyPlayer

View file

@ -1,8 +1,10 @@
#include "PlayerStates.h"
#include "physics_world.h"
#include "DamageEvent.h"
#include "Damagable.h"
#include "Player.h"
#include "Layers.h"
#include "physics_world.h"
static inline
void InternalSpriteFlipWithMovement(Player* self) {
@ -58,11 +60,15 @@ void PlayerAttackTrigger(Player* self) {
MakeVector(0.1f, 0.06f), PHYSICS_LAYER_COMBAT, self->rigidbody);
if(found != NULL) {
PhysicsEntity entity = collider_get_owner(found);
DamageEventData data = {
.damageAmount = 1,
.origin = self->transform.position
};
entity.message_receiver->handle_message(entity.data, 1, (uintptr_t)&data);
Damagable(*const as_damagable)(void*) = mirror_get_converter(entity.data, entity.mirror, "Damagable");
if(as_damagable) {
Damagable damagable = as_damagable(entity.data);
DamageEventData data = {
.damageAmount = 1,
.origin = self->transform.position
};
damagable.tc->damage(entity.data, &data);
}
}
++self->animationTriggers;
}

View file

@ -1,10 +1,16 @@
#include "Prop.h"
#include "DamageEvent.h"
#include "Messages.h"
#include "debug.h"
#include "game_world.h"
#include "mirror.h"
#include "physics_world.h"
START_REFLECT(Prop)
REFLECT_TYPECLASS(Prop, Transformable)
REFLECT_TYPECLASS(Prop, Drop)
REFLECT_TYPECLASS(Prop, BehaviourEntity)
END_REFLECT(Prop)
Prop* MakeProp(Sprite* sprite, Shape* shape) {
Prop* self = malloc(sizeof(Prop));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for Prop instance");
@ -61,11 +67,3 @@ Transform* PropGetTransform(Prop* self) {
RigidBody* PropGetRigidBody(Prop* self) {
return self->rigidbody;
}
void* PropReceiveMessage(Prop* self, MessageID message, uintptr_t data) {
DamageEventData* damage = (DamageEventData*)data;
if(message == MESSAGE_DEAL_DAMAGE) {
LOG_INFO("Punching bag took %d damage", damage->damageAmount);
}
return 0;
}

View file

@ -31,13 +31,9 @@ void PropOnOverlap(Prop* self, Collider* other);
Transform* PropGetTransform(Prop* self);
RigidBody* PropGetRigidBody(Prop* self);
void* PropReceiveMessage(Prop* self, MessageID message, uintptr_t data);
static long PropGetDepth(Prop* self) { return -(int)(self->transform.position.y * 1000); }
impl_MessageReceiver_for(Prop,
PropReceiveMessage
)
DECL_REFLECT(Prop)
impl_Transformable_for(Prop,
PropGetTransform