51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#include "GameObject.h"
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <sys/time.h>
|
|
#include <ctime>
|
|
|
|
using namespace std;
|
|
|
|
struct LastFrame
|
|
{
|
|
string frameName;
|
|
int frameIndex;
|
|
};
|
|
|
|
class DinamicObject: public GameObject
|
|
{
|
|
public:
|
|
DinamicObject(int x, int y, int w, int h, map<string, vector<SDL_Rect>> 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<string, vector<SDL_Rect>> animations;
|
|
int frameLenth;
|
|
time_t lastFrameTime;
|
|
LastFrame lastFrame;
|
|
}; |