wow, I created ground and fire
This commit is contained in:
parent
c14d6e6c59
commit
2288fb8d15
14 changed files with 185 additions and 48 deletions
51
src/game/DinamicObject.h
Normal file
51
src/game/DinamicObject.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue