Extending with Perks
Overview
The core component of BitsmistJS “Unit” has few functions. Each unit can be extended in functionality by applying perks.
For example, in "Create a Sample Unit", an event handler is set in the configuration, which is handled by EventPerk. Loading and applying HTML files is done by SkinPerk.
Perk has the following functions.
- Add event handlers to a unit to perform processing at specific event.
- Add skills, spells, and inventories to a unit.
By default, the following perks are applied to a unit.
Applying Perks
To apply a perk, it must be written in the settings of each unit. There are two ways to do so.
Write in the "perk" Section
Specify the name of the perks in apply option of Perk. Multiple perks can be specified.
_getSettings() { return { "perk": { "options": { "apply": ["EventPerk"] } } }; }
Add Perk's Dedicated Section
Each perk has a dedicated section for the perk, and the presence of that section in the settings will automatically apply the perk.
For example, the following settings has an “event” section, so the EventPerk will be applied automatically.
_getSettings() { return { "event": { "events": { "this": { "handlers": { "doSetup": this.onDoSetup } }, } } }; }
To see which perks are associated with which section, see the “section” tag under the title of each perk reference.
Event Handlers
Some perks register event handlers to the units and perform their own processing within those handlers. The event handlers registered by the perks have a higher priority and are designed to be processed before user event handlers.
Which events are handled and how they are handled varies from perk to perk. For example, SkinPerk loads the necessary HTML file in “beforeTransform” and applies it to the unit in “doTransform” event.
Below is a list of what the perk in the core library does at which events. For more information, please refer to each link.
Event | Perk | Description |
---|---|---|
doApplySettings | EventPerk | Install event handlers. |
StatuePerk | Install event handlers to wait for other units. | |
UnitPerk | Add child units. | |
beforeTransform | SkinPerk | Load HTML file. |
StylePerk | Load and Apply common CSS files. | |
doTransform | SkinPerk | Apply HTML file. |
StylePerk | Load and Apply unit-specifi CSS files. | |
afterTransform | EventPerk | Install event handlers for child elements. |
Extended Features
Each perk also has the ability to add assets, inventory, skills, and spells to the unit prototype and/or each instance.
Below is a list of the functions added by the perks in the core library. Please see the description of each perk for more details.
Perk | Type | Name |
---|---|---|
BasicPerk | Method | cast |
use | ||
Asset | inventory | |
perk | ||
skill | ||
spell | ||
Inventory | basic.unitRoot | |
Skill | basic.locate | |
basic.locateAll | ||
basic.scan | ||
basic.scanAll | ||
Spell | basic.clear | |
basic.fetch | ||
basic.fill | ||
basic.refresh | ||
basic.setup | ||
basic.start | ||
basic.stop | ||
basic.transform | ||
EventPerk | Skill | event.add |
event.init | ||
event.remove | ||
event.reset | ||
event.triggerSync | ||
Spell | event.trigger | |
Perk | Spell | perk.attach |
perk.attachPerks | ||
SettingPerk | Asset | setting |
Skill | setting.get | |
setting.merge | ||
setting.set | ||
Spell | setting.apply | |
setting.summon | ||
SkinPerk | Inventory | skin.skins |
skin.active | ||
Skill | skin.apply | |
Spell | skin.summon | |
StatusPerk | Inventory | status.status |
Skill | status.change | |
Spell | status.wait | |
StylePerk | Inventory | style.styles |
Spell | style.apply | |
style.summon | ||
UnitPerk | Inventory | unit.units |
Spell | unit.materialize | |
unit.materializeAll | ||
unit.summon |