Add hurt, die animation when taking damage

This commit is contained in:
2024-02-07 17:14:17 +01:00
parent d6466b8f55
commit 6c1d0dfdb2
16 changed files with 251 additions and 24 deletions

View File

@@ -5,6 +5,7 @@
#include "../input.h"
#include "../pathfinding.h"
#include "../entity_factory.h"
#include "../utils.h"
#include <math.h>
#include <raymath.h>
@@ -144,16 +145,22 @@ void entityUpdateKinematic(ecs_iter_t *it) {
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
}
}
void entityUpdatePhysics(ecs_iter_t *it) {
void entityUpdate(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *position = ecs_field(it, Position, 1);
HitBox *hitbox = ecs_field(it, HitBox, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 4);
Unit *unit = ecs_field(it, Unit, 4);
Owner *owner = ecs_field(it, Owner, 5);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 6);
f32 dt = it->delta_time;
for (i32 i = 0; i < it->count; i++) {
// Attack thingies
unit[i].attackElapsed += dt;
bool canAttack = unit[i].attackElapsed > unit[i].attackCooldown;
// Only update "stationary" entities
bool stationary = Vector2Length(velocity[i]) <= 0.2f;
Rectangle bounds = entityTransformHitBox(position[i], hitbox[i]);
@@ -173,9 +180,35 @@ void entityUpdatePhysics(ecs_iter_t *it) {
if (!CheckCollisionRecs(bounds, otherBounds)) {
continue;
}
// Attack update
if (canAttack && ecs_has(ECS, other, Health) && ecs_has(ECS, other, Owner)) {
Health *otherHealth = ecs_get_mut(ECS, other, Health);
Player otherPlayer = ecs_get(ECS, other, Owner)->player;
if (otherPlayer != owner[i].player) {
Rectangle collisionRec = GetCollisionRec(bounds, otherBounds);
f32 percentageCovered = (collisionRec.width * collisionRec.height) / (bounds.width * bounds.height);
f32 dealDmg = randFloatRange(unit[i].minDamage, unit[i].maxDamage);
f32 multiplier = 1.0f + percentageCovered;
multiplier = Clamp(multiplier, 0.8f, 1.6f);
dealDmg *= multiplier;
damageEvent(other, (DamageEvent) {
.amount = dealDmg
});
canAttack = false;
unit[i].attackElapsed = 0.0f;
}
}
// Physics update
slowDown += 0.1f;
Position dif = Vector2Subtract(otherPos, position[i]);
//dif = Vector2Normalize(dif);
dir = Vector2Add(dir, dif);
}
@@ -270,7 +303,7 @@ void updateBuildingRecruitment(ecs_iter_t *it) {
}
}
void renderHealth(ecs_iter_t *it) {
void renderHealthBar(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
HitBox *hitbox = ecs_field(it, HitBox, 2);
Health *health = ecs_field(it, Health, 3);