Compare commits

..

2 Commits

Author SHA1 Message Date
En Yi 332fe33e8e Prevent crashing when no sprites is supplied 2023-05-27 17:01:20 +08:00
En Yi 542c7cf57c Add crouch sprite transition 2023-05-27 16:39:17 +08:00
3 changed files with 16 additions and 15 deletions

View File

@ -24,7 +24,7 @@
#define PLAYER_WIDTH 30 #define PLAYER_WIDTH 30
#define PLAYER_HEIGHT 42 #define PLAYER_HEIGHT 42
#define PLAYER_C_WIDTH 30 #define PLAYER_C_WIDTH 30
#define PLAYER_C_HEIGHT 30 #define PLAYER_C_HEIGHT 26
#else #else
#define PLAYER_WIDTH 14 #define PLAYER_WIDTH 14
#define PLAYER_HEIGHT 30 #define PLAYER_HEIGHT 30

View File

@ -1067,17 +1067,21 @@ void sprite_animation_system(Scene_t* scene)
{ {
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
// Update animation state // Update animation state
unsigned int next_idx = p_cspr->current_idx;
if (p_cspr->transition_func != NULL) if (p_cspr->transition_func != NULL)
{ {
unsigned int spr_idx = p_cspr->transition_func(p_ent); next_idx = p_cspr->transition_func(p_ent);
if (p_cspr->current_idx != spr_idx)
{
p_cspr->current_idx = spr_idx;
p_cspr->sprites[spr_idx].sprite->current_frame = 0;
}
} }
if (p_cspr->pause) return; if (p_cspr->pause) return;
bool reset = p_cspr->current_idx != next_idx;
p_cspr->current_idx = next_idx;
SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx]; SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
if (spr.sprite == NULL) continue;
if (reset) spr.sprite->current_frame = 0;
// Animate it (handle frame count) // Animate it (handle frame count)
spr.sprite->elapsed++; spr.sprite->elapsed++;
if (spr.sprite->elapsed == spr.sprite->speed) if (spr.sprite->elapsed == spr.sprite->speed)

View File

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include "raymath.h" #include "raymath.h"
#define N_PLAYER_SPRITES 7 #define N_PLAYER_SPRITES 8
enum PlayerSpriteEnum enum PlayerSpriteEnum
{ {
SPR_PLAYER_STAND = 0, SPR_PLAYER_STAND = 0,
@ -13,6 +13,7 @@ enum PlayerSpriteEnum
SPR_PLAYER_FALL, SPR_PLAYER_FALL,
SPR_PLAYER_LADDER, SPR_PLAYER_LADDER,
SPR_PLAYER_CROUCH, SPR_PLAYER_CROUCH,
SPR_PLAYER_CRMOVE,
SPR_PLAYER_SWIM, SPR_PLAYER_SWIM,
}; };
@ -31,17 +32,13 @@ static unsigned int player_sprite_transition_func(Entity_t* ent)
if (p_move->ground_state & 1) if (p_move->ground_state & 1)
{ {
if (p_plr->is_crouch) if (Vector2LengthSqr(p_ctrans->velocity) > 1000.0f)
{ {
return SPR_PLAYER_CROUCH; return (p_plr->is_crouch) ? SPR_PLAYER_CRMOVE : SPR_PLAYER_RUN;
} }
else else
{ {
if (Vector2LengthSqr(p_ctrans->velocity) > 1000.0f) return (p_plr->is_crouch) ? SPR_PLAYER_CROUCH : SPR_PLAYER_STAND;
{
return SPR_PLAYER_RUN;
}
return SPR_PLAYER_STAND;
} }
} }
else if (p_plr->ladder_state) else if (p_plr->ladder_state)