2016-10-26 11:19:43 -04:00
|
|
|
var RenderedTarget = require('./rendered-target');
|
2016-06-29 13:48:30 -04:00
|
|
|
var Blocks = require('../engine/blocks');
|
|
|
|
|
2016-07-06 14:09:06 -04:00
|
|
|
/**
|
|
|
|
* Sprite to be used on the Scratch stage.
|
|
|
|
* All clones of a sprite have shared blocks, shared costumes, shared variables.
|
|
|
|
* @param {?Blocks} blocks Shared blocks object for all clones of sprite.
|
2016-09-15 19:37:12 -04:00
|
|
|
* @param {Runtime} runtime Reference to the runtime.
|
2016-07-06 14:09:06 -04:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
var Sprite = function (blocks, runtime) {
|
2016-09-15 19:37:12 -04:00
|
|
|
this.runtime = runtime;
|
2016-06-29 13:48:30 -04:00
|
|
|
if (!blocks) {
|
|
|
|
// Shared set of blocks for all clones.
|
|
|
|
blocks = new Blocks();
|
|
|
|
}
|
|
|
|
this.blocks = blocks;
|
2016-08-31 11:30:09 -04:00
|
|
|
/**
|
|
|
|
* Human-readable name for this sprite (and all clones).
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.name = '';
|
|
|
|
/**
|
|
|
|
* List of costumes for this sprite.
|
|
|
|
* Each entry is an object, e.g.,
|
|
|
|
* {
|
|
|
|
* skin: "costume.svg",
|
|
|
|
* name: "Costume Name",
|
|
|
|
* bitmapResolution: 2,
|
|
|
|
* rotationCenterX: 0,
|
|
|
|
* rotationCenterY: 0
|
|
|
|
* }
|
|
|
|
* @type {Array.<!Object>}
|
|
|
|
*/
|
2016-08-31 11:21:32 -04:00
|
|
|
this.costumes = [];
|
2016-08-31 11:30:09 -04:00
|
|
|
/**
|
|
|
|
* List of clones for this sprite, including the original.
|
|
|
|
* @type {Array.<!Clone>}
|
|
|
|
*/
|
2016-06-29 13:48:30 -04:00
|
|
|
this.clones = [];
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 14:09:06 -04:00
|
|
|
/**
|
|
|
|
* Create a clone of this sprite.
|
|
|
|
* @returns {!Clone} Newly created clone.
|
|
|
|
*/
|
2016-06-29 20:56:55 -04:00
|
|
|
Sprite.prototype.createClone = function () {
|
2016-10-26 11:19:43 -04:00
|
|
|
var newClone = new RenderedTarget(this, this.runtime);
|
2016-10-23 17:55:31 -04:00
|
|
|
newClone.isOriginal = this.clones.length === 0;
|
2016-06-29 20:56:55 -04:00
|
|
|
this.clones.push(newClone);
|
2016-09-15 19:37:12 -04:00
|
|
|
if (newClone.isOriginal) {
|
|
|
|
newClone.initDrawable();
|
|
|
|
}
|
2016-06-29 20:56:55 -04:00
|
|
|
return newClone;
|
|
|
|
};
|
|
|
|
|
2016-06-29 13:48:30 -04:00
|
|
|
module.exports = Sprite;
|