Difference between revisions of "Composed Value Registry"

From Beerplop
(Created page with "The composed value registry is a global available registry for numbers which have various components modifying the value (eg. Bottle Caps). A single composed value provides an...")
 
Line 1: Line 1:
The composed value registry is a global available registry for numbers which have various components modifying the value (eg. Bottle Caps). A single composed value provides an API to attach additional multipliers to the value or to trigger a recalculation of the value.
+
The '''composed value registry''' is a global available registry for numbers which have various components modifying the value (eg. Bottle Caps). A single '''composed value''' provides an API to attach additional multipliers to the value or to trigger a recalculation of the value. An example for the usage of composed values is the Beer Blender Bar. The ingredients hook into the composed values (eg. Bottle Caps) and modify their multiplier to change their current influence.
 +
 
 +
Get a composed value:<syntaxhighlight lang="javascript" line="1">
 +
let composedValue = ComposedValueRegistry.getComposedValue(CV_BOTTLE_CAP);
 +
</syntaxhighlight>
 +
{| class="wikitable"
 +
|+Available composed values
 +
!Key
 +
!Description
 +
|-
 +
|''CV_MANUAL_PLOP''
 +
|The manually produced plops
 +
|-
 +
|''CV_BOTTLE_CAP''
 +
|Produced Bottle Caps
 +
|-
 +
|''CV_MANA''
 +
|Produced Mana in Beerwarts
 +
|}
 +
A composed value holds multiple modifiers. Each modifier is identified via a '''modifierKey'''. Each modifier must provide a callback function returning the current influence on the value. By default the returned values are cached inside the composed value.
 +
{| class="wikitable"
 +
|+Composed value API
 +
!Method
 +
!Parameters
 +
!Description
 +
|-
 +
|getValue
 +
| -
 +
|Get the current value including all registered modifiers.
 +
|-
 +
|getValueExcludingModifier
 +
|'''excludeModifier''' (array)
 +
|Get the current value. '''excludeModifier''' contains a list of '''modifierKeys''' which shall not be included in the returned value.
 +
|-
 +
|triggerModifierChange
 +
|'''modifierKey''' (string)
 +
|Notify the composed value about a change for the given '''modifierKey'''. deletes the internal cache and updates the current influence of the modifier on the next call of ''getValue'' or ''getValueExcludingModifier.''
 +
|-
 +
|recalculate
 +
| -
 +
|Force a recalculation of all internal caches
 +
|-
 +
|addModifier
 +
|'''modifierKey''' (string),
 +
'''modifierCallback''' (function),
 +
 
 +
'''cache'''
 +
 
 +
(bool, optional, default true)
 +
|Add an additional modifier to the composed value. The callback function provided will be executed on the next call of ''getValue'' or ''getValueExcludingModifier''. By default the value returned by the callback will be cached. Set '''cache''' to false to fetch the value on each call of getValue via the callback function (for values which change on each tick so the values must not manually trigger a modifier change on each tick)
 +
|-
 +
|removeModifier
 +
|'''modifierKey''' (string)
 +
|Remove the given modifier from the composed value.
 +
|-
 +
|onValueChange
 +
|'''callback'''
 +
(function)
 +
|Execute a callback function if the composed value changes
 +
|-
 +
|debug
 +
| -
 +
|Get a debug log with all modifiers
 +
|}
 +
Example usage:<syntaxhighlight lang="javascript" line="1">
 +
ComposedValueRegistry.getComposedValue(CV_MANA)
 +
  .onValueChange(() => this._updateBeerwartsView())
 +
  .addModifier('Beerwarts_BaseProduction', () => this._getBaseManaProduction())
 +
  .addModifier('Beerwarts_GameSpeed', () => this.gameState.getGameSpeed());
 +
</syntaxhighlight>

Revision as of 11:12, 4 November 2019

The composed value registry is a global available registry for numbers which have various components modifying the value (eg. Bottle Caps). A single composed value provides an API to attach additional multipliers to the value or to trigger a recalculation of the value. An example for the usage of composed values is the Beer Blender Bar. The ingredients hook into the composed values (eg. Bottle Caps) and modify their multiplier to change their current influence.

Get a composed value:

1 let composedValue = ComposedValueRegistry.getComposedValue(CV_BOTTLE_CAP);
Available composed values
Key Description
CV_MANUAL_PLOP The manually produced plops
CV_BOTTLE_CAP Produced Bottle Caps
CV_MANA Produced Mana in Beerwarts

A composed value holds multiple modifiers. Each modifier is identified via a modifierKey. Each modifier must provide a callback function returning the current influence on the value. By default the returned values are cached inside the composed value.

Composed value API
Method Parameters Description
getValue - Get the current value including all registered modifiers.
getValueExcludingModifier excludeModifier (array) Get the current value. excludeModifier contains a list of modifierKeys which shall not be included in the returned value.
triggerModifierChange modifierKey (string) Notify the composed value about a change for the given modifierKey. deletes the internal cache and updates the current influence of the modifier on the next call of getValue or getValueExcludingModifier.
recalculate - Force a recalculation of all internal caches
addModifier modifierKey (string),

modifierCallback (function),

cache

(bool, optional, default true)

Add an additional modifier to the composed value. The callback function provided will be executed on the next call of getValue or getValueExcludingModifier. By default the value returned by the callback will be cached. Set cache to false to fetch the value on each call of getValue via the callback function (for values which change on each tick so the values must not manually trigger a modifier change on each tick)
removeModifier modifierKey (string) Remove the given modifier from the composed value.
onValueChange callback

(function)

Execute a callback function if the composed value changes
debug - Get a debug log with all modifiers

Example usage:

1 ComposedValueRegistry.getComposedValue(CV_MANA)
2   .onValueChange(() => this._updateBeerwartsView())
3   .addModifier('Beerwarts_BaseProduction', () => this._getBaseManaProduction())
4   .addModifier('Beerwarts_GameSpeed', () => this.gameState.getGameSpeed());