AssetManager
A class similar to PIXI's Loader.shared that is intended to load resources for an entire Sandy application while providing many utility/helper functions to handle various types of sprites and data for the many resources a screen can have.
JLS.loaders.AssetManager
class AssetManager(directories)
// Implementation example - shorten for brevity
import { Screen, Device } from 'path/to/core'
import { AssetManager, SAM } from 'path/to/loaders'
const CONFIG = require('path/to/config').data
const CenterTouchScreen = new Screen(CONFIG, {
. . .
resources: {
images: {
background: 'RJ45_CenterTouchscreen_background_PROD.png',
},
},
});
const LANDING_GEAR_UNIT = new Device({
. . .
resources: {
images: {
spritesheet: 'RJ45_GEAR_Spritesheet.json',
}
}
});
const PILOT_RMU_UNIT = new Device({
. . .
// let's say this device doesn't have any assets for example
});
AssetManager.shared // or SAM (alias of AssetMananger.shared)
.addObjAssets('screen', CenterTouchScreen, CenterTouchScreen.load)
.addObjAssets('device', LANDING_GEAR_UNIT, LANDING_GEAR_UNIT.load)
.load(() => {
// If this device doesn't have any assets, you can simply call load
// on the device after all assets have been loaded and the Screen have been loaded
PILOT_RMU_UNIT.load()
// do other important stuff here that depends on all assets being loaded
// for example set a main container for display and begin rendering
// Set a screen to be the main container for the "scene."
// This will usually be a JLS.Screen, but can be any PIXI.Container object
displayEngine.set(TestScreen)
// Boot the screen and devices
CenterTouchScreen.boot()
// Begin rendering the display. This is the most important step!!! If this isn't
// done, you won't see anything because it is not continously rendering anything.
GameEngine.register(displayEngine.render)
})
options
The word "options" is used somewhat loosely here as most of the fields are required. Actual optional parameters will have an optional flag.
| Name | Type | Default | Description |
|---|---|---|---|
| directories | object | { images: 'images', sounds: 'sounds' } | optional The path to store each type of asset. |
Members
directories
A reference to the path being used to store each type of asset.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| directories | object | { images: 'images', sounds: 'sounds' } | optional The path to store each type of asset. |
shared static
A premade instance of the loader that can be used to load resources. If one doesn't exist, a new instance is created with default options and returned.
Instead of importing AssetManager.shared, the alias SAM can be used for the shared asset manger.
all
Returns all the assets of the manager.
Methods
This class extends PIXI.Loader so all it's methods are available on the class in addition to the custom ones below.
addObjectAssets(type, obj, callback, options)
Loads the assets on a specified object such as a screen or device.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| type | string | --- | The type of object that assets need to be loaded for. Options: screen, device |
| obj | JLS.Screen or JLS.Device | --- | A reference to the actual object. |
| callback | function | --- | A function to call when the assets have finished loading. |
addAsset(resourceType, resourcePath, assetPrefix)
Loads the specified asset and gives it a keyname to be stored globally and retrieved later.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| resourceType | string | --- | The type or category of the asset. Options: images, sounds |
| resourcePath | string | --- | The path of the resource to load |
| assetPrefix | string | global | The prefix to add as part of the key when retrieving the asset. |
setAsset(assetId, asset)
Saves an asset to the manager's asset collection. Can be used to set assets not created in the loading/parsing process of the AssetManager. Returns the asset if saved successfully. Else will return undefined.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| assetId | string | --- | The asset key to use when retrieving the asset from the shared asset manager. |
| asset | object | --- | The asset to save to the asset loader. Can be any PIXI.Container,.DisplayObject,.Sprite |
setTexture(textureId, textureId)
Saves a new texture to the manager's texture directory
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| textureId | string | --- | The unique id to use for the texture |
| texture | PIXI.Texture | --- | The actual texture to save to the loader |
getTexture(textureId)
Retrieves an asset from the manager's texture directory.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| textureId | string | --- | The unique id of the texture to retrieve |
getAsset(assetId)
Retrieves an asset from the manager's asset collection. If no asset found using the specified assetId, will return undefined.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| assetId | string | --- | The id that was used to set it, now used to retrieve the asset. |
get(assetId)
Alias of getAsset
getClone(assetId)
Creates a new instance (or copy) of a sprite using the saved texture.
parameters
| Name | Type | Default | Description |
|---|---|---|---|
| assetId | string | --- | The id that was used to set it, now used to retrieve the asset. |
init()
"Initializes" the class. Called either from constructor or as public method. In this case, it is called as soon as the class is constructed.