Peoplemon  0.1.0
Peoplemon 3 game source documentation
Trainer.cpp
Go to the documentation of this file.
1 #include <Core/Files/Trainer.hpp>
2 
3 #include <Core/Items/Item.hpp>
4 #include <Core/Properties.hpp>
5 #include <Core/Resources.hpp>
6 
7 namespace core
8 {
9 namespace file
10 {
12 : payout(0) {}
13 
14 bool Trainer::save(const std::string& file) const {
15  std::ofstream output(file.c_str());
16  return bl::serial::json::Serializer<Trainer>::serializeStream(output, *this, 4, 0);
17 }
18 
19 bool Trainer::saveBundle(bl::serial::binary::OutputStream& output,
20  bl::resource::bundle::FileHandlerContext& ctx) const {
21  if (!bl::serial::binary::Serializer<Trainer>::serialize(output, *this)) return false;
22 
23  const std::array<std::string, 4> Suffixes{"up.anim", "right.anim", "down.anim", "left.anim"};
24  const std::string ap =
25  bl::util::FileUtil::joinPath(Properties::CharacterAnimationPath(), animation);
26  for (const std::string& s : Suffixes) {
27  const std::string p = bl::util::FileUtil::joinPath(ap, s);
28  if (bl::util::FileUtil::exists(p)) { ctx.addDependencyFile(p); }
29  else { BL_LOG_WARN << "Trainer " << name << " is missing anim: " << p; }
30  }
31 
32  const auto addConv = [this, &ctx](const std::string& cf) {
33  const std::string c = bl::util::FileUtil::joinPath(Properties::ConversationPath(), cf);
34  if (bl::util::FileUtil::exists(c)) { ctx.addDependencyFile(c); }
35  else { BL_LOG_WARN << "Trainer " << name << " is missing conversation " << cf; }
36  };
37  addConv(prebattleConversation);
38  addConv(postBattleConversation);
39 
40  return true;
41 }
42 
43 bool Trainer::load(const std::string& file, bl::tmap::Direction spawnDir) {
44  sourceFile = file;
45  if (!TrainerManager::initializeExisting(file, *this)) return false;
46  if (behavior.type() == Behavior::StandStill) { behavior.standing().facedir = spawnDir; }
47  if (payout == 0) { payout = 40; }
48  for (auto& ppl : peoplemon) { ppl.heal(); }
49  return true;
50 }
51 
52 bool Trainer::loadProd(bl::serial::binary::InputStream& input) {
53  return bl::serial::binary::Serializer<Trainer>::deserialize(input, *this);
54 }
55 
56 bool Trainer::loadDev(std::istream& input) {
57  return bl::serial::json::Serializer<Trainer>::deserializeStream(input, *this);
58 }
59 
60 } // namespace file
61 } // namespace core
Core classes and functionality for both the editor and game.
Type type() const
The type of behavior this is.
Definition: Behavior.cpp:75
Standing & standing()
The data for standing still.
Definition: Behavior.cpp:102
@ StandStill
The character will stand and face a given direction.
Definition: Behavior.hpp:24
bl::tmap::Direction facedir
The direction to face.
Definition: Behavior.hpp:39
std::vector< pplmn::OwnedPeoplemon > peoplemon
Definition: Trainer.hpp:78
bool loadDev(std::istream &input)
Loads the trainer from the json stream.
Definition: Trainer.cpp:56
std::uint8_t payout
Definition: Trainer.hpp:80
std::string postBattleConversation
Definition: Trainer.hpp:74
std::string animation
Definition: Trainer.hpp:72
bool saveBundle(bl::serial::binary::OutputStream &output, bl::resource::bundle::FileHandlerContext &ctx) const
Saves the data from this object to the given bundle and registers depency files if any.
Definition: Trainer.cpp:19
bool save(const std::string &file) const
Saves the trainer data to the given file.
Definition: Trainer.cpp:14
std::string name
Definition: Trainer.hpp:71
bool loadProd(bl::serial::binary::InputStream &input)
Loads the trainer from it's binary format.
Definition: Trainer.cpp:52
bool load(const std::string &file, bl::tmap::Direction spawnDir=bl::tmap::Direction::Down)
Loads the trainer data from the given file.
Definition: Trainer.cpp:43
std::string sourceFile
Definition: Trainer.hpp:83
Behavior behavior
Definition: Trainer.hpp:77
Trainer()
Gives sane defaults.
Definition: Trainer.cpp:11
std::string prebattleConversation
Definition: Trainer.hpp:73
static const std::string & ConversationPath()
Definition: Properties.cpp:539
static const std::string & CharacterAnimationPath()
Definition: Properties.cpp:557