forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimSpriteComponent.cpp
More file actions
91 lines (76 loc) · 2.29 KB
/
AnimSpriteComponent.cpp
File metadata and controls
91 lines (76 loc) · 2.29 KB
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
87
88
89
90
91
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "AnimSpriteComponent.h"
#include "Math.h"
AnimSpriteComponent::AnimSpriteComponent(Actor* owner, int drawOrder)
:SpriteComponent(owner, drawOrder)
, mCurrFrame(0.0f)
, mAnimFPS(24.0f)
, mCurrentAnimation(-1)
, mNextAnimationTimer(0.f)
, mCanChangeAnimation(true)
{
}
void AnimSpriteComponent::Update(float deltaTime)
{
SpriteComponent::Update(deltaTime);
if (!mCanChangeAnimation)
{
mNextAnimationTimer += deltaTime;
if (mNextAnimationTimer > 0.5f)
{
mCanChangeAnimation = true;
mNextAnimationTimer = 0.f;
}
}
if (!mAnimTextures.empty())
{
// Update the current frame based on frame rate
// and delta time
mCurrFrame += mAnimFPS * deltaTime;
// Wrap current frame if needed
while (mCurrFrame >= mAnimSpriteInfos[mCurrentAnimation].end + 1)
{
if (!mAnimSpriteInfos[mCurrentAnimation].looping)
{
mCurrFrame = mAnimSpriteInfos[mCurrentAnimation].end;
}
else
{
mCurrFrame -= mAnimSpriteInfos[mCurrentAnimation].end - mAnimSpriteInfos[mCurrentAnimation].start;
}
}
// Set the current texture
SetTexture(mAnimTextures[static_cast<int>(mCurrFrame)]);
}
}
void AnimSpriteComponent::SetAnimTextures(const std::vector<SDL_Texture*>& textures, bool looping)
{
if (!textures.empty())
{
mCurrFrame = mAnimTextures.size();
mAnimSpriteInfos.emplace_back(mCurrFrame, (mCurrFrame + textures.size() - 1), looping);
SDL_Log("Animation start: %i Animation end: %i", mAnimSpriteInfos.back().start, mAnimSpriteInfos.back().end);
mAnimTextures.insert(mAnimTextures.end(), textures.begin(), textures.end());
// Set the active texture to first frame
SetTexture(mAnimTextures[mCurrFrame]);
mCurrentAnimation++;
}
}
void AnimSpriteComponent::NextAnimation()
{
if (mAnimSpriteInfos.size() > 1 && mCanChangeAnimation)
{
if (++mCurrentAnimation == mAnimSpriteInfos.size())
{
mCurrentAnimation = 0;
}
SetTexture(mAnimTextures[mAnimSpriteInfos[mCurrentAnimation].start]);
mCanChangeAnimation = false;
}
}