Skip to main content

Pool

Trait Pool 

Source
pub trait Pool: Sized + Component<Mutability = Mutable> {
    type Quantity: Add<Output = Self::Quantity> + Sub<Output = Self::Quantity> + AddAssign + SubAssign + PartialEq + PartialOrd + Clone + Copy + Send + Sync + 'static;

    const MIN: Self::Quantity;

    // Required methods
    fn current(&self) -> Self::Quantity;
    fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity;
    fn max(&self) -> Self::Quantity;
    fn set_max(
        &mut self,
        new_max: Self::Quantity,
    ) -> Result<(), MaxPoolLessThanMin>;

    // Provided methods
    fn available(&self, amount: Self::Quantity) -> Result<(), CannotUseAbility> { ... }
    fn is_full(&self) -> bool { ... }
    fn is_empty(&self) -> bool { ... }
    fn expend(&mut self, amount: Self::Quantity) -> Result<(), CannotUseAbility> { ... }
    fn replenish(&mut self, amount: Self::Quantity) { ... }
}
Expand description

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, 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.

Required Associated Constants§

Source

const MIN: Self::Quantity

The minimum value of the pool type.

At this point, no resources remain to be spent.

Required Associated Types§

Source

type Quantity: Add<Output = Self::Quantity> + Sub<Output = Self::Quantity> + AddAssign + SubAssign + PartialEq + PartialOrd + Clone + Copy + Send + Sync + 'static

A type that tracks the quantity within a pool.

Unlike a Pool type, which stores a max, min and regeneration, quantities are lighter weight and should be used for things like damage amounts, mana costs and regen rates.

Required Methods§

Source

fn current(&self) -> Self::Quantity

The current quantity of resources in the pool.

§Panics

Panics if max is less than Pool::MIN.

Source

fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity

Sets the current quantity of resources in the pool.

This will be bounded by the minimum and maximum values of this pool. The value that was actually set is returned.

Source

fn max(&self) -> Self::Quantity

The maximum quantity of resources that this pool can store.

Source

fn set_max(&mut self, new_max: Self::Quantity) -> Result<(), MaxPoolLessThanMin>

Sets the maximum quantity of resources that this pool can store.

The current value will be reduced to the new max if necessary.

Has no effect if new_max < Pool::MIN. Returns a MaxPoolLessThanMin error if this occurs.

Provided Methods§

Source

fn available(&self, amount: Self::Quantity) -> Result<(), CannotUseAbility>

Check if the given cost can be paid by this pool.

Source

fn is_full(&self) -> bool

Is the pool currently full?

Source

fn is_empty(&self) -> bool

Is the pool currently empty?

Note that this compares the current value to Pool::MIN, not 0.

Source

fn expend(&mut self, amount: Self::Quantity) -> Result<(), CannotUseAbility>

Spend the specified amount from the pool, if there is that much available.

Otherwise, return the error CannotUseAbility::PoolInsufficient.

Source

fn replenish(&mut self, amount: Self::Quantity)

Replenish the pool by the specified amount.

This cannot cause the pool to exceed maximum value that can be stored in the pool. This is the sign-flipped counterpart to Self::expend, however, unlike Self::expend, this method will not return an error if the pool is empty.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§