Peoplemon  0.1.0
Peoplemon 3 game source documentation
Behavior.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Logging.hpp>
4 
5 namespace core
6 {
7 namespace file
8 {
10 : _type(Type::StandStill)
11 , data(Standing(bl::tmap::Direction::Down)) {}
12 
13 bool Behavior::legacyLoad(bl::serial::binary::InputStream& input) {
14  std::uint8_t behaviorType;
15  if (!input.read(behaviorType)) return false;
16  switch (behaviorType) {
17  case 0:
19  break;
20  case 1:
22  break;
23  case 2:
25  break;
26  case 3:
28  break;
29  default:
30  BL_LOG_ERROR << "Unrecognized behavior: " << behaviorType;
31  return false;
32  }
33 
34  switch (_type) {
35  case StandStill:
36  return true;
37 
38  case SpinInPlace: {
39  std::uint8_t spinDir;
40  if (!input.read<std::uint8_t>(spinDir)) return false;
41  spinning().spinDir = static_cast<Spinning::Direction>(spinDir);
42  }
43  return true;
44 
45  case FollowingPath: {
46  std::uint8_t paceCount;
47  std::uint8_t backwards;
48  if (!input.read<std::uint8_t>(paceCount)) return false;
49  if (!input.read<std::uint8_t>(backwards)) return false;
50  path().reverse = backwards == 1;
51 
52  path().paces.reserve(paceCount);
53  for (std::uint8_t i = 0; i < paceCount; ++i) {
54  std::uint8_t dir;
55  std::uint8_t steps;
56  if (!input.read<std::uint8_t>(dir)) return false;
57  if (!input.read<std::uint8_t>(steps)) return false;
58  path().paces.emplace_back(static_cast<bl::tmap::Direction>(dir), steps);
59  }
60  }
61  return true;
62 
63  case Wandering: {
64  std::uint8_t radius;
65  if (!input.read<std::uint8_t>(radius)) return false;
66  wander().radius = radius;
67  }
68  return true;
69 
70  default:
71  return false;
72  }
73 }
74 
75 Behavior::Type Behavior::type() const { return _type; }
76 
78  _type = t;
79  switch (t) {
80  case StandStill:
81  data.emplace<Standing>(bl::tmap::Direction::Down);
82  break;
83 
84  case SpinInPlace:
85  data.emplace<Spinning>(Spinning::Clockwise);
86  break;
87 
88  case FollowingPath:
89  data.emplace<Path>();
90  break;
91 
92  case Wandering:
93  data.emplace<Wander>(10);
94  break;
95 
96  default:
97  BL_LOG_ERROR << "Unknown behavior type: " << t;
98  break;
99  }
100 }
101 
102 Behavior::Standing& Behavior::standing() { return std::get<Standing>(data); }
103 
104 const Behavior::Standing& Behavior::standing() const { return std::get<Standing>(data); }
105 
106 Behavior::Spinning& Behavior::spinning() { return std::get<Spinning>(data); }
107 
108 const Behavior::Spinning& Behavior::spinning() const { return std::get<Spinning>(data); }
109 
110 Behavior::Path& Behavior::path() { return std::get<Path>(data); }
111 
112 const Behavior::Path& Behavior::path() const { return std::get<Path>(data); }
113 
114 Behavior::Wander& Behavior::wander() { return std::get<Wander>(data); }
115 
116 const Behavior::Wander& Behavior::wander() const { return std::get<Wander>(data); }
117 
119 : facedir(bl::tmap::Direction::Down) {}
120 
121 Behavior::Standing::Standing(bl::tmap::Direction d)
122 : facedir(d) {}
123 
125 : spinDir(Direction::Random) {}
126 
128 : spinDir(d) {}
129 
131 : reverse(true) {}
132 
134 : radius(10) {}
135 
136 Behavior::Wander::Wander(unsigned int r)
137 : radius(r) {}
138 
139 Behavior::Path::Pace::Pace(bl::tmap::Direction dir, std::uint16_t s)
140 : direction(dir)
141 , steps(s) {}
142 
144 : direction(bl::tmap::Direction::Up)
145 , steps(1) {}
146 
147 } // namespace file
148 } // namespace core
Core classes and functionality for both the editor and game.
bool legacyLoad(bl::serial::binary::InputStream &input)
Loads the behavior data from the legacy formatted file. Use serialize and deserialize for reading and...
Definition: Behavior.cpp:13
Spinning & spinning()
The data for spinning.
Definition: Behavior.cpp:106
Behavior()
Empty behavior. Standing still facing down.
Definition: Behavior.cpp:9
Type type() const
The type of behavior this is.
Definition: Behavior.cpp:75
void setType(Type type)
Set the Type of behavior. Existing behavior data is cleared.
Definition: Behavior.cpp:77
Wander & wander()
The data for wandering.
Definition: Behavior.cpp:114
Standing & standing()
The data for standing still.
Definition: Behavior.cpp:102
Type
The type of behavior.
Definition: Behavior.hpp:22
@ Wandering
The character is allowed to wander freely.
Definition: Behavior.hpp:33
@ FollowingPath
The character will follow a preset path.
Definition: Behavior.hpp:30
@ StandStill
The character will stand and face a given direction.
Definition: Behavior.hpp:24
@ SpinInPlace
The character will spin in place.
Definition: Behavior.hpp:27
Path & path()
The data for path following.
Definition: Behavior.cpp:110
Contains data if the behavior type is StandStill.
Definition: Behavior.hpp:37
Standing()
Stand facing down.
Definition: Behavior.cpp:118
Contains data for when the behavior type is SpinInPlace.
Definition: Behavior.hpp:49
enum core::file::Behavior::Spinning::Direction spinDir
Direction
The direction to spin.
Definition: Behavior.hpp:51
@ Clockwise
The character spins clockwise.
Definition: Behavior.hpp:53
Contains data for when the behavior type is following a path.
Definition: Behavior.hpp:70
std::vector< Pace > paces
The sections of the path.
Definition: Behavior.hpp:95
bool reverse
True if the path should be done in reverse when completed, false to loop.
Definition: Behavior.hpp:98
Path()
Makes an empty path.
Definition: Behavior.cpp:130
Pace()
Makes a single step up.
Definition: Behavior.cpp:143
Contains data for when the behavior type is Wandering.
Definition: Behavior.hpp:105
std::uint32_t radius
The radius to stay within, in tiles.
Definition: Behavior.hpp:107
Wander()
Wander in a 10 tile radius.
Definition: Behavior.cpp:133