Peoplemon  0.1.0
Peoplemon 3 game source documentation
MoveAnimation.cpp
Go to the documentation of this file.
2 
4 #include <Core/Resources.hpp>
5 
6 namespace core
7 {
8 namespace battle
9 {
10 namespace view
11 {
12 namespace
13 {
14 std::string getMoveForeground(MoveAnimation::User user, pplmn::MoveId mid) {
15  return user == MoveAnimation::User::Player ? pplmn::Move::playerAnimationForeground(mid) :
16  pplmn::Move::opponentAnimationForeground(mid);
17 }
18 
19 std::string getMoveBackground(MoveAnimation::User user, pplmn::MoveId mid) {
20  return user == MoveAnimation::User::Player ? pplmn::Move::playerAnimationBackground(mid) :
21  pplmn::Move::opponentAnimationBackground(mid);
22 }
23 } // namespace
24 
25 MoveAnimation::MoveAnimation(bl::engine::Engine& engine)
26 : engine(engine)
27 , scene(nullptr)
28 , playing(nullptr) {}
29 
30 void MoveAnimation::init(bl::rc::scene::CodeScene* s) { scene = s; }
31 
33  const pplmn::BattlePeoplemon& op) {
34  for (int i = 0; i < 4; ++i) {
35  const auto mid = lp.base().knownMoves()[i].id;
36  if (mid != pplmn::MoveId::Unknown && mid != playerAnims[i].move) {
37  playerAnims[i].init(engine, scene, User::Player, mid);
38  }
39  }
40 
41  for (int i = 0; i < 4; ++i) {
42  const auto mid = op.base().knownMoves()[i].id;
43  if (mid != pplmn::MoveId::Unknown && mid != opponentAnims->move) {
44  opponentAnims[i].init(engine, scene, User::Opponent, mid);
45  }
46  }
47 }
48 
50  playing = nullptr;
51  auto& toSearch = user == User::Player ? playerAnims : opponentAnims;
52  for (unsigned int i = 0; i < 4; ++i) {
53  if (toSearch[i].move == move && toSearch[i].valid) {
54  playing = &toSearch[i];
55  break;
56  }
57  }
58  if (!playing) {
59  if (extraMove.init(engine, scene, user, move)) { playing = &extraMove; }
60  else {
61  BL_LOG_ERROR << "Missing animation for move " << move;
62  return;
63  }
64  }
65 
66  playing->play();
67 }
68 
69 bool MoveAnimation::completed() const { return !playing || playing->finished(); }
70 
71 void MoveAnimation::renderBackground(bl::rc::scene::CodeScene::RenderContext& ctx) {
72  if (playing) {
73  if (playing->background.getPlayer().playing()) { playing->background.draw(ctx); }
74  }
75 }
76 
77 void MoveAnimation::renderForeground(bl::rc::scene::CodeScene::RenderContext& ctx) {
78  if (playing) {
79  if (playing->foreground.getPlayer().playing()) { playing->foreground.draw(ctx); }
80  }
81 }
82 
83 MoveAnimation::Anim::Anim()
84 : move(pplmn::MoveId::Unknown)
85 , valid(false) {}
86 
87 bool MoveAnimation::Anim::init(bl::engine::Engine& engine, bl::rc::scene::CodeScene* scene,
88  User user, pplmn::MoveId mid) {
89  move = mid;
90 
91  auto bgSrc = AnimationManager::load(getMoveBackground(user, mid));
92  auto fgSrc = AnimationManager::load(getMoveForeground(user, mid));
93  if (bgSrc->frameCount() == 0 || fgSrc->frameCount() == 0) {
94  valid = false;
95  return false;
96  }
97 
98  background.createWithUniquePlayer(engine, bgSrc);
99  foreground.createWithUniquePlayer(engine, fgSrc);
100  background.addToScene(scene, bl::rc::UpdateSpeed::Static);
101  foreground.addToScene(scene, bl::rc::UpdateSpeed::Static);
102 
103  valid = true;
104  return true;
105 }
106 
107 void MoveAnimation::Anim::play() {
108  if (valid) {
109  background.getPlayer().play(true);
110  foreground.getPlayer().play(true);
111  }
112 }
113 
114 bool MoveAnimation::Anim::finished() const {
115  return !valid || (!background.getPlayer().playing() && !foreground.getPlayer().playing());
116 }
117 
118 } // namespace view
119 } // namespace battle
120 } // namespace core
MoveId
The id of a move.
Definition: MoveId.hpp:16
Core classes and functionality for both the editor and game.
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.
MoveAnimation(bl::engine::Engine &engine)
Construct a new Move Animation helper.
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.
User
Which peoplemon is using the move.
void renderForeground(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the move animation foreground.
Represents a Peoplemon in battle. Stores some extra state that only exists in battle....
OwnedPeoplemon & base()
Returns the wrapped peoplemon.
static std::string playerAnimationBackground(MoveId move)
Returns the path to the animation for when the local player uses the move.
Definition: Move.cpp:136
static std::string playerAnimationForeground(MoveId move)
Returns the path to the animation for when the local player uses the move.
Definition: Move.cpp:142
MoveId id
The id of the move.
Definition: OwnedMove.hpp:19
const OwnedMove * knownMoves() const
Returns the moves known by this Peoplemon.