Add knockback when attacking, fix animation state

This commit is contained in:
2024-02-09 18:50:18 +01:00
parent 484e5e9b39
commit b8da0a9f2d
4 changed files with 28 additions and 17 deletions

View File

@@ -135,18 +135,29 @@ Particle spawnParticle(const ParticleEmitter *emitter) {
};
}
void animationSetState(Animation *anim, AnimType type, bool playInFull) {
static void animationSetTextureRec(ecs_entity_t entity, Animation *anim, TextureRegion *texture) {
texture->rec = getTextureRect(anim->frame.frame);
if (ecs_has(ECS, entity, Owner)) {
Owner owner = *ecs_get(ECS, entity, Owner);
BzTileID base = anim->frame.frame;
Vector2 offset = getTileOffset(base, owner.player);
texture->rec.x += offset.x;
texture->rec.y += offset.y;
}
}
void animationSetState(ecs_entity_t entity, Animation *anim, TextureRegion *texture, AnimType type, bool playInFull) {
anim->animType = type;
anim->sequence = entityGetAnimationSequence(anim->entityType, type);
anim->curFrame = 0;
anim->elapsed = 0;
anim->playInFull = playInFull;
animationSetTextureRec(entity, anim, texture);
}
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;
if (anim[i].playInFull) continue;
ecs_entity_t entity = it->entities[i];
AnimType type = ANIM_IDLE;
if (ecs_has(ECS, entity, Velocity)) {
@@ -159,7 +170,7 @@ void updateAnimationState(ecs_iter_t *it) {
}
if (type != anim[i].animType) {
animationSetState(&anim[i], type, false);
animationSetState(entity, &anim[i], &text[i], type, false);
}
}
}
@@ -183,14 +194,7 @@ void updateAnimation(ecs_iter_t *it) {
anim[i].frame = entityGetAnimationFrame(anim[i].entityType, anim[i].animType, nextFrame);
anim[i].elapsed = 0.0f;
texture[i].rec = getTextureRect(anim[i].frame.frame);
if (ecs_has(ECS, entity, Owner)) {
Owner owner = *ecs_get(ECS, entity, Owner);
BzTileID base = anim[i].frame.frame;
Vector2 offset = getTileOffset(base, owner.player);
texture[i].rec.x += offset.x;
texture[i].rec.y += offset.y;
}
animationSetTextureRec(entity, &anim[i], &texture[i]);
}
}