IModifierService
Introduction
The IModifierService provides logic to apply the effects of Modifiers
on Modifiables
. It maintains an IModifierCache
that contains all active Modifiers
, Modifiables
, and ModifierFormulas
. The service can retrieve Modifier
and Modifiable
objects using their instanceId
or the various helper methods in the service.
Adding / Removing Modifiers
and Modifiables
In IdleKit's concrete implementation of ModifierService
, the service subscribes to EntityAddedAction
. The ModifierService
reacts to Entities as they are added and removed by the Entity
Loader Service". Added Entities are registered with the service and added to the ModifierCache
. The service also subscribes to EntityRemovedAction
where removed entities are unregistered and uncached. This results in a cache that only contains currently loaded entities. Internally, the ModifierCache
keeps several organized lookups to provide quick and efficient access.
This is how added Entities are registered in the ModifierCache
:
The service does not introduce any dependencies and there are no dependencies between the IModifier
, IModifiable
Entities, and the service.
IModifierCache
The ModifierCache
is used by the ModifierService
to store and retrieve Modifiers
and Modifiables
. It has various methods that return these Entities based on different parameters. While it is possible to resolve the ModifierCache
itself, it is recommended to use the ModifierService
instead so that the concrete implementation of the ModifierCache
class can be easily altered.
Applying Modifiers
Modifiers
are applied to Modifiables
via the ApplyModifiers<TModifierFormula>()
methods. These methods take a ModifierFormula
type and a reference to a value. The ModifierFormula
then applies the effects for each relevant Modifier
to the value.
In the case where the ModifierService
is queried for information on a Modifier
, Modifiable
or ModifierFormula
the cache does not contain, it does not alter the original value.
Tip
See IdleKit.BaseCollectorEntity.GetModifiedPayout()
for a sample implementation using Modifiers.
IModifierService.ApplyModifiers()
is called, passing aModifiable
(e.g.IGeneratorEntity
) and a value (e.g. the generator's payout) which is to be modified.- The
IModifierService
then retrieves theIModifierFormula
of the specified type (e.g.GeneratorPayoutModifierFormula
). - The
IModifierService
then gets the list ofModifiers
that affect theIModifiable
from theIModifierCache
(e.g.UpgradeableCurrencies
associated with the generator). Apply()
is then called on theIModifierFormula
, which generates the modified amount.
See Modifiers for more information on the Modifier
and Modifiable
Entities.