added fancy animations

This commit is contained in:
Lauchmelder 2022-01-15 18:05:36 +01:00
parent 690b577e87
commit 758d577609
5 changed files with 96 additions and 12 deletions

View file

@ -24,9 +24,14 @@ void Application::Run()
return; return;
} }
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
spdlog::debug("Launched Application"); spdlog::debug("Launched Application");
while (!window->ShouldClose()) while (!window->ShouldClose())
{ {
frametime = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
glfwPollEvents(); glfwPollEvents();
for (lol::Layer* layer : layerStack) for (lol::Layer* layer : layerStack)
@ -44,7 +49,10 @@ void Application::Run()
void Application::OnKeyPressed(unsigned int character) void Application::OnKeyPressed(unsigned int character)
{ {
if (character == 'd' || character == 'D') if (character == 'd' || character == 'D')
game.GetBoard().RevealCard(); {
if(!game.GetStack().Empty())
game.GetBoard().RevealCard();
}
} }
Application::Application() : Application::Application() :

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <chrono>
#include <lol/lol.hpp> #include <lol/lol.hpp>
#include "CardStack.hpp" #include "CardStack.hpp"
@ -22,6 +23,8 @@ public:
void Run(); void Run();
void OnKeyPressed(unsigned int character); // TODO: Remove later on void OnKeyPressed(unsigned int character); // TODO: Remove later on
inline double GetFrametime() { return frametime; }
private: private:
bool valid; bool valid;
Window* window; Window* window;
@ -31,6 +34,8 @@ private:
lol::ObjectManager manager; lol::ObjectManager manager;
std::vector<lol::Layer*> layerStack; std::vector<lol::Layer*> layerStack;
double frametime;
private: private:
Application(); Application();
~Application(); ~Application();

View file

@ -3,8 +3,14 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "ObjectIDs.hpp" #include "ObjectIDs.hpp"
double ParametricCurve(double t, double alpha, double n)
{
double power = pow(t / n, alpha);
return power / (power + pow(1.0f - t / n, alpha));
}
CardSprite::CardSprite(lol::ObjectManager& manager, std::shared_ptr<Card> card) : CardSprite::CardSprite(lol::ObjectManager& manager, std::shared_ptr<Card> card) :
card(card) card(card), animationRunning(false)
{ {
try try
{ {
@ -44,6 +50,33 @@ CardSprite::CardSprite(lol::ObjectManager& manager, std::shared_ptr<Card> card)
); );
} }
void CardSprite::CreateAnimation(Animation animation)
{
this->animation = animation;
t = 0.0;
animationRunning = true;
}
void CardSprite::Update(double dt)
{
if (!animationRunning)
return;
t += dt;
if (t >= animation.duration) // Animation is finished
{
SetPosition(animation.to);
animationRunning = false;
return;
}
double animVal = ParametricCurve(t, animation.alpha, animation.duration);
glm::vec3 distance = animation.to - animation.from;
glm::vec3 newPosition = animation.from + (float)animVal * distance;
SetPosition(newPosition);
}
void CardSprite::PreRender(const lol::CameraBase& camera) void CardSprite::PreRender(const lol::CameraBase& camera)
{ {
cards->Bind(); cards->Bind();

View file

@ -3,6 +3,14 @@
#include <lol/lol.hpp> #include <lol/lol.hpp>
#include "Card.hpp" #include "Card.hpp"
struct Animation
{
glm::vec3 from;
glm::vec3 to;
double alpha;
double duration;
};
class CardSprite : public lol::Drawable, public lol::Transformable class CardSprite : public lol::Drawable, public lol::Transformable
{ {
public: public:
@ -10,6 +18,10 @@ public:
glm::vec3 GetSize() { return glm::vec3(0.61035f, 1.0f, 0.0f) * GetScale(); } glm::vec3 GetSize() { return glm::vec3(0.61035f, 1.0f, 0.0f) * GetScale(); }
void CreateAnimation(Animation animation);
inline const Animation& GetAnimation() { return animation; }
void Update(double dt);
private: private:
void PreRender(const lol::CameraBase& camera) override; void PreRender(const lol::CameraBase& camera) override;
@ -17,5 +29,10 @@ private:
std::shared_ptr<lol::Texture2D> cards; std::shared_ptr<lol::Texture2D> cards;
glm::vec2 textureOffset; glm::vec2 textureOffset;
// TODO: Create Animation handler?
Animation animation;
double t;
bool animationRunning;
std::shared_ptr<Card> card; std::shared_ptr<Card> card;
}; };

View file

@ -1,5 +1,7 @@
#include "BoardLayer.hpp" #include "BoardLayer.hpp"
#include "../Application.hpp"
BoardLayer::BoardLayer(lol::ObjectManager& manager, const Board& board) : BoardLayer::BoardLayer(lol::ObjectManager& manager, const Board& board) :
lol::Layer("Board"), manager(manager), board(board) lol::Layer("Board"), manager(manager), board(board)
{ {
@ -7,6 +9,9 @@ BoardLayer::BoardLayer(lol::ObjectManager& manager, const Board& board) :
void BoardLayer::OnUpdate() void BoardLayer::OnUpdate()
{ {
double dt = Application::GetInstance().GetFrametime();
for (CardSprite& sprite : sprites)
sprite.Update(dt);
} }
void BoardLayer::OnRender(lol::CameraBase& camera) void BoardLayer::OnRender(lol::CameraBase& camera)
@ -21,14 +26,15 @@ void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
CardSprite& newCard = sprites.back(); CardSprite& newCard = sprites.back();
newCard.SetScale(glm::vec3(0.2f)); newCard.SetScale(glm::vec3(0.2f));
glm::vec3 cardPosition;
// If this is the first card to be put on the board, place it in the top left slot // If this is the first card to be put on the board, place it in the top left slot
if (sprites.size() == 1) if (sprites.size() == 1)
{ {
newCard.SetPosition(glm::vec3( cardPosition = glm::vec3(
0.25f + 0.05f, 0.25f + 0.05f,
0.5f + 0.025f, 0.5f + 0.025f,
0.0f 0.0f
)); );
} }
else else
{ {
@ -36,20 +42,35 @@ void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
if (sprites.size() % 2 == 0) if (sprites.size() % 2 == 0)
{ {
CardSprite& spriteBeforeLast = sprites[sprites.size() - 2]; CardSprite& spriteBeforeLast = sprites[sprites.size() - 2];
newCard.SetPosition(glm::vec3( cardPosition = glm::vec3(
spriteBeforeLast.GetPosition().x, spriteBeforeLast.GetAnimation().to.x,
spriteBeforeLast.GetPosition().y - newCard.GetSize().y - 0.05f, spriteBeforeLast.GetAnimation().to.y - newCard.GetSize().y - 0.05f,
0.0f 0.0f
)); );
} }
else // Else, place it in the next row else // Else, place it in the next row
{ {
CardSprite& secondSpriteBeforeLast = sprites[sprites.size() - 3]; CardSprite& secondSpriteBeforeLast = sprites[sprites.size() - 3];
newCard.SetPosition(glm::vec3( cardPosition = glm::vec3(
secondSpriteBeforeLast.GetPosition().x + newCard.GetSize().x + 0.05f, secondSpriteBeforeLast.GetAnimation().to.x + newCard.GetSize().x + 0.05f,
secondSpriteBeforeLast.GetPosition().y, secondSpriteBeforeLast.GetAnimation().to.y,
0.0f 0.0f
)); );
} }
} }
cardPosition.z = 1.0f + sprites.size();
Animation anim{
glm::vec3(
0.25f - 0.05 - newCard.GetSize().x,
0.5f - newCard.GetSize().y * 0.5f,
cardPosition.z
), // hardcode position of stack lol
cardPosition,
2.5f,
0.2f
};
newCard.CreateAnimation(anim);
} }