feat: knockback is now implemented using velocity and drag

This commit is contained in:
Sara 2024-01-19 18:27:35 +01:00
parent 25badcb847
commit 27fbda2c6d
3 changed files with 17 additions and 8 deletions

View file

@ -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;
}
}

View file

@ -28,6 +28,7 @@ typedef struct Enemy {
int facing;
DamageEventData last_damage;
float stun_time;
AnimationSprite* idleAnim;

View file

@ -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();
else
return EnemyHurt();
}