diff options
author | BoredGuy <osome3717@gmail.com> | 2025-08-21 20:07:04 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-08-21 20:07:04 +0300 |
commit | 8a8ed6d3fa7059dbb2a95072bbae4bf4618349a0 (patch) | |
tree | 5f1383eb3983b2bfc73680322a4bf0b68abbf770 /src/game.c | |
parent | 251be1ac2d808dfd0fca5c0eb37398357ca7bb20 (diff) |
Work on animation subsystem
- Also incomplete refactor of inline functions
Diffstat (limited to 'src/game.c')
-rw-r--r-- | src/game.c | 45 |
1 files changed, 31 insertions, 14 deletions
@@ -8,23 +8,10 @@ #include "player.h" #include "background.h" #include <math.h> +#include "utils.h" Game game; -static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { - return EntityAllocated(e) - && (e->flags & ENTITY_VISIBLE); -} - -static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { - Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; - - destRectGlobal.x += e->position.x; - destRectGlobal.y += e->position.y; - - return destRectGlobal; -} - void AddEntity(Entity* e) { static int nextId = 0; @@ -158,3 +145,33 @@ void AddWall(float xpos, float ypos, float width, float height) { AddEntity(&wall); } + +static inline float GetCurrentFrameTime(const Animation* animation) { + return animation->frameTimes[animation->currentFrame]; +} + +void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) { + if (!IsAnimated(sprite)) + return; + + Animation* currentAnimation = GetCurrentAnimation(sprite); + + if (currentAnimation->isComplete) + return; + + currentAnimation->currentFrameTimer += dt; + + if (currentAnimation->currentFrameTimer >= GetCurrentFrameTime(currentAnimation)) { + currentAnimation->currentFrame++; + currentAnimation->currentFrameTimer = 0; + } + + if (currentAnimation->currentFrame >= currentAnimation->numFrames) { + if (currentAnimation->isLooping) + currentAnimation->currentFrame = 0; + else { + currentAnimation->currentFrame = currentAnimation->numFrames - 1; + currentAnimation->isComplete = true; + } + } +} |