feat(animation): animation sprite loop mode implemented
This commit is contained in:
parent
05334eb8ee
commit
c1a043c4a7
|
@ -12,13 +12,13 @@ struct AnimationSprite {
|
|||
float start_time;
|
||||
};
|
||||
|
||||
AnimationSprite* animation_sprite_new(Sprite* target_sprite, Spritesheet* sheet, float framerate) {
|
||||
AnimationSprite* animation_sprite_new(Sprite* target_sprite, Spritesheet* sheet, float framerate, AnimationSpriteLoopMode loop_mode) {
|
||||
AnimationSprite* self = malloc(sizeof(AnimationSprite));
|
||||
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate memory for AnimationSprite");
|
||||
*self = (AnimationSprite){
|
||||
.sheet = sheet,
|
||||
.frame_interval = 1.0f / framerate,
|
||||
.loop_mode = LoopMode_Stop,
|
||||
.loop_mode = loop_mode,
|
||||
.start_time = game_time(),
|
||||
.sprite_target = target_sprite
|
||||
};
|
||||
|
@ -36,8 +36,30 @@ void animation_sprite_play_from(AnimationSprite* self, float normalized_time) {
|
|||
}
|
||||
|
||||
void animation_sprite_draw(AnimationSprite* self, Transform* transform) {
|
||||
const size_t frame_count = spritesheet_get_tile_count(self->sheet);
|
||||
const float time = game_time() - self->start_time;
|
||||
const size_t frame = time / self->frame_interval;
|
||||
|
||||
size_t frame = time / self->frame_interval;
|
||||
|
||||
switch(self->loop_mode) {
|
||||
case LoopMode_Hide:
|
||||
if(frame >= frame_count)
|
||||
return;
|
||||
else
|
||||
break;
|
||||
case LoopMode_Loop:
|
||||
frame %= frame_count;
|
||||
break;
|
||||
case LoopMode_PingPong:
|
||||
frame %= frame_count * 2;
|
||||
if(frame >= frame_count)
|
||||
frame = frame_count - (frame - frame_count);
|
||||
return;
|
||||
case LoopMode_Stop:
|
||||
if(frame >= frame_count)
|
||||
frame = frame_count - 1;
|
||||
}
|
||||
|
||||
sprite_set_spritesheet(self->sprite_target, self->sheet);
|
||||
sprite_set_tile(self->sprite_target, frame);
|
||||
sprite_draw(self->sprite_target, *transform);
|
||||
|
|
|
@ -13,7 +13,7 @@ typedef enum AnimationSpriteLoopMode {
|
|||
|
||||
typedef struct AnimationSprite AnimationSprite;
|
||||
|
||||
extern AnimationSprite* animation_sprite_new(Sprite* target_sprite, Spritesheet* sheet, float framerate);
|
||||
extern AnimationSprite* animation_sprite_new(Sprite* target_sprite, Spritesheet* sheet, float framerate, AnimationSpriteLoopMode loop_mode);
|
||||
extern void animation_sprite_destroy(AnimationSprite* self);
|
||||
|
||||
extern void animation_sprite_play_from(AnimationSprite* self, float normalized_time);
|
||||
|
|
Loading…
Reference in a new issue