sbepistats 0.1.0

A Minecraft-inspired stat system for Bevy
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! A Minecraft-inspired stat system for Bevy.
//!
//! To get started, add the [`StatsPlugin`] plugin to your app.
//! Then, derive [`StatType`] on a struct and register it to your app with [`AppExt::add_stat_type`].
//! Finally, impl [`StatModifierAdd`] or [`StatModifierMul`] on a component and
//! register it to your app with [`AppExt::add_stat_modifier_add`] or [`AppExt::add_stat_modifier_mul`].
//!
//! To use the stats, add a [`Stat`] component to an entity, and any stat modifiers added to it will be reflected in [`Stat::total`].
//!
//! Stat datatypes are flexible, hence the separation of [`Add`] and [`Mul`]. If you need multiplication for a datatype that either
//! can't multiply or does it in an unwanted way, consider using a wrapper type. At minimum, a stat datatype requires [`Add`].
//!
//! If you're using [`bevy_auto_plugin`](::bevy_auto_plugin), the build hooks [`StatTypeHook`], [`StatModifierAddHook`], and [`StatModifierMulHook`] are available.
//!
//! # Example
//!
//! ```rust
//! # use bevy::prelude::*;
//! # use sbepistats::*;
//! #
//! fn main() {
//!     App::new()
//!         .add_plugins((DefaultPlugins, StatsPlugin))
//!         .add_stat_type::<Speed>()
//!         .add_stat_modifier_add::<Speed, SpeedBoost>()
//!         .add_systems(Startup, |mut commands: Commands| {
//!             commands.spawn((Stat::<Speed>::new(1.0), SpeedBoost));
//!         });
//! }
//!
//! #[derive(StatType)]
//! struct Speed;
//!
//! #[derive(Component)]
//! struct SpeedBoost;
//!
//! impl StatModifierAdd<Speed> for SpeedBoost {
//!     fn add(&self) -> f32 {
//!         0.2
//!     }
//! }
//! ```

use bevy::prelude::*;
#[cfg(feature = "bevy_auto_plugin")]
pub use bevy_auto_plugin::*;

/// Derive macro for [`StatType`].
///
/// Has a `stat_type` attribute that takes a type for the [`StatType::DataType`], which defaults to [`f32`].
///
/// ```rust
/// # use sbepistats::*;
/// #[derive(StatType)]
/// #[stat_type(u32)]
/// struct MyStat;
/// ```
pub use sbepistats_derive::StatType;

#[cfg(feature = "bevy_auto_plugin")]
mod bevy_auto_plugin;

/// Marker trait for defining a unique stat.
///
/// Must be registered with [`AppExt::add_stat_type`].
///
/// ```rust
/// # use sbepistats::*;
/// #[derive(StatType)]
/// #[stat_type(u32)]
/// struct MyStat;
/// ```
pub trait StatType {
    /// What type the stat uses.
    ///
    /// The derive defaults to [`f32`].
    type DataType;
}

/// Stat datatypes that can do addition, eg [`f32`] and [`std::time::Duration`].
pub trait Add {
    /// Addition.
    fn add(self, rhs: Self) -> Self;

    /// Additive identity.
    fn zero() -> Self;
}

impl<T: std::ops::Add<T, Output = T> + num_traits::Zero> Add for T {
    fn add(self, rhs: Self) -> Self {
        self + rhs
    }

    fn zero() -> Self {
        num_traits::Zero::zero()
    }
}

/// Stat datatypes that can do multiplication, eg [`f32`].
pub trait Mul {
    /// Multiplication.
    fn mul(self, rhs: Self) -> Self;

    /// Multiplicative identity.
    fn one() -> Self;
}

impl<T: std::ops::Mul<T, Output = T> + num_traits::One> Mul for T {
    fn mul(self, rhs: Self) -> Self {
        self * rhs
    }

    fn one() -> Self {
        num_traits::One::one()
    }
}

/// Representations of possible stat modifier operations.
///
/// Used in [`StatsSystems`].
#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
pub enum DataTypeOp {
    Add,
    MulBefore,
    MulAfter,
}

/// Component containing the [`StatType`]'s values.
///
/// ```rust
/// # use bevy::prelude::*;
/// # use sbepistats::*;
/// #
/// # #[derive(StatType)]
/// # struct MyStat;
/// #
/// fn my_system(stats: Query<&Stat<MyStat>>) {
///     for stat in stats.iter() {
///         println!("Stat total: {}", stat.total());
///     }
/// }
/// ```
#[derive(Component)]
pub struct Stat<T: StatType> {
    base: T::DataType,
    running_total: T::DataType,
    running_op_total: T::DataType,
}

impl<T: StatType<DataType: Clone + Add>> Stat<T> {
    pub fn new(base: T::DataType) -> Self {
        Stat {
            base: base.clone(),
            running_total: base,
            running_op_total: Add::zero(),
        }
    }
}

impl<T: StatType<DataType: Clone>> Stat<T> {
    fn clear(&mut self) {
        self.running_total = self.base.clone();
    }

    /// The base stat value.
    pub fn base(&self) -> T::DataType {
        self.base.clone()
    }

    /// The stat value after all modifiers.
    ///
    /// Updates in [`PreUpdate`].
    pub fn total(&self) -> T::DataType {
        self.running_total.clone()
    }
}

/// A modifier to a [`Stat`] that adds to it.
///
/// Must be registered with [`AppExt::add_stat_modifier_add`].
///
/// ```rust
/// # use bevy::prelude::*;
/// # use sbepistats::*;
/// #
/// # #[derive(StatType)]
/// # struct MyStat;
/// #
/// #[derive(Component)]
/// struct MyStatModifier;
///
/// impl StatModifierAdd<MyStat> for MyStatModifier {
///     fn add(&self) -> f32 {
///         0.2
///     }
/// }
/// ```
pub trait StatModifierAdd<T: StatType<DataType: Add>> {
    /// Addition to the total, after [`StatModifierMul::mul_before`] but before [`StatModifierMul::mul_after`].
    fn add(&self) -> T::DataType {
        Add::zero()
    }
}

/// A modifier to a [`Stat`] that multiplies to it.
///
/// Must be registered with [`AppExt::add_stat_modifier_mul`].
///
/// ```rust
/// # use bevy::prelude::*;
/// # use sbepistats::*;
/// #
/// # #[derive(StatType)]
/// # struct MyStat;
/// #
/// #[derive(Component)]
/// struct MyStatModifier;
///
/// impl StatModifierMul<MyStat> for MyStatModifier {
///     fn mul_before(&self) -> f32 {
///         0.2
///     }
///     fn mul_after(&self) -> f32 {
///         0.2
///     }
/// }
/// ```
pub trait StatModifierMul<T: StatType<DataType: Add + Mul>> {
    /// Multiplication to the base, before [`StatModifierAdd::add`].
    fn mul_before(&self) -> T::DataType {
        Add::zero()
    }

    /// Multiplication to the total after [`StatModifierAdd::add`] and [`StatModifierMul::mul_before`].
    fn mul_after(&self) -> T::DataType {
        Add::zero()
    }
}

/// System ordering for stat systems.
///
/// Set up in [`StatsPlugin`].
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
pub enum StatsSystems {
    Clear,
    Op(DataTypeOp),
    Apply(DataTypeOp),
}

fn clear_stat<T: StatType<DataType: Clone + Send + Sync + 'static> + Send + Sync + 'static>(
    mut stats: Query<&mut Stat<T>>,
) {
    for mut stat in stats.iter_mut() {
        stat.clear();
    }
}

fn apply_add<T: StatType<DataType: Add + Clone + Send + Sync + 'static> + Send + Sync + 'static>(
    mut stats: Query<&mut Stat<T>>,
) {
    for mut stat in stats.iter_mut() {
        stat.running_total = stat
            .running_total
            .clone()
            .add(stat.running_op_total.clone());
        stat.running_op_total = Add::zero();
    }
}

fn apply_mul_before<
    T: StatType<DataType: Add + Mul + Clone + Send + Sync + 'static> + Send + Sync + 'static,
>(
    mut stats: Query<&mut Stat<T>>,
) {
    for mut stat in stats.iter_mut() {
        stat.running_total = stat
            .running_total
            .clone()
            .mul(T::DataType::one().add(stat.running_op_total.clone()));
        stat.running_op_total = Add::zero();
    }
}

fn apply_mul_after<
    T: StatType<DataType: Add + Mul + Clone + Send + Sync + 'static> + Send + Sync + 'static,
>(
    mut stats: Query<&mut Stat<T>>,
) {
    for mut stat in stats.iter_mut() {
        stat.running_total = stat
            .running_total
            .clone()
            .mul(T::DataType::one().add(stat.running_op_total.clone()));
        stat.running_op_total = Add::zero();
    }
}

/// Extension trait for [`App`] for stat registration methods.
pub trait AppExt {
    /// Register a [`StatType`].
    fn add_stat_type<T: StatType<DataType: Clone + Send + Sync + 'static> + Send + Sync + 'static>(
        &mut self,
    ) -> &mut Self;

    /// Register a [`StatModifierAdd`].
    fn add_stat_modifier_add<
        T: StatType<DataType: Add + Clone + Send + Sync + 'static> + Send + Sync + 'static,
        Modifier: StatModifierAdd<T> + Component,
    >(
        &mut self,
    ) -> &mut Self;

    /// Register a [`StatModifierMul`].
    fn add_stat_modifier_mul<
        T: StatType<DataType: Add + Mul + Clone + Send + Sync + 'static> + Send + Sync + 'static,
        Modifier: StatModifierMul<T> + Component,
    >(
        &mut self,
    ) -> &mut Self;
}

impl AppExt for App {
    fn add_stat_type<
        T: StatType<DataType: Clone + Send + Sync + 'static> + Send + Sync + 'static,
    >(
        &mut self,
    ) -> &mut Self {
        self.add_systems(PreUpdate, clear_stat::<T>.in_set(StatsSystems::Clear));
        self
    }

    fn add_stat_modifier_add<
        T: StatType<DataType: Add + Clone + Send + Sync + 'static> + Send + Sync + 'static,
        Modifier: StatModifierAdd<T> + Component,
    >(
        &mut self,
    ) -> &mut Self {
        self.add_systems(
            PreUpdate,
            (
                (move |mut stats: Query<(&mut Stat<T>, &Modifier)>| {
                    for (mut stat, modifier) in stats.iter_mut() {
                        stat.running_op_total = stat.running_op_total.clone().add(modifier.add());
                    }
                })
                .in_set(StatsSystems::Op(DataTypeOp::Add)),
                apply_add::<T>.in_set(StatsSystems::Apply(DataTypeOp::Add)),
            ),
        );
        self
    }

    fn add_stat_modifier_mul<
        T: StatType<DataType: Add + Mul + Clone + Send + Sync + 'static> + Send + Sync + 'static,
        Modifier: StatModifierMul<T> + Component,
    >(
        &mut self,
    ) -> &mut Self {
        self.add_systems(
            PreUpdate,
            (
                (move |mut stats: Query<(&mut Stat<T>, &Modifier)>| {
                    for (mut stat, modifier) in stats.iter_mut() {
                        stat.running_op_total =
                            stat.running_op_total.clone().add(modifier.mul_before());
                    }
                })
                .in_set(StatsSystems::Op(DataTypeOp::MulBefore)),
                apply_mul_before::<T>.in_set(StatsSystems::Apply(DataTypeOp::MulBefore)),
                (move |mut stats: Query<(&mut Stat<T>, &Modifier)>| {
                    for (mut stat, modifier) in stats.iter_mut() {
                        stat.running_op_total =
                            stat.running_op_total.clone().add(modifier.mul_after());
                    }
                })
                .in_set(StatsSystems::Op(DataTypeOp::MulAfter)),
                apply_mul_after::<T>.in_set(StatsSystems::Apply(DataTypeOp::MulAfter)),
            ),
        );
        self
    }
}

/// Plugin required for stats to work.
///
/// Sets up [`StatsPlugin`].
pub struct StatsPlugin;

impl Plugin for StatsPlugin {
    fn build(&self, app: &mut App) {
        app.configure_sets(
            PreUpdate,
            (
                StatsSystems::Clear,
                StatsSystems::Op(DataTypeOp::MulBefore),
                StatsSystems::Apply(DataTypeOp::MulBefore),
                StatsSystems::Op(DataTypeOp::Add),
                StatsSystems::Apply(DataTypeOp::Add),
                StatsSystems::Op(DataTypeOp::MulAfter),
                StatsSystems::Apply(DataTypeOp::MulAfter),
            )
                .chain(),
        );
    }
}