GigaPasjans
Loading...
Searching...
No Matches
InteractionManager.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 14.04.2025
2#pragma once
3
4#include <andromenda/ActionRegistry.h>
5#include <foundation/Collections.h>
6#include <foundation/Common.h>
7#include <game/Card/CardHelper.h>
8#include <game/Card/Deck.h>
9#include <phosphorus/GameBootstrapper.h>
10#include <phosphorus/GameSerializer.h>
11#include "GameRulesEngine.h"
12
13namespace Game
14{
15 class Card;
16
20 enum class InteractionType : uint8
21 {
22 StockTakeNextCard = 0,
23 BulkCard = 1,
24 Card = 2
25 };
26
31 {
32 virtual ~IInteraction() = default;
33
37 InteractionType Type;
38
42 virtual void Do() {}
43
47 virtual void Undo() {}
48
52 virtual void Serialize(nlohmann::json &obj) {}
53 };
54
55 struct StockNextCardInteraction;
56 struct CardInteraction;
57 struct BulkCardInteraction;
58
63 {
64 private:
65 friend class ReplaySystem;
66
70 inline static std::vector<IInteraction *> m_Motions;
71
75 inline static int32 m_AllowedReverts = 0;
76
80 static bool canRevert();
81
82 public:
86 inline static Deck *Deck = nullptr;
87
91 inline static std::function<void()> ReloadCallback;
92
94
98 static int32 GetMotionCount();
99
103 static void SetAllowedReverts(int32 allowedReverts);
104
108 static int32 GetAvailableReverts();
109
113 static void Clear();
114
118 template<typename T, class... Args>
119 static T *Push(InteractionType type, Args &&...args)
120 {
121 static_assert(std::is_base_of_v<IInteraction, T>, "T must be derived from IInteraction");
122
123 IInteraction *im = new T(std::forward<Args>(args)...);
124 im->Type = type;
125 im->Do();
126 m_Motions.push_back(im);
128
130
131 return static_cast<T *>(im);
132 }
133
137 static void TakeStockNextCard();
138
142 static void MoveCard(Card *card, CardStack *targetStack, bool authorizedMove = false);
143
147 static void MoveCards(const std::vector<Card *> &cards, CardStack *targetStack);
148
152 static void SaveMovement(std::ostream &stream);
153
157 static void LoadMovement(std::istream &stream);
158
162 static bool ProvideActions(Andromenda::ActionRegistry &registry);
163
167 static void PlayIllegalSound();
168
169 static void Undo();
170 };
171
175 struct BulkCardInteraction : IInteraction
176 {
177 private:
178 BulkCardInteraction() = default;
179
180 public:
184 inline static std::vector<Card *> GrabbedCards;
185
189 std::vector<Card *> Cards;
190
195
200
205
210
211 BulkCardInteraction(const std::vector<Card *> &cards, CardStack *stack);
212
214 void Do() override;
215
217 void Undo() override;
218
222 void Serialize(nlohmann::json &obj) override;
223
227 static BulkCardInteraction *Load(nlohmann::json &obj);
228 };
229
233 struct CardInteraction : IInteraction
234 {
235 private:
236 CardInteraction() = default;
237
238 public:
242 inline static Card *Grabbed = nullptr;
243
248
253
258
263
268
269 CardInteraction(::Game::Card *card, CardStack *stack);
270
272 void Do() override;
273
275 void Undo() override;
276
280 void Serialize(nlohmann::json &obj) override;
281
285 static CardInteraction *Load(nlohmann::json &obj);
286 };
287
291 struct StockNextCardInteraction : IInteraction
292 {
296 inline static int32 StackIndex = 0;
297
301 int32 Index;
302
306 int32 NewIndex;
307
308 StockNextCardInteraction();
309
311 void Do() override;
312
314 void Undo() override;
315
319 static Card *GetCard() { return InteractionManager::Deck->Stock->Get(StackIndex); }
320
324 static int32 GetStockSize();
325
329 void Serialize(nlohmann::json &obj) override;
330
334 static StockNextCardInteraction *Load(nlohmann::json &obj);
335 };
336} // namespace Game
Represent registry of Action objects.
Definition ActionRegistry.h:74
Represents a playing card in a game. This class defines a card with properties such as color,...
Definition Card.h:61
Represents a stack of cards in the game.
Definition Deck.h:14
static void CheckForWin()
Evaluates the game state and triggers win logic if necessary.
Definition GameRulesEngine.cpp:116
Handles logic for managing and performing card interactions.
Definition InteractionManager.h:63
static T * Push(InteractionType type, Args &&...args)
Pushes a new interaction to the history and executes it.
Definition InteractionManager.h:119
static void SetAllowedReverts(int32 allowedReverts)
Sets the allowed number of interaction reverts.
Definition InteractionManager.cpp:42
static int32 GetAvailableReverts()
Gets the number of reverts remaining.
Definition InteractionManager.cpp:238
static void SaveMovement(std::ostream &stream)
Saves the current interaction history to a stream.
Definition InteractionManager.cpp:21
static Deck * Deck
Pointer to the game deck used in interactions.
Definition InteractionManager.h:86
static void TakeStockNextCard()
Draws the next card from the stock.
Definition InteractionManager.cpp:195
static int32 GetMotionCount()
Returns the number of interactions in the motion stack.
Definition InteractionManager.cpp:44
static bool ProvideActions(Andromenda::ActionRegistry &registry)
Registers actions in the provided action registry.
Definition InteractionManager.cpp:286
static void PlayIllegalSound()
Plays a sound for invalid interactions.
Definition InteractionManager.cpp:337
static void MoveCard(Card *card, CardStack *targetStack, bool authorizedMove=false)
Moves a single card to the given target stack.
Definition InteractionManager.cpp:197
static void Clear()
Clears all stored interaction history.
Definition InteractionManager.cpp:13
static void MoveCards(const std::vector< Card * > &cards, CardStack *targetStack)
Moves a group of cards to the given target stack.
Definition InteractionManager.cpp:213
static void LoadMovement(std::istream &stream)
Loads interaction history from a stream.
Definition InteractionManager.cpp:243
static std::function< void()> ReloadCallback
Callback triggered after any interaction is performed.
Definition InteractionManager.h:91
void Do() override
Executes the interaction.
Definition InteractionManager.cpp:78
int32 PreviousIndex
Index of the first moved card in the original stack.
Definition InteractionManager.h:204
void Undo() override
Reverts the interaction.
Definition InteractionManager.cpp:94
std::vector< Card * > Cards
Cards involved in the interaction.
Definition InteractionManager.h:189
void Serialize(nlohmann::json &obj) override
Serializes the bulk interaction to JSON.
Definition InteractionManager.cpp:106
CardStack * Stack
Destination stack.
Definition InteractionManager.h:194
static std::vector< Card * > GrabbedCards
Cards currently being moved (global state).
Definition InteractionManager.h:184
int32 FlippedCardIndex
Index of any card that was flipped due to this move.
Definition InteractionManager.h:209
CardStack * PreviousStack
Stack from which the cards originated.
Definition InteractionManager.h:199
static BulkCardInteraction * Load(nlohmann::json &obj)
Loads a bulk interaction from JSON.
Definition InteractionManager.cpp:121
static CardInteraction * Load(nlohmann::json &obj)
Loads a card interaction from JSON.
Definition InteractionManager.cpp:176
int32 FlippedCardIndex
Index of a card flipped during the move, if any.
Definition InteractionManager.h:267
void Do() override
Executes the interaction.
Definition InteractionManager.cpp:142
static Card * Grabbed
Currently selected card (global state).
Definition InteractionManager.h:242
Card * Card
The card involved in the interaction.
Definition InteractionManager.h:247
void Serialize(nlohmann::json &obj) override
Serializes the interaction to JSON.
Definition InteractionManager.cpp:167
int32 PreviousIndex
Original index of the card.
Definition InteractionManager.h:262
CardStack * Stack
Destination stack.
Definition InteractionManager.h:252
CardStack * PreviousStack
Original stack before the move.
Definition InteractionManager.h:257
void Undo() override
Reverts the interaction.
Definition InteractionManager.cpp:158
Base interface for all interaction types.
Definition InteractionManager.h:31
virtual void Serialize(nlohmann::json &obj)
Serializes the interaction to JSON.
Definition InteractionManager.h:52
virtual void Do()
Executes the interaction.
Definition InteractionManager.h:42
InteractionType Type
Type of the interaction.
Definition InteractionManager.h:37
virtual void Undo()
Reverts the interaction.
Definition InteractionManager.h:47
Represents drawing the next card from the stock pile.
Definition InteractionManager.h:292
static int32 GetStockSize()
Returns the current size of the stock stack.
Definition InteractionManager.cpp:53
int32 Index
Index of the card before the draw.
Definition InteractionManager.h:301
void Undo() override
Reverts the interaction.
Definition InteractionManager.cpp:345
int32 NewIndex
Index of the card after the draw.
Definition InteractionManager.h:306
void Serialize(nlohmann::json &obj) override
Serializes the stock interaction to JSON.
Definition InteractionManager.cpp:58
static Card * GetCard()
Gets the currently selected card from the stock.
Definition InteractionManager.h:319
void Do() override
Executes the interaction.
Definition InteractionManager.cpp:339
static int32 StackIndex
Current index in the stock pile.
Definition InteractionManager.h:296
static StockNextCardInteraction * Load(nlohmann::json &obj)
Loads a stock interaction from JSON.
Definition InteractionManager.cpp:64