added more animations (im just stalling the inevitable eventually i will have to sit down and write a proper event system but for now i am just making these animatons)

This commit is contained in:
Lauchmelder 2022-01-16 16:56:24 +01:00
parent 758d577609
commit dd671571b0
10 changed files with 106 additions and 23 deletions

View file

@ -11,11 +11,23 @@ Board::Board(Game* parent) :
void Board::RevealCard() void Board::RevealCard()
{ {
openCards.push_back(parent->GetStack().DrawCard()); openCards.push_back(parent->GetStack().DrawCard());
if (callback) if (revealCallback)
callback(openCards.back()); revealCallback(openCards.back());
}
void Board::Cleanup()
{
openCards.clear();
if (cleanupCallback)
cleanupCallback();
} }
void Board::SetRevealCallback(RevealCallbackFunc callbackFunc) void Board::SetRevealCallback(RevealCallbackFunc callbackFunc)
{ {
callback = callbackFunc; revealCallback = callbackFunc;
}
void Board::SetCleanupCallback(CleanupCallbackFunc callbackFunc)
{
cleanupCallback = callbackFunc;
} }

View file

@ -6,22 +6,33 @@
#include "Card.hpp" #include "Card.hpp"
typedef std::function<void(std::shared_ptr<Card>)> RevealCallbackFunc; typedef std::function<void(std::shared_ptr<Card>)> RevealCallbackFunc;
typedef std::function<void(void)> CleanupCallbackFunc;
class Game; class Game;
enum class BoardState
{
Empty,
HasCards,
C
};
class Board class Board
{ {
public: public:
Board(Game* parent); Board(Game* parent);
void RevealCard(); void RevealCard();
void Cleanup();
inline const std::vector<std::shared_ptr<Card>>& GetOpenCards() { return openCards; } inline const std::vector<std::shared_ptr<Card>>& GetOpenCards() { return openCards; }
void SetRevealCallback(RevealCallbackFunc callbackFunc); void SetRevealCallback(RevealCallbackFunc callbackFunc);
void SetCleanupCallback(CleanupCallbackFunc callbackFunc);
private: private:
std::vector<std::shared_ptr<Card>> openCards; std::vector<std::shared_ptr<Card>> openCards;
RevealCallbackFunc callback; RevealCallbackFunc revealCallback;
CleanupCallbackFunc cleanupCallback;
Game* parent; Game* parent;
}; };

View file

@ -16,7 +16,7 @@ CardStack::CardStack()
} }
// Shuffle stack // Shuffle stack
std::default_random_engine engine(time(0)); std::default_random_engine engine(std::chrono::steady_clock::now().time_since_epoch().count());
std::shuffle(stack.begin(), stack.end(), engine); std::shuffle(stack.begin(), stack.end(), engine);
} }

View file

@ -11,4 +11,10 @@ void Game::Setup()
// Put 8 cards on the table // Put 8 cards on the table
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
board.RevealCard(); board.RevealCard();
} }
void Game::Cleanup()
{
stack = CardStack();
board.Cleanup();
}

View file

@ -10,6 +10,7 @@ public:
~Game() {} ~Game() {}
void Setup(); void Setup();
void Cleanup();
inline CardStack& GetStack() { return stack; } inline CardStack& GetStack() { return stack; }
inline Board& GetBoard() { return board; } inline Board& GetBoard() { return board; }

View file

@ -53,6 +53,12 @@ void Application::OnKeyPressed(unsigned int character)
if(!game.GetStack().Empty()) if(!game.GetStack().Empty())
game.GetBoard().RevealCard(); game.GetBoard().RevealCard();
} }
if (character == 'r' || character == 'R')
{
game.Cleanup();
game.Setup();
}
} }
Application::Application() : Application::Application() :
@ -94,8 +100,10 @@ Application::Application() :
spdlog::debug("Setting up layers"); spdlog::debug("Setting up layers");
layerStack.push_back(new CardStackLayer(manager, game.GetStack())); layerStack.push_back(new CardStackLayer(manager, game.GetStack()));
BoardLayer* boardLayer = new BoardLayer(manager, game.GetBoard()); BoardLayer* boardLayer = new BoardLayer(manager, game.GetBoard());
game.GetBoard().SetRevealCallback(std::bind(&BoardLayer::OnRevealCard, boardLayer, std::placeholders::_1)); game.GetBoard().SetRevealCallback(std::bind(&BoardLayer::OnRevealCard, boardLayer, std::placeholders::_1));
game.GetBoard().SetCleanupCallback(std::bind(&BoardLayer::OnCleanup, boardLayer));
layerStack.push_back(boardLayer); layerStack.push_back(boardLayer);
spdlog::debug("Setting up game"); spdlog::debug("Setting up game");

View file

@ -66,6 +66,7 @@ void CardSprite::Update(double dt)
if (t >= animation.duration) // Animation is finished if (t >= animation.duration) // Animation is finished
{ {
SetPosition(animation.to); SetPosition(animation.to);
animation.onAnimationEnd(this);
animationRunning = false; animationRunning = false;
return; return;
} }

View file

@ -3,12 +3,15 @@
#include <lol/lol.hpp> #include <lol/lol.hpp>
#include "Card.hpp" #include "Card.hpp"
class CardSprite;
struct Animation struct Animation
{ {
glm::vec3 from; glm::vec3 from;
glm::vec3 to; glm::vec3 to;
double alpha; double alpha;
double duration; double duration;
std::function<void(CardSprite*)> onAnimationEnd = [](CardSprite*) {};
}; };
class CardSprite : public lol::Drawable, public lol::Transformable class CardSprite : public lol::Drawable, public lol::Transformable

View file

@ -10,21 +10,27 @@ BoardLayer::BoardLayer(lol::ObjectManager& manager, const Board& board) :
void BoardLayer::OnUpdate() void BoardLayer::OnUpdate()
{ {
double dt = Application::GetInstance().GetFrametime(); double dt = Application::GetInstance().GetFrametime();
for (CardSprite& sprite : sprites) for (CardSprite* sprite : sprites)
sprite.Update(dt); sprite->Update(dt);
for (CardSprite* sprite : oldSprites)
sprite->Update(dt);
} }
void BoardLayer::OnRender(lol::CameraBase& camera) void BoardLayer::OnRender(lol::CameraBase& camera)
{ {
for (CardSprite& sprite : sprites) for (CardSprite* sprite : sprites)
camera.Draw(sprite); camera.Draw(*sprite);
for (CardSprite* sprite : oldSprites)
camera.Draw(*sprite);
} }
void BoardLayer::OnRevealCard(std::shared_ptr<Card> card) void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
{ {
sprites.push_back(CardSprite(manager, card)); sprites.push_back(new CardSprite(manager, card));
CardSprite& newCard = sprites.back(); CardSprite* newCard = sprites.back();
newCard.SetScale(glm::vec3(0.2f)); newCard->SetScale(glm::vec3(0.2f));
glm::vec3 cardPosition; 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
@ -41,19 +47,19 @@ void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
// If an even number of cards has been drawn, placce the last one below the previous one // If an even number of cards has been drawn, placce the last one below the previous one
if (sprites.size() % 2 == 0) if (sprites.size() % 2 == 0)
{ {
CardSprite& spriteBeforeLast = sprites[sprites.size() - 2]; CardSprite* spriteBeforeLast = sprites[sprites.size() - 2];
cardPosition = glm::vec3( cardPosition = glm::vec3(
spriteBeforeLast.GetAnimation().to.x, spriteBeforeLast->GetAnimation().to.x,
spriteBeforeLast.GetAnimation().to.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];
cardPosition = glm::vec3( cardPosition = glm::vec3(
secondSpriteBeforeLast.GetAnimation().to.x + newCard.GetSize().x + 0.05f, secondSpriteBeforeLast->GetAnimation().to.x + newCard->GetSize().x + 0.05f,
secondSpriteBeforeLast.GetAnimation().to.y, secondSpriteBeforeLast->GetAnimation().to.y,
0.0f 0.0f
); );
} }
@ -63,8 +69,8 @@ void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
Animation anim{ Animation anim{
glm::vec3( glm::vec3(
0.25f - 0.05 - newCard.GetSize().x, 0.25f - 0.05 - newCard->GetSize().x,
0.5f - newCard.GetSize().y * 0.5f, 0.5f - newCard->GetSize().y * 0.5f,
cardPosition.z cardPosition.z
), // hardcode position of stack lol ), // hardcode position of stack lol
cardPosition, cardPosition,
@ -72,5 +78,35 @@ void BoardLayer::OnRevealCard(std::shared_ptr<Card> card)
0.2f 0.2f
}; };
newCard.CreateAnimation(anim); newCard->CreateAnimation(anim);
}
void BoardLayer::OnCleanup()
{
for (CardSprite* card : sprites)
{
Animation anim{
card->GetPosition(),
glm::vec3(
0.25f - 0.05 - card->GetSize().x,
0.5f - card->GetSize().y * 0.5f,
card->GetPosition().z
),
2.5f,
0.2f,
std::bind(&BoardLayer::OnCleanupAnimationEnd, this, std::placeholders::_1)
};
card->CreateAnimation(anim);
oldSprites.push_back(card);
}
sprites.clear();
}
void BoardLayer::OnCleanupAnimationEnd(CardSprite* sprite)
{
oldSprites.erase(std::find(oldSprites.begin(), oldSprites.end(), sprite));
delete sprite;
} }

View file

@ -13,10 +13,15 @@ public:
void OnRender(lol::CameraBase& camera) override; void OnRender(lol::CameraBase& camera) override;
void OnRevealCard(std::shared_ptr<Card> card); void OnRevealCard(std::shared_ptr<Card> card);
void OnCleanup();
private:
void OnCleanupAnimationEnd(CardSprite* sprite);
private: private:
const Board& board; const Board& board;
lol::ObjectManager& manager; lol::ObjectManager& manager;
std::vector<CardSprite> sprites; std::vector<CardSprite*> sprites;
std::vector<CardSprite*> oldSprites;
}; };