-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
86 lines (74 loc) · 1.57 KB
/
Animation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Animation.hpp"
#include <iostream>
using namespace std;
Animation::Animation(GameObject* gameObject, vector<Sprite*>* sprites)
{
this->sprites = sprites;
isPlaying = false;
currSprite = 0;
engine = Engine::getInstance();
window = engine->getWindow();
this->gameObject = gameObject;
// default time
timeBetweenFrames = 1.0 / 24.0;
layer = RenderingLayer::UIELEMENT;
Renderer::getInstance()->addToRenderQueue(this);
}
Animation::~Animation()
{
for (int i = 0; i < sprites->size(); i++)
{
Sprite* currSprite = (*sprites)[i];
delete(currSprite);
}
free(sprites);
Renderer::getInstance()->removeFromRenderQueue(this);
}
void Animation::draw()
{
if (isPlaying)
{
if (currSprite < sprites->size())
{
cout << "draw sprite" << endl;
Sprite* sprite = (*sprites)[currSprite];
sprite->setPosition(gameObject->transform->position);
sprite->setScale(gameObject->transform->scale);
window->draw(*sprite);
}
}
//Sprite* sprite = (*sprites)[0];
//sprite->setPosition(gameObject->transform->position);
//sprite->setScale(gameObject->transform->scale);
//window->draw(*sprite);
}
void Animation::update()
{
if (isPlaying)
{
elapsedTime += engine->getDeltaTime();
if (currSprite >= sprites->size())
{
// animation finished playing
isPlaying = false;
currSprite = 0;
elapsedTime = 0;
return;
}
// draw current sprite
if (elapsedTime > timeBetweenFrames)
{
elapsedTime = 0;
currSprite++;
}
}
}
void Animation::lateUpdate()
{
}
void Animation::playAnimation()
{
isPlaying = true;
currSprite = 0;
elapsedTime = 0;
}