IsAnimationFinished function rather than a bool

This commit is contained in:
Joseph Ferano 2024-01-05 12:42:31 +07:00
parent 1dda2f9315
commit ba44c72d09
3 changed files with 9 additions and 14 deletions

6
main.c
View File

@ -162,7 +162,7 @@ void Update(GameState *game, Camera2D *cam, float dt) {
// Handle the attacking state, if not handle state transitions // Handle the attacking state, if not handle state transitions
if (knight->state == KNIGHT_ATTACKING) { if (knight->state == KNIGHT_ATTACKING) {
if (game->anim_playbacks[i].is_finished) { if (IsAnimationFinished(game->anim_playbacks[i])) {
knight->state = KNIGHT_IDLE; knight->state = KNIGHT_IDLE;
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]); PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
} }
@ -254,6 +254,8 @@ void Draw(const GameState *game, Assets assets, Camera2D cam, float dt) {
Vector2 world = GetScreenToWorld2D(GetMousePosition(), cam); Vector2 world = GetScreenToWorld2D(GetMousePosition(), cam);
Vector2 pointer_pos = Vector2Subtract(world, (Vector2){24, 19}); Vector2 pointer_pos = Vector2Subtract(world, (Vector2){24, 19});
D_DrawTextureV(assets.textures[2], pointer_pos, WHITE); D_DrawTextureV(assets.textures[2], pointer_pos, WHITE);
DrawFPS(1500, 10);
} }
int main(void) { int main(void) {
@ -281,7 +283,7 @@ int main(void) {
GameState game = {0}; GameState game = {0};
game.knights = malloc(sizeof(Knight) * MAX_KNIGHTS); game.knights = malloc(sizeof(Knight) * MAX_KNIGHTS);
game.knights[0] = (Knight){0}; game.knights[0] = (Knight){0};
game.knights[0].position = (Vector2){100,100}; game.knights[0].position = (Vector2){400,150};
game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_KNIGHTS); game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_KNIGHTS);
game.anim_playbacks_count = 1; game.anim_playbacks_count = 1;
// First one is idle // First one is idle

View File

@ -5,7 +5,6 @@ inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimation
playback->time_elapsed = 0.0f; playback->time_elapsed = 0.0f;
playback->current_frame = 0; playback->current_frame = 0;
playback->row = animation; playback->row = animation;
playback->is_finished = false;
playback->total_frames = anims[animation].total_frames; playback->total_frames = anims[animation].total_frames;
playback->loop = anims[animation].loop; playback->loop = anims[animation].loop;
} }
@ -21,11 +20,12 @@ void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
if (playback->current_frame >= playback->total_frames) { if (playback->current_frame >= playback->total_frames) {
if (playback->loop) { if (playback->loop) {
playback->current_frame = 0; playback->current_frame = 0;
} else {
playback->is_finished = true;
playback->current_frame--;
} }
} }
} }
} }
} }
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
return playback.current_frame == playback.total_frames;
}

View File

@ -4,12 +4,6 @@
#include "lib.h" #include "lib.h"
#include "include/raylib.h" #include "include/raylib.h"
// typedef struct {
// Texture2D texture;
// char *name;
// Size size;
// } SpriteSheet;
typedef struct { typedef struct {
u8 loop; u8 loop;
u8 total_frames; u8 total_frames;
@ -21,9 +15,8 @@ typedef struct {
u8 total_frames; u8 total_frames;
u8 current_frame; u8 current_frame;
u8 loop; u8 loop;
// TODO: Maybe we can get rid of this one
u8 is_finished;
} SpriteAnimationPlayback; } SpriteAnimationPlayback;
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback); void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len); void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len);
bool IsAnimationFinished(SpriteAnimationPlayback playback);