PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
patrol_agent_component.h
1#pragma once
2
3#include "pixelbullet/scene/entity_id.h"
4#include "pixelbullet/serialization/node.h"
5
6#include <algorithm>
7#include <string>
8#include <string_view>
9
10namespace pixelbullet
11{
12enum class PatrolOrientationMode
13{
14 FaceMovement,
15 KeepRotation,
16};
17
18[[nodiscard]] constexpr std::string_view ToString(const PatrolOrientationMode value) noexcept
19{
20 switch (value)
21 {
22 case PatrolOrientationMode::KeepRotation:
23 return "KeepRotation";
24 case PatrolOrientationMode::FaceMovement:
25 default:
26 return "FaceMovement";
27 }
28}
29
30inline Node& operator<<(Node& node, const PatrolOrientationMode& value)
31{
32 node << std::string(ToString(value));
33 return node;
34}
35
36inline const Node& operator>>(const Node& node, PatrolOrientationMode& value)
37{
38 std::string serialized;
39 node >> serialized;
40 value = serialized == "KeepRotation" ? PatrolOrientationMode::KeepRotation : PatrolOrientationMode::FaceMovement;
41 return node;
42}
43
45{
46 bool enabled = true;
47 EntityId route_entity = EntityId::Invalid();
48 float move_speed = 1.5f;
49 float arrive_distance = 0.1f;
50 PatrolOrientationMode orientation_mode = PatrolOrientationMode::FaceMovement;
51
52 [[nodiscard]] bool operator==(const PatrolAgentComponent&) const noexcept = default;
53};
54
55inline Node& operator<<(Node& node, const PatrolAgentComponent& component)
56{
57 node["enabled"] << component.enabled;
58 if (component.route_entity)
59 {
60 node["routeEntity"] << component.route_entity.Raw();
61 }
62 node["moveSpeed"] << component.move_speed;
63 node["arriveDistance"] << component.arrive_distance;
64 node["orientationMode"] << component.orientation_mode;
65 return node;
66}
67
68inline const Node& operator>>(const Node& node, PatrolAgentComponent& component)
69{
70 if (node.HasProperty("enabled"))
71 {
72 node["enabled"] >> component.enabled;
73 }
74 else
75 {
76 component.enabled = true;
77 }
78
79 if (node.HasProperty("routeEntity"))
80 {
81 EntityId::ValueType raw_value = 0u;
82 node["routeEntity"] >> raw_value;
83 component.route_entity = EntityId::FromRaw(raw_value);
84 }
85 else
86 {
87 component.route_entity = EntityId::Invalid();
88 }
89
90 if (node.HasProperty("moveSpeed"))
91 {
92 node["moveSpeed"] >> component.move_speed;
93 }
94 else
95 {
96 component.move_speed = 1.5f;
97 }
98
99 if (node.HasProperty("arriveDistance"))
100 {
101 node["arriveDistance"] >> component.arrive_distance;
102 }
103 else
104 {
105 component.arrive_distance = 0.1f;
106 }
107
108 if (node.HasProperty("orientationMode"))
109 {
110 node["orientationMode"] >> component.orientation_mode;
111 }
112 else
113 {
114 component.orientation_mode = PatrolOrientationMode::FaceMovement;
115 }
116
117 component.move_speed = std::max(component.move_speed, 0.0f);
118 component.arrive_distance = std::max(component.arrive_distance, 0.0001f);
119 return node;
120}
121} // namespace pixelbullet
Definition entity_id.h:11
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition patrol_agent_component.h:45