Skip to content

Commit

Permalink
Remove f32-related Mul and Div bounds on Pool::Quantity (#61)
Browse files Browse the repository at this point in the history
* Remove f32-related bounds on Pool

* Improve docs for Pool
  • Loading branch information
alice-i-cecile authored Sep 4, 2024
1 parent 12e5e0b commit c119b1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
7 changes: 7 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

## Version 0.9

## Dependencies (0.9)

- now support `leafwing-input-manager` 0.15

## Usability (0.9)

- all types provided by this library are now `Reflect`
- removed `ToggleActions`: this functionality no longer makes sense with changes to how LWIM disables actions. Use run conditions directly on the new `AbilitySystem::TickCooldowns` system set
- the associated type `Pool::Quantity` no longer needs to be able to be multiplied and divided by f32s to ease working with integer-based resource pools
- in exchange, the `RegeneratingPool::regenerate` method no longer has a default implementation

## Version 0.8

Expand Down
18 changes: 10 additions & 8 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use bevy::utils::Duration;
use bevy::{ecs::prelude::*, reflect::Reflect};
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use core::ops::{Add, AddAssign, Sub, SubAssign};
use std::{collections::HashMap, marker::PhantomData};
use thiserror::Error;

Expand All @@ -20,7 +20,14 @@ use crate::{Abilitylike, CannotUseAbility};
/// A reservoir of a resource that can be used to pay for abilities, or keep track of character state.
///
/// Each type that implements this trait should be stored on a component (or, if your actions are globally unique, a resource),
/// and contains information about the current, max and regeneration rates
/// and contains information about the current and max values.
///
/// There are two core benefits to using pools, rather than creating your own solutions:
///
/// 1. It is impossible to accidentally set the value outside of the bounds of the pool.
/// 2. The [`AbilityCosts`] component can be used to automatically check if an ability can be used.
///
/// See [`RegeneratingPool`] for pools that regenerate over time.
pub trait Pool: Sized {
/// A type that tracks the quantity within a pool.
///
Expand All @@ -30,8 +37,6 @@ pub trait Pool: Sized {
+ Sub<Output = Self::Quantity>
+ AddAssign
+ SubAssign
+ Mul<f32, Output = Self::Quantity>
+ Div<f32, Output = Self::Quantity>
+ PartialEq
+ PartialOrd
+ Clone
Expand Down Expand Up @@ -134,10 +139,7 @@ pub trait RegeneratingPool: Pool {
///
/// Called in the [`regenerate_resource_pool`](crate::systems::regenerate_resource_pool) system.
/// Can also be called in your own regeneration systems.
fn regenerate(&mut self, delta_time: Duration) {
let pool_regained = self.regen_per_second() * delta_time.as_secs_f32();
self.replenish(pool_regained)
}
fn regenerate(&mut self, delta_time: Duration);
}

/// The maximum value for a [`Pool`] was set to be less than [`Pool::MIN`].
Expand Down
8 changes: 8 additions & 0 deletions src/premade_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ pub mod life {
fn set_regen_per_second(&mut self, new_regen_per_second: Self::Quantity) {
self.regen_per_second = new_regen_per_second;
}

fn regenerate(&mut self, delta_time: std::time::Duration) {
self.set_current(self.current + self.regen_per_second * delta_time.as_secs_f32());
}
}

impl Add<Life> for LifePool {
Expand Down Expand Up @@ -289,6 +293,10 @@ pub mod mana {
fn set_regen_per_second(&mut self, new_regen_per_second: Self::Quantity) {
self.regen_per_second = new_regen_per_second;
}

fn regenerate(&mut self, delta_time: std::time::Duration) {
self.set_current(self.current + self.regen_per_second * delta_time.as_secs_f32());
}
}

impl Add<Mana> for ManaPool {
Expand Down

0 comments on commit c119b1a

Please sign in to comment.