RegenPool

A resource that actions spend and that refills on its own after a pause: stamina, mana, ammo, boost or dash charges. Call update(dt) once per frame.

By default any amount left is enough to start an action, which can take the pool down to zero (souls-style stamina). Set requireFullAmount for mana or ammo, where an action needs all of its cost. A refused spend starts a short "denied" window (isDenied()) for a HUD to flash, and calls onDenied().

Properties

  • max (float)
  • current (float)
  • regenPerSecond (float) — Per second, once regenDelaySeconds have passed since the last spend
  • regenDelaySeconds (float)
  • slowedRegenFactor (float) — Fraction of the regen rate while update() is told the pool is slowed (e.g. holding a block)
  • requireFullAmount (boolean) — When true, spend() refuses unless the whole amount is there. When false, any amount left
  • deniedSeconds (float) — How long isDenied() stays true after a refused spend

Example

CODE
m.stamina = new BGE.RegenPool(100)
m.stamina.regenPerSecond = 50
' In onUpdate(dt):
if dodgePressed and m.stamina.spend(28)
m.startDodge()
end if
m.stamina.update(dt, m.isBlocking) ' regenerates slower while blocking

Constructor

new RegenPool(max?: float): RegenPool

Starts full.

Parameters

  • max (float, optional, default: 100)

Instance Methods

canSpend(amount?: float): boolean

Whether spend(amount) would succeed.

Parameters

  • amount (float, optional, default: 0)

Returns

  • boolean

spend(amount: float): boolean

Takes amount for an action, never below zero. When there isn't enough (see requireFullAmount), spends nothing, starts the denied window and returns false.

Parameters

  • amount (float)

Returns

  • boolean — whether the action may go ahead

drain(amount: float): boolean

Takes amount whether or not there's enough (an overdraw, e.g. a blocked hit), stopping at zero. Never refused.

Parameters

  • amount (float)

Returns

  • boolean — true if it all came out of the pool, false if the pool ran dry

update(dt: float, slowed?: boolean): void

Regenerates, once regenDelaySeconds have passed since the last spend or drain, and counts down the denied window. Call once per frame.

Parameters

  • dt (float) — seconds
  • slowed (boolean, optional, default: false) — regenerate at slowedRegenFactor of the rate

Returns

  • void

refill(): void

Fills the pool and restarts the regen delay.

Returns

  • void

fraction(): float

current / max, 0 to 1.

Returns

  • float

isDenied(): boolean

True for deniedSeconds after a refused spend.

Returns

  • boolean

deniedRemaining(): float

Seconds left of the denied window; it jumps back up on each fresh refusal.

Returns

  • float

onDenied(amount: float): void

protected

Parameters

  • amount (float)

Returns

  • void