diff --git a/game/src/Enemy.c b/game/src/Enemy.c index b44dd51..0711beb 100644 --- a/game/src/Enemy.c +++ b/game/src/Enemy.c @@ -4,6 +4,7 @@ #include "game_world.h" #include "physics.h" #include "physics_world.h" +#include "program.h" #include "sprite.h" START_REFLECT(Enemy) @@ -92,8 +93,6 @@ void EnemyStart(Enemy* self) {} void EnemyUpdate(Enemy* self, float deltaTime) { state_machine_update(self->behaviour, deltaTime); - if(self->stun_time > 0.0f) - self->stun_time -= deltaTime; } void EnemyDraw(Enemy* self) { @@ -123,7 +122,7 @@ RigidBody* EnemyGetRigidBody(Enemy* self) { } int EnemyDamage(Enemy* self, DamageEventData* data) { - if(self->stun_time > 0.f) + if(self->stun_time > game_time()) return 0; self->health -= data->damageAmount; LOG_INFO("Damage received"); @@ -131,10 +130,13 @@ int EnemyDamage(Enemy* self, DamageEventData* data) { game_world_destroy_entity(Enemy_as_BehaviourEntity(self)); return 1; } else { - self->stun_time = data->stun; + self->last_damage = *data; + const float stun_time = game_time() + data->stun; + if(stun_time > self->stun_time) + self->stun_time = stun_time; float direction = ((data->origin.x - self->transform.position.x) > 0) * 2 - 1; self->facing = direction; - rigidbody_get_transform(self->rigidbody)->position = vaddf(self->transform.position, MakeVector(-direction * data->knockback, 0)); + rigidbody_add_impulse(self->rigidbody, MakeVector(-direction * data->knockback, 0), 0); return 0; } } diff --git a/game/src/Enemy.h b/game/src/Enemy.h index af36694..4e683dc 100644 --- a/game/src/Enemy.h +++ b/game/src/Enemy.h @@ -28,6 +28,7 @@ typedef struct Enemy { int facing; + DamageEventData last_damage; float stun_time; AnimationSprite* idleAnim; diff --git a/game/src/EnemyStates.c b/game/src/EnemyStates.c index 87559b7..2db53b5 100644 --- a/game/src/EnemyStates.c +++ b/game/src/EnemyStates.c @@ -1,6 +1,7 @@ #include "EnemyStates.h" #include "Enemy.h" #include "animation_sprite.h" +#include "program.h" void EnemyState_Exit(Enemy* self) {} @@ -10,7 +11,9 @@ void EnemyIdle_Enter(Enemy* self) { } const State* EnemyIdle_Update(Enemy* self, float deltaTime) { - if(self->stun_time > 0.f) + Vector velocity = vmovetowardsf(rigidbody_get_velocity(self->rigidbody), ZeroVector, 0.1f); + rigidbody_set_velocity(self->rigidbody, velocity); + if(self->stun_time >= game_time()) return EnemyHurt(); return EnemyIdle(); } @@ -31,7 +34,10 @@ void EnemyHurt_Enter(Enemy* self) { const State* EnemyHurt_Update(Enemy* self, float deltaTime) { const float time = animation_sprite_get_time(self->currentAnimation); - if(self->stun_time < 0.f) + Vector velocity = vmovetowardsf(rigidbody_get_velocity(self->rigidbody), ZeroVector, 0.1f); + rigidbody_set_velocity(self->rigidbody, velocity); + if(self->stun_time < game_time()) return EnemyIdle(); - return EnemyHurt(); + else + return EnemyHurt(); }