Peoplemon  0.1.0
Peoplemon 3 game source documentation
NPC.cpp
Go to the documentation of this file.
1 #include <Core/Files/NPC.hpp>
2 
3 #include <Core/Properties.hpp>
4 #include <Core/Resources.hpp>
5 
6 namespace core
7 {
8 namespace file
9 {
10 bool NPC::save(const std::string& file) const {
11  std::ofstream output(file.c_str());
12  return bl::serial::json::Serializer<NPC>::serializeStream(output, *this, 4, 0);
13 }
14 
15 bool NPC::saveBundle(bl::serial::binary::OutputStream& output,
16  bl::resource::bundle::FileHandlerContext& ctx) const {
17  if (!bl::serial::binary::Serializer<NPC>::serialize(output, *this)) return false;
18 
19  const std::array<std::string, 4> Suffixes{"up.anim", "right.anim", "down.anim", "left.anim"};
20  const std::string ap =
21  bl::util::FileUtil::joinPath(Properties::CharacterAnimationPath(), animField);
22  for (const std::string& s : Suffixes) {
23  const std::string p = bl::util::FileUtil::joinPath(ap, s);
24  if (bl::util::FileUtil::exists(p)) { ctx.addDependencyFile(p); }
25  else { BL_LOG_WARN << "NPC " << nameField << " is missing anim: " << p; }
26  }
27 
28  const std::string c =
29  bl::util::FileUtil::joinPath(Properties::ConversationPath(), conversationField);
30  if (bl::util::FileUtil::exists(c)) { ctx.addDependencyFile(c); }
31  else { BL_LOG_WARN << "NPC " << nameField << " is missing conversation " << conversationField; }
32 
33  return true;
34 }
35 
36 bool NPC::load(const std::string& file, bl::tmap::Direction spawnDir) {
37  if (!NpcManager::initializeExisting(file, *this)) return false;
38  if (behavior().type() == Behavior::StandStill) { behavior().standing().facedir = spawnDir; }
39  return true;
40 }
41 
42 bool NPC::loadDev(std::istream& input) {
43  return bl::serial::json::Serializer<NPC>::deserializeStream(input, *this);
44 }
45 
46 bool NPC::loadProd(bl::serial::binary::InputStream& input) {
47  return bl::serial::binary::Serializer<NPC>::deserialize(input, *this);
48 }
49 
50 std::string& NPC::name() { return nameField; }
51 
52 const std::string& NPC::name() const { return nameField; }
53 
54 std::string& NPC::animation() { return animField; }
55 
56 const std::string& NPC::animation() const { return animField; }
57 
58 std::string& NPC::conversation() { return conversationField; }
59 
60 const std::string& NPC::conversation() const { return conversationField; }
61 
62 Behavior& NPC::behavior() { return behaviorField; }
63 
64 Behavior NPC::behavior() const { return behaviorField; }
65 
66 } // namespace file
67 } // namespace core
Core classes and functionality for both the editor and game.
Set of behaviors for NPCs and trainers.
Definition: Behavior.hpp:19
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
bool loadDev(std::istream &input)
Loads the NPC from the json stream.
Definition: NPC.cpp:42
bool save(const std::string &file) const
Saves the NPC data to the given file.
Definition: NPC.cpp:10
std::string & conversation()
The conversation file of the NPC.
Definition: NPC.cpp:58
Behavior & behavior()
The behavior of the NPC.
Definition: NPC.cpp:62
std::string & name()
The name of the NPC.
Definition: NPC.cpp:50
bool load(const std::string &file, bl::tmap::Direction spawnDir=bl::tmap::Direction::Up)
Loads the NPC data from the given file.
Definition: NPC.cpp:36
bool loadProd(bl::serial::binary::InputStream &input)
Loads the NPC from it's binary format.
Definition: NPC.cpp:46
std::string & animation()
The directory of the movement animations.
Definition: NPC.cpp:54
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: NPC.cpp:15
static const std::string & ConversationPath()
Definition: Properties.cpp:539
static const std::string & CharacterAnimationPath()
Definition: Properties.cpp:557