-
Notifications
You must be signed in to change notification settings - Fork 1
Added animations for the snake's face #33
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
Open
stanplayzz
wants to merge
2
commits into
main
Choose a base branch
from
stan/head-animation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
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,60 @@ | ||
| #pragma once | ||
| #include "chomper/animator.hpp" | ||
| #include "chomper/heading.hpp" | ||
| #include "klib/base_types.hpp" | ||
| #include "klib/log.hpp" | ||
| #include "klib/ptr.hpp" | ||
| #include "le2d/drawable/sprite.hpp" | ||
| #include "le2d/render_instance.hpp" | ||
| #include <glm/vec2.hpp> | ||
|
|
||
| namespace chomper { | ||
| class Engine; | ||
|
|
||
| namespace animation { | ||
| class DirectionProvider : public klib::Polymorphic { | ||
| public: | ||
| [[nodiscard]] virtual glm::vec2 getHeadPosition() const = 0; | ||
| [[nodiscard]] virtual Heading getHeading() const = 0; | ||
| }; | ||
|
|
||
| class CollectibleProvider : public klib::Polymorphic { | ||
| public: | ||
| [[nodiscard]] virtual std::span<le::RenderInstance const> getCollectibles() const = 0; | ||
| }; | ||
|
|
||
| class Eye { | ||
| public: | ||
| explicit Eye(le::ITexture& eyeLidTexture); | ||
| void setPosition(glm::vec2 position); | ||
| void lookAt(glm::vec2 target); | ||
| void draw(le::IRenderer& renderer, bool drawEyeLids) const; | ||
|
|
||
| private: | ||
| le::drawable::Sprite m_eyeLid{}; | ||
| le::drawable::Circle m_eye{}; | ||
| le::drawable::Circle m_pupil{}; | ||
| }; | ||
|
|
||
| class HeadAnimation : public IAnimation { | ||
| public: | ||
| explicit HeadAnimation(gsl::not_null<DirectionProvider const*> directionProvider, gsl::not_null<CollectibleProvider const*> collectibleProvider, | ||
| gsl::not_null<Engine const*> engine); | ||
| void tick(kvf::Seconds dt) final; | ||
| void draw(le::IRenderer& renderer) const final; | ||
|
|
||
| private: | ||
| klib::TypedLogger<HeadAnimation> m_log; | ||
|
|
||
| gsl::not_null<DirectionProvider const*> m_directionProvider; | ||
| gsl::not_null<CollectibleProvider const*> m_collectibleProvider; | ||
|
|
||
| le::drawable::Sprite m_mouth{}; | ||
| std::unique_ptr<Eye> m_leftEye{}; | ||
| std::unique_ptr<Eye> m_rightEye{}; | ||
| klib::Ptr<le::ITexture> m_eyeLidTexture{}; | ||
|
|
||
| bool m_shouldDraw{}; | ||
| }; | ||
| } // namespace animation | ||
| } // 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
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,21 @@ | ||
| #pragma once | ||
| #include <glm/trigonometric.hpp> | ||
| #include <glm/vec2.hpp> | ||
| #include <klib/enum_array.hpp> | ||
|
|
||
| namespace chomper { | ||
| // represents a direction. | ||
| enum class Heading : std::int8_t { | ||
| East, | ||
| North, | ||
| West, | ||
| South, | ||
| COUNT_ | ||
| }; | ||
|
|
||
| constexpr auto headingName_v = klib::EnumArray<Heading, std::string_view>{"East", "North", "West", "South"}; | ||
| constexpr auto oppositeHeading_v = klib::EnumArray<Heading, Heading>{Heading::West, Heading::South, Heading::East, Heading::North}; | ||
| constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::ivec2>{glm::ivec2{1, 0}, glm::ivec2{0, 1}, glm::ivec2{-1, 0}, glm::ivec2{0, -1}}; | ||
| constexpr klib::EnumArray<Heading, float> headingToRot_v{glm::radians(90.f), glm::radians(0.f), glm::radians(270.f), glm::radians(180.f)}; | ||
|
|
||
| } // 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
2 changes: 1 addition & 1 deletion
2
lib/src/animations/deathAnimation.cpp → lib/src/animations/death_animation.cpp
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,108 @@ | ||
| #include "chomper/animations/head_animation.hpp" | ||
| #include "chomper/engine.hpp" | ||
| #include "chomper/world_space.hpp" | ||
| #include "klib/enum_array.hpp" | ||
|
|
||
| namespace chomper::animation { | ||
| namespace { | ||
| constexpr auto isAdjacent(glm::ivec2 const& grid1, glm::ivec2 const& grid2) { | ||
| auto dx = klib::abs(grid1.x - grid2.x); | ||
| auto dy = klib::abs(grid1.y - grid2.y); | ||
|
|
||
| return (dx <= 1 && dy <= 1) && (dx != 0 || dy != 0); | ||
| } | ||
|
|
||
| std::optional<glm::vec2> findAdjacentCollectible(std::span<le::RenderInstance const> collectibles, glm::ivec2 headPos) { | ||
| auto it = std::ranges::find_if(collectibles, [&](auto const& c) { | ||
| return isAdjacent(headPos, worldSpace::worldToGrid(c.transform.position)); | ||
| }); | ||
| if (it == collectibles.end()) { | ||
| return std::nullopt; | ||
| } | ||
| return it->transform.position; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| HeadAnimation::HeadAnimation(gsl::not_null<DirectionProvider const*> directionProvider, gsl::not_null<CollectibleProvider const*> collectibleProvider, | ||
| gsl::not_null<Engine const*> engine) | ||
| : m_directionProvider(directionProvider), m_collectibleProvider(collectibleProvider) { | ||
| m_mouth.set_base_size(tileSize_v); | ||
| m_mouth.set_texture(engine->getResources().load<le::ITexture>("images/snake_mouth.png")); | ||
|
|
||
| m_eyeLidTexture = engine->getResources().load<le::ITexture>("images/eye_lid.png"); | ||
| if (!m_eyeLidTexture) { | ||
| return; | ||
| } | ||
| m_leftEye = std::make_unique<Eye>(*m_eyeLidTexture); | ||
| m_rightEye = std::make_unique<Eye>(*m_eyeLidTexture); | ||
stanplayzz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| void HeadAnimation::tick(kvf::Seconds /*dt*/) { | ||
| auto headPos = m_directionProvider->getHeadPosition(); | ||
| auto heading = m_directionProvider->getHeading(); | ||
|
|
||
| auto c = std::cos(headingToRot_v[heading]); | ||
| auto s = std::sin(headingToRot_v[heading]); | ||
| auto rotation = glm::mat2(c, -s, s, c); | ||
|
|
||
| auto rightEyeOffset = rotation * glm::vec2{tileSize_v.x * 0.22f, tileSize_v.y * -0.22f}; | ||
| m_rightEye->setPosition(headPos + rightEyeOffset); | ||
|
|
||
| auto leftEyeOffset = rotation * glm::vec2{tileSize_v.x * -0.22f, tileSize_v.y * -0.22f}; | ||
| m_leftEye->setPosition(headPos + leftEyeOffset); | ||
|
|
||
| auto target = findAdjacentCollectible(m_collectibleProvider->getCollectibles(), worldSpace::worldToGrid(headPos)); | ||
| if (!target) { | ||
| m_shouldDraw = false; | ||
| return; | ||
| } | ||
| m_shouldDraw = true; | ||
karnkaul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| auto mouthOffset = rotation * glm::vec2{0.f, tileSize_v.x * 0.5f}; | ||
| m_mouth.transform.position = headPos + mouthOffset; | ||
| m_mouth.transform.orientation = le::nvec2::from_radians(headingToRot_v[heading]); | ||
|
|
||
| m_leftEye->lookAt(*target); | ||
| m_rightEye->lookAt(*target); | ||
| } | ||
|
|
||
| void HeadAnimation::draw(le::IRenderer& renderer) const { | ||
| m_leftEye->draw(renderer, !m_shouldDraw); | ||
| m_rightEye->draw(renderer, !m_shouldDraw); | ||
|
|
||
| if (!m_shouldDraw) { | ||
| return; | ||
| } | ||
| m_mouth.draw(renderer); | ||
| } | ||
|
|
||
| Eye::Eye(le::ITexture& texture) { | ||
| m_eye.create(tileSize_v.x * 0.4f); | ||
| m_pupil.create(m_eye.get_diameter() * 0.5f); | ||
| m_pupil.tint = kvf::Color{glm::vec4{0.f, 0.f, 0.f, 1.f}}; | ||
| m_eyeLid.set_base_size(m_eye.get_size()); | ||
|
|
||
| m_eyeLid.set_texture(&texture); | ||
| } | ||
|
|
||
| void Eye::setPosition(glm::vec2 position) { | ||
| m_eyeLid.transform.position = position; | ||
| m_eye.transform.position = position; | ||
| m_pupil.transform.position = position; | ||
| } | ||
|
|
||
| void Eye::lookAt(glm::vec2 target) { | ||
| auto dir = glm::normalize(target - m_eye.transform.position); | ||
| m_pupil.transform.position = m_eye.transform.position + dir * m_eye.get_diameter() * 0.25f; | ||
| } | ||
|
|
||
| void Eye::draw(le::IRenderer& renderer, bool drawEyeLids) const { | ||
| m_eye.draw(renderer); | ||
| m_pupil.draw(renderer); | ||
| if (drawEyeLids) { | ||
| m_eyeLid.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
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
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.