From a27acee1551b2b75a468b4fae9f2dee0fe393ffa Mon Sep 17 00:00:00 2001 From: Sara Date: Sat, 20 Jan 2024 12:55:17 +0100 Subject: [PATCH] feat: drag for enemy is now calculated in EnemyUpdate instead of separately in the states --- game/src/Enemy.c | 6 +++++- game/src/EnemyStates.c | 6 ------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/game/src/Enemy.c b/game/src/Enemy.c index a741c3e..c98ef26 100644 --- a/game/src/Enemy.c +++ b/game/src/Enemy.c @@ -95,6 +95,10 @@ Enemy* SpawnEnemy(Vector location, const State* entryState) { void EnemyStart(Enemy* self) {} void EnemyUpdate(Enemy* self, float deltaTime) { + // apply drag + Vector velocity = vmovetowardsf(rigidbody_get_velocity(self->rigidbody), ZeroVector, 0.1f); + rigidbody_set_velocity(self->rigidbody, velocity); + // update state machine state_machine_update(self->behaviour, deltaTime); } @@ -142,7 +146,7 @@ int EnemyDamage(Enemy* self, DamageEventData* data) { // get stunned self->stun_time = game_time() + data->stun; // calculate which direction the damage is coming from - float direction = ((data->origin.x - self->transform.position.x) > 0) * 2 - 1; + const float direction = (data->origin.x - self->transform.position.x) >= 0.0f ? 1.0f : -1.0f; // face the direction damage is coming from self->facing = direction; // add knockback according to damage data in the calculated direction diff --git a/game/src/EnemyStates.c b/game/src/EnemyStates.c index ceafefa..db9028d 100644 --- a/game/src/EnemyStates.c +++ b/game/src/EnemyStates.c @@ -11,9 +11,6 @@ void EnemyIdle_Enter(Enemy* self) { } const State* EnemyIdle_Update(Enemy* self, float deltaTime) { - // apply drag - Vector velocity = vmovetowardsf(rigidbody_get_velocity(self->rigidbody), ZeroVector, 0.1f); - rigidbody_set_velocity(self->rigidbody, velocity); // state transitions if(self->stun_time >= game_time()) return EnemyHurt(); @@ -36,9 +33,6 @@ void EnemyHurt_Enter(Enemy* self) { const State* EnemyHurt_Update(Enemy* self, float deltaTime) { const float time = animation_sprite_get_time(self->currentAnimation); - // apply drag - Vector velocity = vmovetowardsf(rigidbody_get_velocity(self->rigidbody), ZeroVector, 0.1f); - rigidbody_set_velocity(self->rigidbody, velocity); // state transitions if(self->stun_time < game_time()) return EnemyIdle();