PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
pawn_vitality_component.h
1#pragma once
2
3#include "pixelbullet/serialization/node.h"
4
5#include <algorithm>
6
7namespace pixelbullet
8{
10{
11 bool enabled = true;
12 int max_health = 3;
13 int respawn_health = 3;
14 float post_respawn_invulnerability_seconds = 1.0f;
15
16 [[nodiscard]] bool operator==(const PawnVitalityComponent&) const noexcept = default;
17};
18
19inline Node& operator<<(Node& node, const PawnVitalityComponent& component)
20{
21 node["enabled"] << component.enabled;
22 node["maxHealth"] << component.max_health;
23 node["respawnHealth"] << component.respawn_health;
24 node["postRespawnInvulnerabilitySeconds"] << component.post_respawn_invulnerability_seconds;
25 return node;
26}
27
28inline const Node& operator>>(const Node& node, PawnVitalityComponent& component)
29{
30 if (node.HasProperty("enabled"))
31 {
32 node["enabled"] >> component.enabled;
33 }
34 else
35 {
36 component.enabled = true;
37 }
38
39 if (node.HasProperty("maxHealth"))
40 {
41 node["maxHealth"] >> component.max_health;
42 }
43 else
44 {
45 component.max_health = 3;
46 }
47
48 if (node.HasProperty("respawnHealth"))
49 {
50 node["respawnHealth"] >> component.respawn_health;
51 }
52 else
53 {
54 component.respawn_health = component.max_health;
55 }
56
57 if (node.HasProperty("postRespawnInvulnerabilitySeconds"))
58 {
59 node["postRespawnInvulnerabilitySeconds"] >> component.post_respawn_invulnerability_seconds;
60 }
61 else
62 {
63 component.post_respawn_invulnerability_seconds = 1.0f;
64 }
65
66 component.max_health = std::max(component.max_health, 1);
67 component.respawn_health = std::clamp(component.respawn_health, 1, component.max_health);
68 component.post_respawn_invulnerability_seconds = std::max(component.post_respawn_invulnerability_seconds, 0.0f);
69 return node;
70}
71} // namespace pixelbullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition pawn_vitality_component.h:10