-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.cpp
More file actions
115 lines (85 loc) · 2.09 KB
/
stats.cpp
File metadata and controls
115 lines (85 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <sstream>
#include "stats.hpp"
namespace tol {
size_t Health::get() const {
return health;
}
void Health::set(size_t stat) {
health = stat;
}
void Health::increase(size_t value) {
health = std::clamp<size_t>(health + value, 0, 100);
}
void Health::decrease(size_t value) {
health = std::clamp<int>(health - value, 0, 100);
}
Health::Health(size_t health_): health(health_) {}
std::ostream& Health::print(std::ostream& out) const {
out << "Health: " << health << "\n";
return out;
}
Strength::Strength(size_t strength_): strength(strength_) {}
size_t Strength::get() const {
return strength;
}
void Strength::set(size_t stat) {
strength = stat;
}
void Strength::increase(size_t value) {
strength += value;
}
std::ostream& Strength::print(std::ostream& out) const {
out << "Strength: " << strength << "\n";
return out;
}
Speed::Speed(size_t speed_): speed(speed_) {}
size_t Speed::get() const {
return speed;
}
void Speed::set(size_t stat) {
speed = stat;
}
void Speed::increase(size_t value) {
speed += value;
}
std::ostream& Speed::print(std::ostream& out) const {
out << "Speed: " << speed << "\n";
return out;
}
Experience::Experience(size_t xp_): xp(xp_) {}
size_t Experience::get() const {
return xp;
}
void Experience::set(size_t stat) {
xp = stat;
}
void Experience::increase(size_t value) {
xp += value;
}
size_t Experience::level() const {
auto level = 0;
for (const auto& [xp_, level_]: xp_bracket) {
if (xp >= xp_) {
level = level_;
continue;
}
break;
}
return level;
}
std::ostream& Experience::print(std::ostream& out) const {
out << "Experience: " << xp << ", Level: " << level() << "\n";
return out;
}
Stats::Stats(const json& stats):
_health(Health(stats["health"].get<size_t>())), _strength(Strength(stats["strength"].get<size_t>())),
_speed(Speed(stats["speed"].get<size_t>())), _experience(Experience(stats["experience"].get<size_t>())) {}
std::string Stats::get() const {
std::stringstream ss;
ss << _health;
ss << _strength;
ss << _speed;
ss << _experience;
return ss.str();
}
} // namespace tol