-
Notifications
You must be signed in to change notification settings - Fork 1
Add Animator, IAnimation, DeathAnimation
#30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "chomper/animator.hpp" | ||
|
|
||
| namespace chomper::animation { | ||
| class DeathAnimation : public chomper::IAnimation { | ||
| public: | ||
| DeathAnimation(std::span<le::RenderInstance const> instances); | ||
| void tick(kvf::Seconds dt) final; | ||
| void draw(le::IRenderer& renderer) const final; | ||
|
|
||
| private: | ||
| struct Segment { | ||
| float remaining{}; | ||
| float lifetime{}; | ||
| float rotSpeed{}; | ||
| glm::vec2 velocity{}; | ||
| }; | ||
|
|
||
| std::vector<Segment> m_segments{}; | ||
| le::drawable::InstancedQuad m_quads{}; | ||
|
|
||
| le::Random m_random{}; | ||
| }; | ||
| } // namespace chomper::animation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #pragma once | ||
| #include <kvf/time.hpp> | ||
| #include <le2d/drawable/shape.hpp> | ||
| #include <le2d/random.hpp> | ||
| #include <le2d/render_instance.hpp> | ||
|
|
||
| namespace chomper { | ||
| class IAnimation : public klib::Polymorphic { | ||
| public: | ||
| virtual void tick(kvf::Seconds dt) = 0; | ||
| virtual void draw(le::IRenderer& renderer) const = 0; | ||
|
|
||
| [[nodiscard]] bool finished() const { | ||
| return m_finished; | ||
| } | ||
|
|
||
| protected: | ||
| bool m_finished{}; | ||
| }; | ||
|
|
||
| class Animator { | ||
| public: | ||
| void play(std::unique_ptr<IAnimation> animation); | ||
|
|
||
| void tick(kvf::Seconds dt); | ||
|
|
||
| void draw(le::IRenderer& renderer) const; | ||
|
|
||
| private: | ||
| std::vector<std::unique_ptr<IAnimation>> m_playing{}; | ||
| }; | ||
| } // namespace chomper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #include "chomper/animations/deathAnimation.hpp" | ||
| #include "chomper/world_space.hpp" | ||
| #include <numbers> | ||
|
|
||
| namespace chomper::animation { | ||
| namespace { | ||
| constexpr auto easeOut(float t) { | ||
| return 1.f - ((1.f - t) * (1.f - t)); | ||
| } | ||
| } // namespace | ||
|
|
||
| DeathAnimation::DeathAnimation(std::span<le::RenderInstance const> instances) { | ||
| m_segments.clear(); | ||
| m_segments.reserve(instances.size()); | ||
| m_quads.instances.clear(); | ||
| m_quads.instances.reserve(instances.size()); | ||
| m_quads.create(tileSize_v); | ||
|
|
||
| for (auto const& instance : instances) { | ||
| auto dir = m_random.next_float(0, 360); | ||
| auto lifetime = m_random.next_float(2, 5); | ||
| auto speed = m_random.next_float(300, 600); | ||
| auto rotSpeed = m_random.next_float(5.f, 20.f); | ||
|
|
||
| auto rad = dir * std::numbers::pi_v<float> / 180.f; | ||
|
|
||
| Segment seg; | ||
| seg.rotSpeed = rotSpeed; | ||
| seg.velocity = {std::cos(rad) * speed, std::sin(rad) * speed}; | ||
| seg.remaining = lifetime; | ||
| seg.lifetime = lifetime; | ||
|
|
||
| m_quads.instances.emplace_back().tint = instance.tint; | ||
| m_quads.instances.back().transform = instance.transform; | ||
|
|
||
| m_segments.push_back(seg); | ||
| } | ||
| } | ||
|
|
||
| void DeathAnimation::tick(kvf::Seconds dt) { | ||
| assert(m_segments.size() == m_quads.instances.size()); | ||
|
|
||
| auto animating = false; | ||
|
|
||
| for (std::size_t i = 0; i < m_segments.size(); i++) { | ||
| auto& seg = m_segments[i]; | ||
| if (seg.remaining <= 0.f) { | ||
| continue; | ||
| } | ||
|
|
||
| seg.remaining -= dt.count(); | ||
|
|
||
| auto t = std::clamp(1.f - (seg.remaining / seg.lifetime), 0.f, 1.f); | ||
|
|
||
| auto eased = 1.f - easeOut(t); | ||
|
|
||
| auto& quad = m_quads.instances.at(i); | ||
| quad.transform.position += seg.velocity * eased * dt.count(); | ||
| if (worldSpace::isOutOfBounds(worldSpace::worldToGrid(quad.transform.position))) { | ||
| seg.velocity = -seg.velocity; | ||
| } | ||
|
|
||
| auto angle = dt.count() * eased * seg.rotSpeed; | ||
| quad.transform.orientation.rotate(angle); | ||
|
|
||
| if (t > 0.8f) { | ||
| auto scale = 1.f; | ||
| auto u = (t - 0.8f) / 0.2f; // 0 → 1 | ||
| scale = 1.f - u; // 1 → 0 | ||
| quad.transform.scale = {scale, scale}; | ||
| } | ||
|
|
||
| if (t < 1.f) { | ||
| animating = true; | ||
| } | ||
| } | ||
| m_finished = !animating; | ||
| } | ||
|
|
||
| void DeathAnimation::draw(le::IRenderer& renderer) const { | ||
| m_quads.draw(renderer); | ||
| } | ||
|
|
||
| } // namespace chomper::animation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #include "chomper/animator.hpp" | ||
|
|
||
| namespace chomper { | ||
| void Animator::play(std::unique_ptr<IAnimation> animation) { | ||
| m_playing.emplace_back(std::move(animation)); | ||
| } | ||
|
|
||
| void Animator::tick(kvf::Seconds dt) { | ||
| std::erase_if(m_playing, [&](auto const& animation) { | ||
| return animation->finished(); | ||
| }); | ||
| for (auto const& animation : m_playing) { | ||
| animation->tick(dt); | ||
| } | ||
| } | ||
|
|
||
| void Animator::draw(le::IRenderer& renderer) const { | ||
| for (auto const& animation : m_playing) { | ||
| animation->draw(renderer); | ||
| } | ||
| } | ||
| } // namespace chomper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.