Skip to content

Commit 8acd9a9

Browse files
committed
AnimatorComponent
1 parent 05b9aad commit 8acd9a9

23 files changed

+407
-407
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ add_library(Strife.Engine STATIC
163163
Project/Project.cpp
164164
Project/Project.hpp
165165
Resource/ResourceManager.hpp Resource/SpriteResource.hpp Resource/SpriteResource.cpp Resource/ResourceManager.cpp Resource/TilemapResource.hpp Resource/TilemapResource.cpp Resource/SpriteFontResource.hpp Resource/SpriteFontResource.cpp
166-
Resource/ShaderResource.cpp Resource/ShaderResource.hpp Components/ParticleSystemComponent.hpp Components/ParticleSystemComponent.cpp Scene/Isometric.hpp Scene/Isometric.cpp Components/IsometricSpriteComponent.hpp Components/IsometricSpriteComponent.cpp Resource/FileResource.hpp Resource/FileResource.cpp ML/UtilityAI.hpp Resource/ScriptResource.hpp Resource/ScriptResource.cpp ML/GridSensor.hpp Renderer/SpriteEffect.hpp Renderer/SpriteEffect.cpp Renderer/Stage/RenderPipeline.hpp Renderer/Stage/RenderPipeline.cpp)
166+
Resource/ShaderResource.cpp Resource/ShaderResource.hpp Components/ParticleSystemComponent.hpp Components/ParticleSystemComponent.cpp Scene/Isometric.hpp Scene/Isometric.cpp Components/IsometricSpriteComponent.hpp Components/IsometricSpriteComponent.cpp Resource/FileResource.hpp Resource/FileResource.cpp ML/UtilityAI.hpp Resource/ScriptResource.hpp Resource/ScriptResource.cpp ML/GridSensor.hpp Renderer/SpriteEffect.hpp Renderer/SpriteEffect.cpp Renderer/Stage/RenderPipeline.hpp Renderer/Stage/RenderPipeline.cpp Resource/SpriteAtlasResource.hpp Resource/SpriteAtlasResource.cpp Resource/ResourceSettings.hpp Components/AnimatorComponent.hpp Components/AnimatorComponent.cpp)
167167

168168
message("SDL DIRS: ${SDL2_INCLUDE_DIRS}")
169169

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "AnimatorComponent.hpp"
2+
#include "Renderer/Renderer.hpp"
3+
#include "Renderer/Sprite.hpp"
4+
#include "Scene/Scene.hpp"
5+
6+
AnimatorComponent::AnimatorComponent(const char* atlasResourceName)
7+
: _atlas(GetResource<SpriteAtlasResource>(atlasResourceName))
8+
{
9+
10+
}
11+
12+
13+
const AtlasAnimation* SpriteAtlas::GetAnimation(StringId name) const
14+
{
15+
for (const auto& animation : _animations)
16+
{
17+
if (animation.name == name)
18+
{
19+
return &animation;
20+
}
21+
}
22+
23+
FatalError("No such animation: %s", name.ToString());
24+
}
25+
26+
void AnimatorComponent::PlayAnimation(StringId name, AnimationPlayMode mode)
27+
{
28+
if (_atlas == nullptr) return;
29+
30+
_currentAnimation = _atlas->Get().GetAnimation(name);
31+
32+
_frameDuration = 1.0f / static_cast<float>(_currentAnimation->fps);
33+
_relativeTime = 0;
34+
_currentAnimationIndex = 0;
35+
_mode = mode;
36+
_animationComplete = false;
37+
}
38+
39+
void AnimatorComponent::Render(Renderer* renderer)
40+
{
41+
if (_currentAnimation == nullptr)
42+
{
43+
return;
44+
}
45+
46+
Sprite currentFrame;
47+
_atlas->Get().GetFrame(_currentAnimation->frames[_currentAnimationIndex], &currentFrame);
48+
49+
renderer->RenderSprite(
50+
&currentFrame,
51+
owner->ScreenCenter() + offsetFromCenter,
52+
depth,
53+
Vector2(1, 1),
54+
0,
55+
_flipHorizontal,
56+
blendColor);
57+
}
58+
59+
void AnimatorComponent::SetSpriteAtlas(SpriteAtlasResource* atlas)
60+
{
61+
_atlas = atlas;
62+
}
63+
64+
void AnimatorComponent::Pause()
65+
{
66+
_originalMode = _mode;
67+
_mode = AnimationPlayMode::Paused;
68+
}
69+
70+
void AnimatorComponent::Resume()
71+
{
72+
if (_mode == AnimationPlayMode::Paused)
73+
{
74+
_mode = _originalMode;
75+
}
76+
}
77+
78+
void AnimatorComponent::GetCurrentFrame(Sprite& outFrame) const
79+
{
80+
_atlas->Get().GetFrame(_currentAnimation->frames[_currentAnimationIndex], &outFrame);
81+
}
82+
83+
void AnimatorComponent::NextFrame()
84+
{
85+
if(_animationComplete)
86+
{
87+
return;
88+
}
89+
90+
++_currentAnimationIndex;
91+
92+
if (_currentAnimationIndex >= _currentAnimation->frames.size())
93+
{
94+
if (_mode == AnimationPlayMode::Loop)
95+
{
96+
_currentAnimationIndex = _currentAnimationIndex % _currentAnimation->frames.size();
97+
}
98+
else
99+
{
100+
_currentAnimationIndex = _currentAnimation->frames.size() - 1;
101+
_animationComplete = true;
102+
return;
103+
}
104+
}
105+
}
106+
107+
void AnimatorComponent::Update(float deltaTime)
108+
{
109+
if (_mode != AnimationPlayMode::Paused)
110+
{
111+
// Update current frame
112+
_relativeTime += GetScene()->relativeTime - _absoluteTime;
113+
114+
while (_relativeTime >= _frameDuration)
115+
{
116+
_relativeTime -= _frameDuration;
117+
NextFrame();
118+
}
119+
}
120+
121+
_absoluteTime = GetScene()->relativeTime;
122+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#include "Scene/EntityComponent.hpp"
4+
#include "Resource/SpriteAtlasResource.hpp"
5+
6+
enum class AnimationPlayMode
7+
{
8+
Loop, // Loop
9+
Hold, // Play once, then hold the last frame
10+
Paused
11+
};
12+
13+
DEFINE_COMPONENT(AnimatorComponent)
14+
{
15+
public:
16+
explicit AnimatorComponent(SpriteAtlasResource* atlas)
17+
: _atlas(atlas),
18+
_currentAnimation(nullptr)
19+
{
20+
21+
}
22+
23+
AnimatorComponent(const char* atlasResourceName);
24+
25+
StringId CurrentAnimation() const
26+
{
27+
return _currentAnimation != nullptr
28+
? _currentAnimation->name
29+
: ""_sid;
30+
}
31+
32+
AnimationPlayMode GetMode() const { return _mode; }
33+
34+
bool AnimationComplete() const { return _animationComplete; }
35+
36+
37+
void PlayAnimation(StringId name, AnimationPlayMode mode);
38+
39+
void Render(Renderer* renderer) override;
40+
void Update(float deltaTime) override;
41+
42+
void Pause();
43+
void Resume();
44+
void GetCurrentFrame(Sprite& outFrame) const;
45+
46+
void SetFlipHorizontal(bool flipHorizontal)
47+
{
48+
_flipHorizontal = flipHorizontal;
49+
}
50+
51+
bool IsFlippedHorizontal() const { return _flipHorizontal; }
52+
void SetSpriteAtlas(SpriteAtlasResource* atlas);
53+
54+
Color blendColor;
55+
Vector2 offsetFromCenter;
56+
float depth;
57+
58+
private:
59+
void NextFrame();
60+
61+
SpriteAtlasResource* _atlas = nullptr;
62+
63+
const AtlasAnimation* _currentAnimation = nullptr;
64+
65+
int _currentAnimationIndex;
66+
AnimationPlayMode _mode;
67+
AnimationPlayMode _originalMode;
68+
69+
float _relativeTime = 0;
70+
float _absoluteTime = 0;
71+
float _frameDuration = 0;
72+
73+
bool _flipHorizontal = false;
74+
Vector2 _cornerSize;
75+
Vector2 _nineSliceSize;
76+
bool _animationComplete = false;
77+
};

src/Project/Project.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace nlohmann;
1010
json LoadJsonFromFile(const std::filesystem::path& path)
1111
{
1212
std::fstream file(path.string());
13-
if (file.bad())
13+
if (!file.is_open())
1414
{
1515
file.close();
1616
FatalError("Failed to open file %s", path.c_str());
@@ -50,9 +50,9 @@ Project::Project(const std::filesystem::path& path)
5050
}
5151
else
5252
{
53-
resourceManager->LoadResourceFromFile(
54-
resource["path"].get<std::string>().c_str(),
55-
resource["name"].get<std::string>().c_str());
53+
// resourceManager->LoadResourceFromFile(
54+
// resource["path"].get<std::string>().c_str(),
55+
// resource["name"].get<std::string>().c_str());
5656
}
5757
}
5858

src/Renderer/Renderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void Renderer::RenderString(const FontSettings& fontSettings, const char* str, V
9494
return;
9595
}
9696

97-
auto characterSize = fontSettings.spriteFont->GetFont()->CharacterDimension(fontSettings.scale);
97+
auto characterSize = fontSettings.spriteFont->Get().CharacterDimension(fontSettings.scale);
9898
Vector2 position = topLeft;
9999

100100
while (*str != '\0')
@@ -107,7 +107,7 @@ void Renderer::RenderString(const FontSettings& fontSettings, const char* str, V
107107
else
108108
{
109109
Sprite characterSprite;
110-
fontSettings.spriteFont->GetFont()->GetCharacter((unsigned char)*str, &characterSprite);
110+
fontSettings.spriteFont->Get().GetCharacter((unsigned char)*str, &characterSprite);
111111

112112
RenderSprite(
113113
&characterSprite,

0 commit comments

Comments
 (0)