2017-04-17 15:10:04 -04:00
|
|
|
const RenderedTarget = require('./rendered-target');
|
|
|
|
const Blocks = require('../engine/blocks');
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Sprite {
|
2016-08-31 11:30:09 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -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.
|
|
|
|
* @param {Runtime} runtime Reference to the runtime.
|
|
|
|
* @constructor
|
2016-08-31 11:30:09 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
constructor (blocks, runtime) {
|
|
|
|
this.runtime = runtime;
|
|
|
|
if (!blocks) {
|
|
|
|
// Shared set of blocks for all clones.
|
|
|
|
blocks = new Blocks();
|
|
|
|
}
|
|
|
|
this.blocks = blocks;
|
|
|
|
/**
|
|
|
|
* 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.,
|
|
|
|
* {
|
|
|
|
* skinId: 1,
|
|
|
|
* name: "Costume Name",
|
|
|
|
* bitmapResolution: 2,
|
|
|
|
* rotationCenterX: 0,
|
|
|
|
* rotationCenterY: 0
|
|
|
|
* }
|
|
|
|
* @type {Array.<!Object>}
|
|
|
|
*/
|
|
|
|
this.costumes = [];
|
|
|
|
/**
|
|
|
|
* List of sounds for this sprite.
|
|
|
|
*/
|
|
|
|
this.sounds = [];
|
|
|
|
/**
|
|
|
|
* List of clones for this sprite, including the original.
|
|
|
|
* @type {Array.<!RenderedTarget>}
|
|
|
|
*/
|
|
|
|
this.clones = [];
|
|
|
|
}
|
|
|
|
|
2016-08-31 11:30:09 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Create a clone of this sprite.
|
|
|
|
* @returns {!RenderedTarget} Newly created clone.
|
2016-08-31 11:30:09 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
createClone () {
|
|
|
|
const newClone = new RenderedTarget(this, this.runtime);
|
|
|
|
newClone.isOriginal = this.clones.length === 0;
|
|
|
|
this.clones.push(newClone);
|
|
|
|
if (newClone.isOriginal) {
|
|
|
|
newClone.initDrawable();
|
|
|
|
}
|
|
|
|
return newClone;
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
2017-06-07 20:05:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect a clone from this sprite. The clone is unmodified.
|
|
|
|
* In particular, the clone's dispose() method is not called.
|
|
|
|
* @param {!RenderedTarget} clone - the clone to be removed.
|
|
|
|
*/
|
|
|
|
removeClone (clone) {
|
|
|
|
const cloneIndex = this.clones.indexOf(clone);
|
|
|
|
if (cloneIndex >= 0) {
|
|
|
|
this.clones.splice(cloneIndex, 1);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-06-29 20:56:55 -04:00
|
|
|
|
2016-06-29 13:48:30 -04:00
|
|
|
module.exports = Sprite;
|