PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
melee_attack_component.h
1#pragma once
2
3#include "pixelbullet/serialization/node.h"
4
5#include <algorithm>
6
7namespace pixelbullet
8{
9enum class MeleeAttackTimingMode
10{
11 Seconds,
12 AnimationNotify,
13};
14
16{
17 bool enabled = true;
18 float attack_range = 1.25f;
19 float windup_seconds = 0.35f;
20 float cooldown_seconds = 0.85f;
21 int damage_amount = 1;
22 MeleeAttackTimingMode timing_mode = MeleeAttackTimingMode::Seconds;
23 std::string impact_notify_id;
24
25 [[nodiscard]] bool operator==(const MeleeAttackComponent&) const noexcept = default;
26};
27
28inline Node& operator<<(Node& node, const MeleeAttackTimingMode mode)
29{
30 node << (mode == MeleeAttackTimingMode::AnimationNotify ? "AnimationNotify" : "Seconds");
31 return node;
32}
33
34inline const Node& operator>>(const Node& node, MeleeAttackTimingMode& mode)
35{
36 std::string token;
37 node >> token;
38 mode = token == "AnimationNotify" ? MeleeAttackTimingMode::AnimationNotify : MeleeAttackTimingMode::Seconds;
39 return node;
40}
41
42inline Node& operator<<(Node& node, const MeleeAttackComponent& component)
43{
44 node["enabled"] << component.enabled;
45 node["attackRange"] << component.attack_range;
46 node["windupSeconds"] << component.windup_seconds;
47 node["cooldownSeconds"] << component.cooldown_seconds;
48 node["damageAmount"] << component.damage_amount;
49 node["timingMode"] << component.timing_mode;
50 if (component.timing_mode == MeleeAttackTimingMode::AnimationNotify && !component.impact_notify_id.empty())
51 {
52 node["impactNotifyId"] << component.impact_notify_id;
53 }
54 return node;
55}
56
57inline const Node& operator>>(const Node& node, MeleeAttackComponent& component)
58{
59 if (node.HasProperty("enabled"))
60 {
61 node["enabled"] >> component.enabled;
62 }
63 else
64 {
65 component.enabled = true;
66 }
67
68 if (node.HasProperty("attackRange"))
69 {
70 node["attackRange"] >> component.attack_range;
71 }
72 else
73 {
74 component.attack_range = 1.25f;
75 }
76
77 if (node.HasProperty("windupSeconds"))
78 {
79 node["windupSeconds"] >> component.windup_seconds;
80 }
81 else
82 {
83 component.windup_seconds = 0.35f;
84 }
85
86 if (node.HasProperty("cooldownSeconds"))
87 {
88 node["cooldownSeconds"] >> component.cooldown_seconds;
89 }
90 else
91 {
92 component.cooldown_seconds = 0.85f;
93 }
94
95 if (node.HasProperty("damageAmount"))
96 {
97 node["damageAmount"] >> component.damage_amount;
98 }
99 else
100 {
101 component.damage_amount = 1;
102 }
103
104 if (node.HasProperty("timingMode"))
105 {
106 node["timingMode"] >> component.timing_mode;
107 }
108 else
109 {
110 component.timing_mode = MeleeAttackTimingMode::Seconds;
111 }
112
113 if (node.HasProperty("impactNotifyId"))
114 {
115 node["impactNotifyId"] >> component.impact_notify_id;
116 }
117 else
118 {
119 component.impact_notify_id.clear();
120 }
121
122 component.attack_range = std::max(component.attack_range, 0.0001f);
123 component.windup_seconds = std::max(component.windup_seconds, 0.0f);
124 component.cooldown_seconds = std::max(component.cooldown_seconds, 0.0f);
125 component.damage_amount = std::max(component.damage_amount, 1);
126 if (component.timing_mode == MeleeAttackTimingMode::Seconds)
127 {
128 component.impact_notify_id.clear();
129 }
130 return node;
131}
132} // namespace pixelbullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition melee_attack_component.h:16