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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Convenient premade resource [`Pool`] types to get you started.
//!
//! These can be annoying due to orphan rules that prevent you from implementing your own methods,
//! so feel free to copy-paste them (without attribution) into your own source to make new variants.

use crate::pool::{MaxPoolLessThanMin, Pool};
use bevy::prelude::{Component, Resource};
use core::fmt::{Display, Formatter};
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use derive_more::{Add, AddAssign, Sub, SubAssign};

/// A premade resource pool for life (aka health, hit points or HP).
pub mod life {
    use crate::pool::RegeneratingPool;

    use super::*;

    /// The amount of life available to a unit.
    /// If they lose it all, they die or pass out.
    ///
    /// This is intended to be stored as a component on each entity.
    #[derive(Debug, Clone, PartialEq, Component, Resource)]
    pub struct LifePool {
        /// The current life.
        current: Life,
        /// The maximum life that can be stored.
        max: Life,
        /// The amount of life regenerated per second.
        pub regen_per_second: Life,
    }

    impl LifePool {
        /// Creates a new [`LifePool`] with the supplied settings.
        ///
        /// # Panics
        /// Panics if `current` is greater than `max`.
        /// Panics if `current` or max is negative.

        pub fn new(current: Life, max: Life, regen_per_second: Life) -> Self {
            assert!(current <= max);
            assert!(current >= LifePool::MIN);
            assert!(max >= LifePool::MIN);
            Self {
                current,
                max,
                regen_per_second,
            }
        }
    }

    /// A quantity of life, used to modify a [`LifePool`].
    ///
    /// This can be used for damage computations, life regeneration, healing and so on.
    #[derive(
        Debug, Clone, Copy, PartialEq, PartialOrd, Default, Add, Sub, AddAssign, SubAssign,
    )]
    pub struct Life(pub f32);

    impl Mul<f32> for Life {
        type Output = Life;

        fn mul(self, rhs: f32) -> Life {
            Life(self.0 * rhs)
        }
    }

    impl Mul<Life> for f32 {
        type Output = Life;

        fn mul(self, rhs: Life) -> Life {
            Life(self * rhs.0)
        }
    }

    impl Div<f32> for Life {
        type Output = Life;

        fn div(self, rhs: f32) -> Life {
            Life(self.0 / rhs)
        }
    }

    impl Div<Life> for Life {
        type Output = f32;

        fn div(self, rhs: Life) -> f32 {
            self.0 / rhs.0
        }
    }

    impl Pool for LifePool {
        type Quantity = Life;
        const MIN: Life = Life(0.);

        fn current(&self) -> Self::Quantity {
            self.current
        }

        fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity {
            let actual_value = Life(new_quantity.0.clamp(0., self.max.0));
            self.current = actual_value;
            self.current
        }

        fn max(&self) -> Self::Quantity {
            self.max
        }

        fn set_max(&mut self, new_max: Self::Quantity) -> Result<(), MaxPoolLessThanMin> {
            if new_max < Self::MIN {
                Err(MaxPoolLessThanMin)
            } else {
                self.max = new_max;
                self.set_current(self.current);
                Ok(())
            }
        }
    }

    impl RegeneratingPool for LifePool {
        fn regen_per_second(&self) -> Self::Quantity {
            self.regen_per_second
        }

        fn set_regen_per_second(&mut self, new_regen_per_second: Self::Quantity) {
            self.regen_per_second = new_regen_per_second;
        }
    }

    impl Add<Life> for LifePool {
        type Output = Self;

        fn add(mut self, rhs: Life) -> Self::Output {
            self.set_current(self.current + rhs);
            self
        }
    }

    impl Sub<Life> for LifePool {
        type Output = Self;

        fn sub(mut self, rhs: Life) -> Self::Output {
            self.set_current(self.current - rhs);
            self
        }
    }

    impl AddAssign<Life> for LifePool {
        fn add_assign(&mut self, rhs: Life) {
            self.set_current(self.current + rhs);
        }
    }

    impl SubAssign<Life> for LifePool {
        fn sub_assign(&mut self, rhs: Life) {
            self.set_current(self.current - rhs);
        }
    }

    impl Display for Life {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl Display for LifePool {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "{}/{}", self.current, self.max)
        }
    }
}

/// A premade resource pool for mana (aka MP).
pub mod mana {
    use crate::pool::RegeneratingPool;

    use super::*;

    /// The amount of mana available to a unit.
    /// Units must spend mana to cast spells according to their [`AbilityCosts<A, Mana>`](crate::pool::AbilityCosts) component.
    ///
    /// This is intended to be stored as a component on each entity.
    #[derive(Debug, Clone, PartialEq, Component, Resource)]
    pub struct ManaPool {
        /// The current mana.
        current: Mana,
        /// The maximum mana that can be stored.
        max: Mana,
        /// The amount of mana regenerated per second.
        pub regen_per_second: Mana,
    }

    impl ManaPool {
        /// Creates a new [`ManaPool`] with the supplied settings.
        ///
        /// # Panics
        /// Panics if `current` is greater than `max`.
        /// Panics if `current` or `max` is negative.
        pub fn new(current: Mana, max: Mana, regen_per_second: Mana) -> Self {
            assert!(current <= max);
            assert!(current >= ManaPool::MIN);
            assert!(max >= ManaPool::MIN);
            Self {
                current,
                max,
                regen_per_second,
            }
        }
    }

    /// A quantity of mana, used to modify a [`ManaPool`].
    ///
    /// This can be used for ability costs, mana regeneration and so on.
    #[derive(
        Debug, Clone, Copy, PartialEq, PartialOrd, Default, Add, Sub, AddAssign, SubAssign,
    )]
    pub struct Mana(pub f32);

    impl Mul<f32> for Mana {
        type Output = Mana;

        fn mul(self, rhs: f32) -> Mana {
            Mana(self.0 * rhs)
        }
    }

    impl Mul<Mana> for f32 {
        type Output = Mana;

        fn mul(self, rhs: Mana) -> Mana {
            Mana(self * rhs.0)
        }
    }

    impl Div<f32> for Mana {
        type Output = Mana;

        fn div(self, rhs: f32) -> Mana {
            Mana(self.0 / rhs)
        }
    }

    impl Div<Mana> for Mana {
        type Output = f32;

        fn div(self, rhs: Mana) -> f32 {
            self.0 / rhs.0
        }
    }

    impl Pool for ManaPool {
        type Quantity = Mana;
        const MIN: Mana = Mana(0.);

        fn current(&self) -> Self::Quantity {
            self.current
        }

        fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity {
            let actual_value = Mana(new_quantity.0.clamp(0., self.max.0));
            self.current = actual_value;
            self.current
        }

        fn max(&self) -> Self::Quantity {
            self.max
        }

        fn set_max(&mut self, new_max: Self::Quantity) -> Result<(), MaxPoolLessThanMin> {
            if new_max < Self::MIN {
                Err(MaxPoolLessThanMin)
            } else {
                self.max = new_max;
                self.set_current(self.current);
                Ok(())
            }
        }
    }

    impl RegeneratingPool for ManaPool {
        fn regen_per_second(&self) -> Self::Quantity {
            self.regen_per_second
        }

        fn set_regen_per_second(&mut self, new_regen_per_second: Self::Quantity) {
            self.regen_per_second = new_regen_per_second;
        }
    }

    impl Add<Mana> for ManaPool {
        type Output = Self;

        fn add(mut self, rhs: Mana) -> Self::Output {
            self.set_current(self.current + rhs);
            self
        }
    }

    impl Sub<Mana> for ManaPool {
        type Output = Self;

        fn sub(mut self, rhs: Mana) -> Self::Output {
            self.set_current(self.current - rhs);
            self
        }
    }

    impl AddAssign<Mana> for ManaPool {
        fn add_assign(&mut self, rhs: Mana) {
            self.set_current(self.current + rhs);
        }
    }

    impl SubAssign<Mana> for ManaPool {
        fn sub_assign(&mut self, rhs: Mana) {
            self.set_current(self.current - rhs);
        }
    }

    impl Display for Mana {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl Display for ManaPool {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "{}/{}", self.current, self.max)
        }
    }
}