#include "GameObject.h" #include #include #include #include #include using namespace std; struct LastFrame { string frameName; int frameIndex; }; class DinamicObject: public GameObject { public: DinamicObject(int x, int y, int w, int h, map> animations, int animationFps = 30): GameObject(x, y, w, h) { this->animations = animations; this->frameLenth = 1000/animationFps; this->lastFrameTime = -1; this->lastFrame = {"", 0}; } Frame frame() { time_t currentTime; { struct timeval timeNow{}; gettimeofday(&timeNow, nullptr); currentTime = (timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000); } if (this->lastFrameTime == -1) this->lastFrameTime = currentTime; int currentFrameIndex = ((this->lastFrame).frameIndex + (currentTime - this->lastFrameTime)/this->frameLenth) % (this->animations)[(this->lastFrame).frameName].size(); this->lastFrameTime += (currentFrameIndex - (this->lastFrame).frameIndex) * this->frameLenth; (this->lastFrame).frameIndex = currentFrameIndex; return Frame(this->dstrect, animations[(this->lastFrame).frameName][currentFrameIndex]); } protected: map> animations; int frameLenth; time_t lastFrameTime; LastFrame lastFrame; };