Peoplemon  0.1.0
Peoplemon 3 game source documentation
BattleView.cpp
Go to the documentation of this file.
2 
4 #include <Core/Properties.hpp>
5 #include <Core/Resources.hpp>
6 
7 namespace core
8 {
9 namespace battle
10 {
11 BattleView::BattleView(bl::engine::Engine& engine, BattleState& s, bool canRun)
12 : engine(engine)
13 , battleState(s)
14 , playerMenu(engine, canRun)
15 , statBoxes(engine)
16 , printer(engine)
17 , localPeoplemon(engine, view::PeoplemonAnimation::Player)
18 , opponentPeoplemon(engine, view::PeoplemonAnimation::Opponent)
19 , moveAnimation(engine) {}
20 
21 void BattleView::init(bl::rc::scene::CodeScene* scene) {
22  bgndTxtr = engine.renderer().texturePool().getOrLoadTexture(
23  bl::util::FileUtil::joinPath(Properties::ImagePath(), "Battle/battleBgnd.png"));
24  background.create(engine, bgndTxtr);
25  background.addToScene(scene, bl::rc::UpdateSpeed::Static);
26 
27  printer.init(scene);
28  statBoxes.init(scene);
29  moveAnimation.init(scene);
30  playerMenu.init(scene);
31  localPeoplemon.init(scene);
32  opponentPeoplemon.init(scene);
33 
34  playerMenu.setPeoplemon(battleState.localPlayer().outNowIndex(),
35  battleState.localPlayer().activePeoplemon());
36  localPeoplemon.setPeoplemon(battleState.localPlayer().activePeoplemon().base().id());
37  opponentPeoplemon.setPeoplemon(battleState.enemy().activePeoplemon().base().id());
38  moveAnimation.ensureLoaded(battleState.localPlayer().activePeoplemon(),
39  battleState.enemy().activePeoplemon());
40 }
41 
43  return statBoxes.synced() && moveAnimation.completed() && localPeoplemon.completed() &&
44  opponentPeoplemon.completed() && printer.messageDone() && playerMenu.ready();
45 }
46 
47 void BattleView::hideText() { printer.hide(); }
48 
50  using Animation = cmd::Animation;
51 
52  switch (cmd.getType()) {
53  case Command::Type::DisplayMessage:
54  printer.setMessage(battleState, cmd.getMessage());
55  break;
56 
57  case Command::Type::PlayAnimation: {
58  const bool userIsPlayer =
59  cmd.getAnimation().forHostBattler() == battleState.localPlayer().isHost();
60 
61  switch (cmd.getAnimation().getType()) {
62  case Animation::Type::UseMove:
63  moveAnimation.playAnimation(userIsPlayer ? view::MoveAnimation::Player :
65  cmd.getAnimation().getMove());
66  break;
67 
68  case Animation::Type::PlayerFirstSendout:
69  localPeoplemon.triggerAnimation(Animation::Type::SendOut);
70  break;
71 
72  case Animation::Type::OpponentFirstSendout:
73  opponentPeoplemon.triggerAnimation(Animation::Type::SendOut);
74  break;
75 
76  default: {
77  auto& anim = userIsPlayer ? localPeoplemon : opponentPeoplemon;
78  anim.triggerAnimation(cmd.getAnimation());
79  } break;
80  }
81  } break;
82 
83  case Command::Type::SyncStateNoSwitch:
84  statBoxes.sync();
85  break;
86 
87  case Command::Type::SyncStateSwitch: {
88  const bool isPlayer = cmd.forHost() == battleState.localPlayer().isHost();
89 
90  if (isPlayer) {
91  statBoxes.setPlayer(&battleState.localPlayer().activePeoplemon());
92  playerMenu.setPeoplemon(battleState.localPlayer().outNowIndex(),
93  battleState.localPlayer().activePeoplemon());
94  localPeoplemon.setPeoplemon(battleState.localPlayer().activePeoplemon().base().id());
95  }
96  else {
97  statBoxes.setOpponent(&battleState.enemy().activePeoplemon());
98  opponentPeoplemon.setPeoplemon(battleState.enemy().activePeoplemon().base().id());
99  }
100  moveAnimation.ensureLoaded(battleState.localPlayer().activePeoplemon(),
101  battleState.enemy().activePeoplemon());
102  } break;
103 
104  default:
105  BL_LOG_ERROR << "Unknown command type: " << cmd.getType();
106  break;
107  }
108 }
109 
110 view::PlayerMenu& BattleView::menu() { return playerMenu; }
111 
112 bool BattleView::playerChoseForgetMove() const { return printer.choseToForget(); }
113 
114 bool BattleView::playerChoseToSetName() const { return printer.choseToSetName(); }
115 
116 const std::string& BattleView::chosenNickname() const { return printer.chosenNickname(); }
117 
118 void BattleView::update(float dt) {
119  statBoxes.update(dt);
120  printer.update(dt);
121  localPeoplemon.update(dt);
122  opponentPeoplemon.update(dt);
123  playerMenu.refresh();
124 }
125 
126 void BattleView::render(bl::rc::scene::CodeScene::RenderContext& ctx) {
127  background.draw(ctx);
128 
129  statBoxes.render(ctx);
130  moveAnimation.renderBackground(ctx);
131  localPeoplemon.render(ctx);
132  opponentPeoplemon.render(ctx);
133  moveAnimation.renderForeground(ctx);
134  if ((battleState.currentStage() == BattleState::Stage::WaitingChoices ||
136  !playerMenu.ready()) {
137  playerMenu.render(ctx);
138  }
139  printer.render(ctx);
140 }
141 
142 bool BattleView::observe(const bl::input::Actor&, unsigned int ctrl, bl::input::DispatchType,
143  bool eventTriggered) {
146  !playerMenu.ready()) {
147  playerMenu.handleInput(ctrl, eventTriggered);
148  }
149  else { printer.process(ctrl, eventTriggered); }
150  return true;
151 }
152 
153 } // namespace battle
154 } // namespace core
Core classes and functionality for both the editor and game.
unsigned int outNowIndex() const
Returns the index of the peoplemon that is out now.
Definition: Battler.cpp:66
core::pplmn::BattlePeoplemon & activePeoplemon()
Returns the peoplemon that is currently out.
Definition: Battler.cpp:59
bool isHost() const
Returns whether or not this battler is the host.
Definition: Battler.cpp:164
Stores and represents the current state of a battle. This is the base class for local and remote batt...
Definition: BattleState.hpp:18
Stage currentStage() const
Returns the current stage the battle is in.
Definition: BattleState.cpp:42
Battler & enemy()
Returns the opponent Battler.
Definition: BattleState.cpp:28
Battler & localPlayer()
Returns the local player Battler.
Definition: BattleState.cpp:26
view::PlayerMenu & menu()
Access the player's menu.
Definition: BattleView.cpp:110
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the battle view.
Definition: BattleView.cpp:126
void init(bl::rc::scene::CodeScene *scene)
Initializes renderer data, loads resources, and adds entities to the scene.
Definition: BattleView.cpp:21
bool playerChoseToSetName() const
Returns whether or not the player chose to set a nickname on a new Peoplemon.
Definition: BattleView.cpp:114
bool playerChoseForgetMove() const
Returns whether or not the player chose to forget a move when prompted.
Definition: BattleView.cpp:112
void update(float dt)
Updates the view, including contained animations and printing text.
Definition: BattleView.cpp:118
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
const std::string & chosenNickname() const
Returns the nickname the player entered.
Definition: BattleView.cpp:116
bool actionsCompleted() const
Returns true if the view is done going through the queued commands and all components are synchronize...
Definition: BattleView.cpp:42
BattleView(bl::engine::Engine &engine, BattleState &state, bool canRun)
Construct a new Battle View.
Definition: BattleView.cpp:11
Represents a type of animation that can be played in battle.
Definition: Animation.hpp:25
pplmn::MoveId getMove() const
Returns the move for this animation.
Definition: Animation.cpp:48
bool forHostBattler() const
Returns whether or not this animation is for the host or not.
Definition: Animation.cpp:44
Type getType() const
Returns the type of animation to play.
Definition: Animation.cpp:46
Emitted by the BattleFSM. All UI is handled via a queue of commands. Commands are resolved in the ord...
Definition: Command.hpp:22
bool forHost() const
Returns whether or not this command is for the active battler.
Definition: Command.cpp:37
const cmd::Message & getMessage() const
Returns the message if this command is a message.
Definition: Command.cpp:25
Type getType() const
Returns the type of this command.
Definition: Command.cpp:23
const cmd::Animation & getAnimation() const
Returns the animation if this command is an animation.
Definition: Command.cpp:31
const std::string & chosenNickname() const
Returns the nickname chosen by the player.
void process(input::EntityControl ctrl, bool ignoreDebounce)
Displays the full message. Call when user presses the continue button.
void update(float dt)
Updates the ghost writer.
void hide()
Hides the text once the view is fully synced.
bool messageDone() const
Returns true when the full message is on display and finishPrint() has been called.
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the message.
bool choseToSetName() const
Whether or not the player chose to set a nickname.
void init(bl::rc::scene::CodeScene *scene)
Initializes the UI components and adds to scene.
bool choseToForget() const
Returns whether or not the player chose to forget a move.
void setMessage(BattleState &state, const cmd::Message &message)
Set the message to be printed.
void renderBackground(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the move animation background.
void ensureLoaded(const pplmn::BattlePeoplemon &player, const pplmn::BattlePeoplemon &opponent)
Loads the move animations into the resource manager for the given peoplemon.
void playAnimation(User user, pplmn::MoveId move)
Begins playing the given move animation.
bool completed() const
Returns whether or not the animation has completed playing.
void init(bl::rc::scene::CodeScene *scene)
Initializes the animations.
void renderForeground(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the move animation foreground.
void init(bl::rc::scene::CodeScene *scene)
Initializes rendering resources.
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the peoplemon animation.
void triggerAnimation(const cmd::Animation &anim)
Begins playing the given animation.
bool completed() const
Returns true if the animation has completed, false if in progress.
void update(float dt)
Updates the playing animation.
void setPeoplemon(pplmn::Id ppl)
Sets the specific peoplemon graphic and resets to hidden state.
The player menu for battles.
Definition: PlayerMenu.hpp:29
void handleInput(input::EntityControl input, bool ignoreDebounce)
Processes player input to update the menu state.
Definition: PlayerMenu.cpp:198
void setPeoplemon(int i, const pplmn::BattlePeoplemon &ppl)
Updates the menu for the given peoplemon.
Definition: PlayerMenu.cpp:93
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the menu.
Definition: PlayerMenu.cpp:213
void init(bl::rc::scene::CodeScene *scene)
Creates UI elements and adds them to the scene.
Definition: PlayerMenu.cpp:31
void refresh()
Polls state from opened peoplemon or bag menu.
Definition: PlayerMenu.cpp:132
bool ready() const
Returns true when the player's choice has been made.
Definition: PlayerMenu.cpp:188
void sync(bool fromSwitch=false)
Updates the UI to reflect the current peoplemon state.
Definition: StatBoxes.cpp:139
void init(bl::rc::scene::CodeScene *scene)
Creates entities and adds them to the scene.
Definition: StatBoxes.cpp:49
void update(float dt)
Updates bar sizes if they need to change.
Definition: StatBoxes.cpp:176
void setOpponent(pplmn::BattlePeoplemon *ppl)
Sets the opponent's peoplemon. Syncs right away.
Definition: StatBoxes.cpp:124
void setPlayer(pplmn::BattlePeoplemon *ppl)
Sets the player's peoplemon. Syncs right away.
Definition: StatBoxes.cpp:131
bool synced() const
Returns true if the view is in sync, false if an animation is in progress.
Definition: StatBoxes.cpp:241
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the boxes.
Definition: StatBoxes.cpp:246
OwnedPeoplemon & base()
Returns the wrapped peoplemon.
Id id() const
Returns the id of this peoplemon.
static const std::string & ImagePath()
Definition: Properties.cpp:333