phaser/src/loader/MultiFile.js
2018-05-04 16:00:02 +01:00

188 lines
4.8 KiB
JavaScript

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../utils/Class');
/**
* @classdesc
* A MultiFile is a special kind of parent that contains two, or more, Files as children and looks after
* the loading and processing of them all. It is commonly extended and used as a base class for file types such as AtlasJSON or BitmapFont.
*
* You shouldn't create an instance of a MultiFile directly, but should extend it with your own class, setting a custom type and processing methods.
*
* @class MultiFile
* @memberOf Phaser.Loader
* @constructor
* @since 3.7.0
*
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that is going to load this File.
* @param {string} type - The file type string for sorting within the Loader.
* @param {string} key - The key of the file within the loader.
* @param {Phaser.Loader.File[]} files - An array of Files that make-up this MultiFile.
*/
var MultiFile = new Class({
initialize:
function MultiFile (loader, type, key, files)
{
/**
* A reference to the Loader that is going to load this file.
*
* @name Phaser.Loader.MultiFile#loader
* @type {Phaser.Loader.LoaderPlugin}
* @since 3.7.0
*/
this.loader = loader;
/**
* The file type string for sorting within the Loader.
*
* @name Phaser.Loader.MultiFile#type
* @type {string}
* @since 3.7.0
*/
this.type = type;
/**
* Unique cache key (unique within its file type)
*
* @name Phaser.Loader.MultiFile#key
* @type {string}
* @since 3.7.0
*/
this.key = key;
/**
* Array of files that make up this MultiFile.
*
* @name Phaser.Loader.MultiFile#files
* @type {Phaser.Loader.File[]}
* @since 3.7.0
*/
this.files = files;
/**
* The completion status of this MultiFile.
*
* @name Phaser.Loader.MultiFile#complete
* @type {boolean}
* @default false
* @since 3.7.0
*/
this.complete = false;
/**
* The number of files to load.
*
* @name Phaser.Loader.MultiFile#pending
* @type {integer}
* @since 3.7.0
*/
this.pending = files.length;
/**
* The number of files that failed to load.
*
* @name Phaser.Loader.MultiFile#failed
* @type {integer}
* @default 0
* @since 3.7.0
*/
this.failed = 0;
/**
* A storage container for transient data that the loading files need.
*
* @name Phaser.Loader.MultiFile#config
* @type {any}
* @since 3.7.0
*/
this.config = {};
// Link the files
for (var i = 0; i < files.length; i++)
{
files[i].multiFile = this;
}
},
/**
* Checks if this MultiFile is ready to process its children or not.
*
* @method Phaser.Loader.MultiFile#isReadyToProcess
* @since 3.7.0
*
* @return {boolean} `true` if all children of this MultiFile have loaded, otherwise `false`.
*/
isReadyToProcess: function ()
{
return (this.pending === 0 && this.failed === 0 && !this.complete);
},
/**
* Adds another child to this MultiFile, increases the pending count and resets the completion status.
*
* @method Phaser.Loader.MultiFile#addToMultiFile
* @since 3.7.0
*
* @param {Phaser.Loader.File} files - The File to add to this MultiFile.
*
* @return {Phaser.Loader.MultiFile} This MultiFile instance.
*/
addToMultiFile: function (file)
{
this.files.push(file);
file.multiFile = this;
this.pending++;
this.complete = false;
return this;
},
/**
* Called by each File when it finishes loading.
*
* @method Phaser.Loader.MultiFile#onFileComplete
* @since 3.7.0
*
* @param {Phaser.Loader.File} file - The File that has completed processing.
*/
onFileComplete: function (file)
{
var index = this.files.indexOf(file);
if (index !== -1)
{
this.pending--;
}
},
/**
* Called by each File that fails to load.
*
* @method Phaser.Loader.MultiFile#onFileFailed
* @since 3.7.0
*
* @param {Phaser.Loader.File} file - The File that has failed to load.
*/
onFileFailed: function (file)
{
var index = this.files.indexOf(file);
if (index !== -1)
{
this.failed++;
}
}
});
module.exports = MultiFile;