Peoplemon  0.1.0
Peoplemon 3 game source documentation
BattleController.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Logging.hpp>
6 
7 namespace core
8 {
9 namespace battle
10 {
12 : view(nullptr)
13 , state(nullptr)
14 , subState(SubState::Done) {}
15 
17  battle = &b;
18  view = &v;
19  state = &s;
20 }
21 
23  switch (subState) {
24  case SubState::WaitingView:
25  if (view->actionsCompleted()) {
26  subState = SubState::Done;
27  view->hideText();
28  }
29  else {
30  break;
31  }
32  [[fallthrough]];
33 
34  case SubState::Done:
35  if (!commandQueue.empty()) {
36  while (updateCommandQueue()) {}
37  }
38  break;
39 
40  default:
41  BL_LOG_WARN << "Unknown substate: " << subState;
42  subState = SubState::Done;
43  break;
44  }
45 
46  onUpdate(view->actionsCompleted(), commandQueue.empty());
47 }
48 
49 void BattleController::queueCommand(Command&& cmd, bool addWait) {
50  commandQueue.emplace(std::forward<Command>(cmd));
51  onCommandQueued(commandQueue.back());
52  if (addWait) {
53  commandQueue.emplace(Command::WaitForView);
54  onCommandQueued(commandQueue.back());
55  }
56 }
57 
58 bool BattleController::updateCommandQueue() {
59  if (commandQueue.empty()) return false;
60 
61  const Command cmd = std::move(commandQueue.front());
62  commandQueue.pop();
63  onCommandProcessed(cmd);
64 
65  switch (cmd.getType()) {
66  case Command::Type::DisplayMessage:
67  case Command::Type::PlayAnimation:
68  case Command::Type::SyncStateNoSwitch:
69  case Command::Type::SyncStateSwitch:
70  view->processCommand(cmd);
71  return true;
72 
73  case Command::Type::WaitForView:
74  subState = SubState::WaitingView;
75  return false;
76 
77  case Command::Type::NotifyBattleWinner:
78  // do nothing here
79  return true;
80 
81  default:
82  BL_LOG_WARN << "Unknown command type: " << cmd.getType();
83  return true;
84  }
85 }
86 
87 } // namespace battle
88 } // namespace core
Core classes and functionality for both the editor and game.
Convenience struct that owns all the components required for a battle and resolves some boilerplate s...
Definition: Battle.hpp:23
void init(Battle &battle, BattleView &view, BattleState &state)
Sets internal references to the view and state of the battle.
void queueCommand(Command &&command, bool addWait=false)
Enqueues a command to manipulate battle state or the view.
void update()
Updates the processing of the command queue.
BattleController()
Initializes some internal state.
virtual void onCommandQueued(const Command &cmd)=0
Called when a command is first enqueued but not processed. May be used to send commands over the netw...
virtual void onCommandProcessed(const Command &cmd)=0
This is called after a command is processed. Derived classes may perform specific actions for command...
virtual void onUpdate(bool viewSynced, bool queueEmpty)=0
Derived controllers may use this method for update logic every frame.
Stores and represents the current state of a battle. This is the base class for local and remote batt...
Definition: BattleState.hpp:18
This is the top level class for rendering battles. It takes commands and state from a BattleControlle...
Definition: BattleView.hpp:29
void hideText()
Hides the battle text when the view is synced.
Definition: BattleView.cpp:47
void processCommand(const Command &command)
Processes a command and updates the view.
Definition: BattleView.cpp:49
bool actionsCompleted() const
Returns true if the view is done going through the queued commands and all components are synchronize...
Definition: BattleView.cpp:42
Emitted by the BattleFSM. All UI is handled via a queue of commands. Commands are resolved in the ord...
Definition: Command.hpp:22
Type getType() const
Returns the type of this command.
Definition: Command.cpp:23