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

@@ -1,6 +1,7 @@
#include "systems.h"
#include "../game_state.h"
#include "../utils.h"
#include <raymath.h>
#include <stdlib.h>
@@ -68,11 +69,6 @@ bool updateParticle(const Texture2D tex, Particle *particle, f32 dt) {
particle->elapsed += dt;
return alpha >= 1.0f;
}
// https://stackoverflow.com/questions/13408990/how-to-generate-random-float-number-in-c
static inline f32 randFloatRange(f32 min, f32 max) {
float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
return min + scale * ( max - min ); /* [min, max] */
}
static inline Vector2 randVector2Range(Vector2 from, Vector2 to) {
return (Vector2) {
randFloatRange(from.x, to.x),
@@ -107,10 +103,18 @@ Particle spawnParticle(const ParticleEmitter *emitter) {
};
}
void animationSetState(Animation *anim, AnimType type, bool playInFull) {
anim->animType = type;
anim->sequence = entityGetAnimationSequence(anim->entityType, type);
anim->curFrame = 0;
anim->elapsed = 0;
anim->playInFull = playInFull;
}
void updateAnimationState(ecs_iter_t *it) {
Animation *anim = ecs_field(it, Animation, 1);
TextureRegion *text = ecs_field(it, TextureRegion, 2);
for (i32 i = 0; i < it->count; i++) {
if (anim->playInFull) continue;
ecs_entity_t entity = it->entities[i];
AnimType type = ANIM_IDLE;
if (ecs_has(ECS, entity, Velocity)) {
@@ -123,10 +127,7 @@ void updateAnimationState(ecs_iter_t *it) {
}
if (type != anim[i].animType) {
anim[i].animType = type;
anim[i].sequence = entityGetAnimationSequence(anim[i].entityType, type);
anim[i].curFrame = 0;
anim[i].elapsed = 0;
animationSetState(&anim[i], type, false);
}
}
}
@@ -145,6 +146,7 @@ void updateAnimation(ecs_iter_t *it) {
if (anim[i].elapsed < frame.duration) continue;
i32 nextFrame = (anim[i].curFrame + 1) % seq.frameCount;
if (nextFrame == 0) anim[i].playInFull = false;
anim[i].curFrame = nextFrame;
anim[i].frame = entityGetAnimationFrame(anim[i].entityType, anim[i].animType, nextFrame);
anim[i].elapsed = 0.0f;