character: support non-repeating animations (stopping at the last frame)

This commit is contained in:
Sebastian Krzyszkowiak 2018-04-13 03:00:54 +02:00
parent 4240b47380
commit c3adf398b1
2 changed files with 20 additions and 10 deletions

View file

@ -182,7 +182,7 @@ SYMBOL_EXPORT void RegisterSpritesheet(struct Game* game, struct Character* char
s->width = 0; s->width = 0;
s->height = 0; s->height = 0;
s->repeats = strtolnull(al_get_config_value(config, "animation", "repeats"), 0); s->repeats = strtolnull(al_get_config_value(config, "animation", "repeats"), -1);
s->successor = NULL; s->successor = NULL;
const char* successor = al_get_config_value(config, "animation", "successor"); const char* successor = al_get_config_value(config, "animation", "successor");
@ -278,7 +278,7 @@ SYMBOL_EXPORT struct Character* CreateCharacter(struct Game* game, char* name) {
character->flipX = false; character->flipX = false;
character->flipY = false; character->flipY = false;
character->shared = false; character->shared = false;
character->repeats = 0; character->repeats = -1;
character->reversing = false; character->reversing = false;
character->parent = NULL; character->parent = NULL;
character->hidden = false; character->hidden = false;
@ -369,12 +369,8 @@ SYMBOL_EXPORT void AnimateCharacter(struct Game* game, struct Character* charact
} }
} }
if (character->spritesheet->frameCount == 1) {
character->pos = 0;
}
if (reachedEnd) { if (reachedEnd) {
if (character->repeats) { if (character->repeats > 0) {
character->repeats--; character->repeats--;
if (character->callback) { if (character->callback) {
character->callback(game, character, NULL, character->spritesheet->name, character->callbackData); character->callback(game, character, NULL, character->spritesheet->name, character->callbackData);
@ -386,17 +382,31 @@ SYMBOL_EXPORT void AnimateCharacter(struct Game* game, struct Character* charact
if (character->callback) { if (character->callback) {
character->callback(game, character, character->spritesheet->name, old, character->callbackData); character->callback(game, character, character->spritesheet->name, old, character->callbackData);
} }
} } else if ((character->reversed) && (character->predecessor)) {
if ((character->reversed) && (character->predecessor)) {
char* old = character->spritesheet->name; char* old = character->spritesheet->name;
SelectSpritesheet(game, character, character->predecessor); SelectSpritesheet(game, character, character->predecessor);
if (character->callback) { if (character->callback) {
character->callback(game, character, character->spritesheet->name, old, character->callbackData); character->callback(game, character, character->spritesheet->name, old, character->callbackData);
} }
} else {
if (character->repeats == 0) {
if (character->reversed) {
character->pos = 1;
} else {
character->pos = character->spritesheet->frameCount - 1;
}
if (character->callback) {
character->callback(game, character, NULL, character->spritesheet->name, character->callbackData);
} }
} }
} }
} }
}
if (character->spritesheet->frameCount == 1) {
character->pos = 0;
}
}
character->frame = &character->spritesheet->frames[character->pos]; character->frame = &character->spritesheet->frames[character->pos];
} }

View file

@ -89,7 +89,7 @@ struct Character {
int confineY; /*!< Height of the canvas being drawn to, for correct position calculation; when -1, uses parent's confines or viewport size */ int confineY; /*!< Height of the canvas being drawn to, for correct position calculation; when -1, uses parent's confines or viewport size */
bool flipX; /*!< Flips the character's sprite vertically. */ bool flipX; /*!< Flips the character's sprite vertically. */
bool flipY; /*!< Flips the character's sprite horizontally. */ bool flipY; /*!< Flips the character's sprite horizontally. */
int repeats; /*!< Number of repeats left before the spritesheet is changed to its successor. */ int repeats; /*!< Number of repeats left before the spritesheet is changed to its successor or stopped. */
bool reversing; /*!< Whether the animation is currently played backwards. */ bool reversing; /*!< Whether the animation is currently played backwards. */
bool reversed; /*!< Whether the current animation has been requested as reversed. */ bool reversed; /*!< Whether the current animation has been requested as reversed. */
bool hidden; bool hidden;