Peoplemon  0.1.0
Peoplemon 3 game source documentation
Behavior.hpp
Go to the documentation of this file.
1 #ifndef CORE_FILES_BEHAVIOR_HPP
2 #define CORE_FILES_BEHAVIOR_HPP
3 
4 #include <BLIB/Serialization.hpp>
5 #include <BLIB/Tilemap/Direction.hpp>
6 #include <cstdint>
7 #include <variant>
8 
9 namespace core
10 {
11 namespace file
12 {
19 class Behavior {
20 public:
22  enum Type : std::uint8_t {
25 
28 
31 
33  Wandering = 3
34  };
35 
37  struct Standing {
39  bl::tmap::Direction facedir;
40 
42  Standing(bl::tmap::Direction d);
43 
45  Standing();
46  };
47 
49  struct Spinning {
51  enum Direction : std::uint8_t {
53  Clockwise = 0,
54 
57 
59  Random = 2
61 
63  Spinning(Direction dir);
64 
66  Spinning();
67  };
68 
70  struct Path {
72  struct Pace {
74  bl::tmap::Direction direction;
75 
77  std::uint16_t steps;
78 
85  Pace(bl::tmap::Direction dir, std::uint16_t steps);
86 
91  Pace();
92  };
93 
95  std::vector<Pace> paces;
96 
98  bool reverse;
99 
101  Path();
102  };
103 
105  struct Wander {
107  std::uint32_t radius;
108 
110  Wander(std::uint32_t r);
111 
113  Wander();
114  };
115 
120  Behavior();
121 
129  bool legacyLoad(bl::serial::binary::InputStream& input);
130 
135  Type type() const;
136 
142  void setType(Type type);
143 
148  Standing& standing();
149 
154  Spinning& spinning();
155 
160  Path& path();
161 
166  Wander& wander();
167 
172  const Standing& standing() const;
173 
178  const Spinning& spinning() const;
179 
184  const Path& path() const;
185 
190  const Wander& wander() const;
191 
192 private:
193  Type _type;
194  std::variant<Standing, Spinning, Path, Wander> data;
195 
197  friend struct bl::serial::SerializableObject<Behavior>;
198 };
199 
200 } // namespace file
201 } // namespace core
202 
203 namespace bl
204 {
205 namespace serial
206 {
207 template<>
208 struct SerializableObject<core::file::Behavior::Path::Pace> : public SerializableObjectBase {
210 
211  SerializableField<1, P, bl::tmap::Direction> direction;
212  SerializableField<2, P, std::uint16_t> steps;
213 
215  : SerializableObjectBase("BehaviorPathPace")
216  , direction("direction", *this, &P::direction, SerializableFieldBase::Required{})
217  , steps("steps", *this, &P::steps, SerializableFieldBase::Required{}) {}
218 };
219 
220 template<>
221 struct SerializableObject<core::file::Behavior::Path> : public SerializableObjectBase {
223 
224  SerializableField<1, P, std::vector<P::Pace>> paces;
225  SerializableField<2, P, bool> reverse;
226 
228  : SerializableObjectBase("BehaviorPath")
229  , paces("paces", *this, &P::paces, SerializableFieldBase::Required{})
230  , reverse("reverse", *this, &P::reverse, SerializableFieldBase::Required{}) {}
231 };
232 
233 template<>
234 struct SerializableObject<core::file::Behavior::Standing> : public SerializableObjectBase {
236 
237  SerializableField<1, S, bl::tmap::Direction> direction;
238 
240  : SerializableObjectBase("BehaviorStanding")
241  , direction("direction", *this, &S::facedir, SerializableFieldBase::Required{}) {}
242 };
243 
244 template<>
245 struct SerializableObject<core::file::Behavior::Spinning> : public SerializableObjectBase {
247 
248  SerializableField<1, S, S::Direction> direction;
249 
251  : SerializableObjectBase("BehaviorSpinning")
252  , direction("direction", *this, &S::spinDir, SerializableFieldBase::Required{}) {}
253 };
254 
255 template<>
256 struct SerializableObject<core::file::Behavior::Wander> : public SerializableObjectBase {
258 
259  SerializableField<1, W, std::uint32_t> radius;
260 
262  : SerializableObjectBase("BehaviorWander")
263  , radius("radius", *this, &W::radius, SerializableFieldBase::Required{}) {}
264 };
265 
266 template<>
267 struct SerializableObject<core::file::Behavior> : public SerializableObjectBase {
269 
270  SerializableField<1, B, B::Type> type;
271  SerializableField<2, B, decltype(B::data)> data;
272 
274  : SerializableObjectBase("Behavior")
275  , type("type", *this, &B::_type, SerializableFieldBase::Required{})
276  , data("data", *this, &B::data, SerializableFieldBase::Required{}) {}
277 };
278 } // namespace serial
279 } // namespace bl
280 
281 #endif
Core classes and functionality for both the editor and game.
bl::serial::json::Serializer< Player > Serializer
Definition: Player.cpp:27
Set of behaviors for NPCs and trainers.
Definition: Behavior.hpp:19
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
bl::tmap::Direction facedir
The direction to face.
Definition: Behavior.hpp:39
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
@ Random
The character spins randomly.
Definition: Behavior.hpp:59
@ Counterclockwise
The character spins counterclockwise.
Definition: Behavior.hpp:56
@ 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
Represents a straight section of path.
Definition: Behavior.hpp:72
std::uint16_t steps
The number of steps to take. No need to account for direction change.
Definition: Behavior.hpp:77
Pace()
Makes a single step up.
Definition: Behavior.cpp:143
bl::tmap::Direction direction
The direction to walk in.
Definition: Behavior.hpp:74
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
Wander(std::uint32_t r)
Creates the data.
SerializableField< 1, P, bl::tmap::Direction > direction
Definition: Behavior.hpp:211
SerializableField< 1, P, std::vector< P::Pace > > paces
Definition: Behavior.hpp:224
SerializableField< 1, S, bl::tmap::Direction > direction
Definition: Behavior.hpp:237
SerializableField< 1, S, S::Direction > direction
Definition: Behavior.hpp:248
SerializableField< 1, W, std::uint32_t > radius
Definition: Behavior.hpp:259
SerializableField< 1, B, B::Type > type
Definition: Behavior.hpp:270
SerializableField< 2, B, decltype(B::data)> data
Definition: Behavior.hpp:271