Skip to main content

Mana

The mana module manages the player's spell resource. It is composed of ManaComponent, which stores the mana pool, and ManaSystem, which handles passive regeneration each frame.


ManaComponent

ManaComponent stores an entity's current, base, and maximum mana via StatValue, plus a regeneration rate. It conforms to StatProvidable.

public class ManaComponent: StatProvidable {
public var value: StatValue
public var regenRate: Float // mana per second

public init(base: Float, max: Float, regenRate: Float = 0)
}
FieldDescription
value.baseStarting mana at initialisation
value.currentLive mana value, modified by spending and regeneration
value.maxMaximum mana ceiling
regenRateMana restored per second. 0 disables passive regeneration.

Example:

// Player with 100 max mana, starts full, regenerates 5 mana/sec
world.addComponent(
component: ManaComponent(base: 100, max: 100, regenRate: 5),
to: player
)

// Entity with mana pool but no passive regen
world.addComponent(
component: ManaComponent(base: 60, max: 60),
to: entity
)

Spending Mana

To consume mana (e.g. when casting a spell), decrement value.current directly. To check whether the player can afford a spell before casting:

if let mana = world.getComponent(type: ManaComponent.self, for: player),
mana.value.current >= spellCost {
// proceed with cast
}

ManaSystem

ManaSystem handles passive mana regeneration. It has no system dependencies and declares no priority, so it runs at the framework's default order.

Each frame, for every entity with a ManaComponent:

  1. Skip if regenRate is 0 or below.
  2. Skip if value.current is already at max (no wasted computation or floating-point drift).
  3. Add regenRate × deltaTime to value.current.
  4. Clamp current to max via clampToMax().
// Effective regen per frame (at 60 fps, deltaTime ≈ 0.0167)
regenAmount = regenRate * Float(deltaTime)

ManaSystem only regenerates — it never spends mana. Spending is the responsibility of the system or logic that consumes it (e.g. a spell-casting system).


Update Loop Summary

Each frame:

  1. ManaSystem — for each entity with ManaComponent and regenRate > 0, tick current upward and clamp to max.
  2. Spell/ability system (your implementation) — checks current, deducts cost, clamps to min.

Configuring Mana for Different Entity Types

Use caseConfiguration
Player with slow regenManaComponent(base: 100, max: 100, regenRate: 3)
Player with fast regenManaComponent(base: 100, max: 100, regenRate: 15)
No passive regen (potion-only)ManaComponent(base: 100, max: 100) — default regenRate: 0
Enemy with a mana poolSame as above; ManaSystem processes any entity with the component

Dependencies

DependencyRole
StatValueUnderlying value type for ManaComponent
ManaComponentRead and modified each frame for regen