Skip to content

Commit c7282e3

Browse files
committed
feat(animation): add playDuration method to AnimationComponent for timed playback of animations
1 parent 39794f4 commit c7282e3

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/common/components/animation.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,27 @@ namespace our {
6262
animator.play(&animIt->second, clip.loop, clip.speed * speedScale);
6363
}
6464

65+
void AnimationComponent::playDuration(const std::string& clipName, float duration) {
66+
if (!model) {
67+
std::cerr << "\033[31mCannot play animation: model is not set\033[0m" << std::endl;
68+
return;
69+
}
70+
71+
auto clipIt = clips.find(clipName);
72+
if (clipIt == clips.end()) {
73+
std::cerr << "\033[31mClip not found: " << clipName << "\033[0m" << std::endl;
74+
return;
75+
}
76+
AnimationClip& clip = clipIt->second;
77+
auto animIt = model->animations.find(clip.clip);
78+
if (animIt == model->animations.end()) {
79+
std::cerr << "\033[31mAnimation not found in model: " << clip.clip << "\033[0m" << std::endl;
80+
return;
81+
}
82+
float originalSpeed = clip.speed;
83+
float animDuration = animIt->second.duration / animIt->second.ticksPerSecond;
84+
float speedScale = animDuration > 0.0f ? animDuration / duration : 1.0f;
85+
animator.play(&animIt->second, false, originalSpeed * speedScale);
86+
}
87+
6588
} // namespace our

src/common/components/animation.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace our {
2727
void deserialize(const nlohmann::json& data) override;
2828

2929
void play(const std::string& clipName, float speedScale = 1.0f);
30+
void playDuration(const std::string& clipName, float duration);
3031
};
3132

3233
} // namespace our

0 commit comments

Comments
 (0)