1use serde::{Deserialize, Serialize};
2
3pub type StatValue = u32;
4
5#[derive(
6 Debug,
7 Clone,
8 Copy,
9 PartialEq,
10 Eq,
11 PartialOrd,
12 Ord,
13 Hash,
14 Default,
15 Serialize,
16 Deserialize,
17)]
18pub struct Stat {
19 value: StatValue,
20 max: StatValue,
21}
22
23impl Stat {
24 pub const fn new(value: StatValue, max: StatValue) -> Self {
25 let mut this = Self { value, max };
26 this.normalize();
27 this
28 }
29
30 pub const fn value(&self) -> StatValue {
31 self.value
32 }
33
34 pub fn set_value(&mut self, value: StatValue) {
35 self.value = value;
36 self.normalize();
37 }
38
39 pub fn increase_value(&mut self, amount: StatValue) {
40 self.set_value(self.value().saturating_add(amount));
41 }
42
43 pub fn decrease_value(&mut self, amount: StatValue) {
44 self.set_value(self.value().saturating_sub(amount));
45 }
46
47 pub const fn curr_max(&self) -> StatValue {
48 self.max
49 }
50
51 pub fn set_max(&mut self, max: StatValue) {
52 self.max = max;
53 self.normalize();
54 }
55
56 pub fn increase_max(&mut self, amount: StatValue) {
57 self.set_max(self.curr_max().saturating_add(amount));
58 }
59
60 pub fn decrease_max(&mut self, amount: StatValue) {
61 self.set_max(self.curr_max().saturating_sub(amount));
62 }
63
64 const fn normalize(&mut self) {
65 if self.value > self.max {
66 self.value = self.max;
67 }
68 }
69}