feat: enemy now looks at player when hit

This commit is contained in:
Sara 2023-12-01 21:44:30 +01:00
parent c2229a5bba
commit 9fbbd833a5
2 changed files with 9 additions and 2 deletions

View file

@ -1,9 +1,11 @@
#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"
Enemy* MakeEnemy() {
Enemy* self = malloc(sizeof(Enemy));
@ -15,6 +17,7 @@ Enemy* MakeEnemy() {
.collider = NULL,
.sprite = sprite_new_empty(),
.hurt = 0,
.facing = 1,
.idleAnim = NULL,
.walkAnim = NULL,
.hurtAnim = NULL,
@ -37,7 +40,7 @@ Enemy* MakeEnemy() {
Enemy* SpawnEnemy(Vector location, const State* entryState) {
Enemy* self = MakeEnemy();
self->behaviour = state_machine_init(self, entryState);
self->transform.position = location;
rigidbody_get_transform(self->rigidbody)->position = location;
game_world_add_entity(Enemy_as_BehaviourEntity(self));
physics_world_add_entity(Enemy_as_PhysicsEntity(self));
return self;
@ -50,6 +53,7 @@ void EnemyUpdate(Enemy* self, float deltaTime) {
}
void EnemyDraw(Enemy* self) {
sprite_flip_horizontal(self->sprite, self->facing == -1);
animation_sprite_draw(self->currentAnimation, &self->transform);
}
@ -67,10 +71,12 @@ 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 -= (int)data;
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;

View file

@ -22,6 +22,7 @@ typedef struct Enemy {
Sprite* sprite;
int hurt;
int facing;
AnimationSprite* idleAnim;
AnimationSprite* walkAnim;