2016-06-29 13:48:30 -04:00
|
|
|
var util = require('util');
|
2016-06-29 23:07:34 -04:00
|
|
|
var MathUtil = require('../util/math-util');
|
2016-06-29 13:48:30 -04:00
|
|
|
var Target = require('../engine/target');
|
|
|
|
|
2016-07-06 14:09:06 -04:00
|
|
|
/**
|
2016-07-13 16:52:46 -04:00
|
|
|
* Clone (instance) of a sprite.
|
2016-07-06 14:09:06 -04:00
|
|
|
* @param {!Blocks} spriteBlocks Reference to the sprite's blocks.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-06-29 13:48:30 -04:00
|
|
|
function Clone(spriteBlocks) {
|
|
|
|
Target.call(this, spriteBlocks);
|
2016-06-29 20:56:55 -04:00
|
|
|
this.drawableID = null;
|
|
|
|
this.initDrawable();
|
2016-06-29 13:48:30 -04:00
|
|
|
}
|
|
|
|
util.inherits(Clone, Target);
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Create a clone's drawable with the renderer.
|
|
|
|
*/
|
2016-06-29 20:56:55 -04:00
|
|
|
Clone.prototype.initDrawable = function () {
|
|
|
|
var createPromise = self.renderer.createDrawable();
|
|
|
|
var instance = this;
|
|
|
|
createPromise.then(function (id) {
|
|
|
|
instance.drawableID = id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
// Clone-level properties.
|
|
|
|
/**
|
|
|
|
* Scratch X coordinate. Currently should range from -240 to 240.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
2016-06-29 13:48:30 -04:00
|
|
|
Clone.prototype.x = 0;
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Scratch Y coordinate. Currently should range from -180 to 180.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
2016-06-29 13:48:30 -04:00
|
|
|
Clone.prototype.y = 0;
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Scratch direction. Currently should range from -179 to 180.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
2016-06-29 13:48:30 -04:00
|
|
|
Clone.prototype.direction = 90;
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Whether the clone is currently visible.
|
|
|
|
* @type {!boolean}
|
|
|
|
*/
|
2016-07-01 12:56:59 -04:00
|
|
|
Clone.prototype.visible = true;
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Size of clone as a percent of costume size. Ranges from 5% to 535%.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
2016-06-30 00:11:47 -04:00
|
|
|
Clone.prototype.size = 100;
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Map of current graphic effect values.
|
|
|
|
* @type {!Object.<string, number>}
|
|
|
|
*/
|
2016-06-30 00:11:47 -04:00
|
|
|
Clone.prototype.effects = {
|
|
|
|
'color': 0,
|
|
|
|
'fisheye': 0,
|
|
|
|
'whirl': 0,
|
|
|
|
'pixelate': 0,
|
|
|
|
'mosaic': 0,
|
|
|
|
'brightness': 0,
|
|
|
|
'ghost': 0
|
|
|
|
};
|
2016-07-06 13:57:58 -04:00
|
|
|
// End clone-level properties.
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Set the X and Y coordinates of a clone.
|
|
|
|
* @param {!number} x New X coordinate of clone, in Scratch coordinates.
|
|
|
|
* @param {!number} y New Y coordinate of clone, in Scratch coordinates.
|
|
|
|
*/
|
2016-06-29 20:56:55 -04:00
|
|
|
Clone.prototype.setXY = function (x, y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
position: [this.x, this.y]
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Set the direction of a clone.
|
|
|
|
* @param {!number} direction New direction of clone.
|
|
|
|
*/
|
2016-06-29 20:56:55 -04:00
|
|
|
Clone.prototype.setDirection = function (direction) {
|
2016-07-06 13:57:58 -04:00
|
|
|
// Keep direction between -179 and +180.
|
2016-06-29 23:07:34 -04:00
|
|
|
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
2016-06-29 20:56:55 -04:00
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
direction: this.direction
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:47:32 -04:00
|
|
|
/**
|
|
|
|
* Set a say bubble on this clone.
|
|
|
|
* @param {?string} type Type of say bubble: "say", "think", or null.
|
|
|
|
* @param {?string} message Message to put in say bubble.
|
|
|
|
*/
|
|
|
|
Clone.prototype.setSay = function (type, message) {
|
|
|
|
// @todo: Render to stage.
|
|
|
|
if (!type || !message) {
|
|
|
|
console.log('Clearing say bubble');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Setting say bubble:', type, message);
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Set visibility of the clone; i.e., whether it's shown or hidden.
|
|
|
|
* @param {!boolean} visible True if the sprite should be shown.
|
|
|
|
*/
|
2016-07-01 12:56:59 -04:00
|
|
|
Clone.prototype.setVisible = function (visible) {
|
|
|
|
this.visible = visible;
|
2016-07-13 16:52:46 -04:00
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
visible: this.visible
|
|
|
|
});
|
2016-07-01 12:56:59 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Set size of the clone, as a percentage of the costume size.
|
|
|
|
* @param {!number} size Size of clone, from 5 to 535.
|
|
|
|
*/
|
2016-06-30 00:11:47 -04:00
|
|
|
Clone.prototype.setSize = function (size) {
|
2016-07-06 13:57:58 -04:00
|
|
|
// Keep size between 5% and 535%.
|
2016-06-30 00:11:47 -04:00
|
|
|
this.size = MathUtil.clamp(size, 5, 535);
|
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
scale: this.size
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Set a particular graphic effect on this clone.
|
|
|
|
* @param {!string} effectName Name of effect (see `Clone.prototype.effects`).
|
|
|
|
* @param {!number} value Numerical magnitude of effect.
|
|
|
|
*/
|
2016-06-30 00:11:47 -04:00
|
|
|
Clone.prototype.setEffect = function (effectName, value) {
|
|
|
|
this.effects[effectName] = value;
|
|
|
|
var props = {};
|
|
|
|
props[effectName] = this.effects[effectName];
|
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, props);
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Clear all graphic effects on this clone.
|
|
|
|
*/
|
2016-06-30 00:11:47 -04:00
|
|
|
Clone.prototype.clearEffects = function () {
|
|
|
|
for (var effectName in this.effects) {
|
|
|
|
this.effects[effectName] = 0;
|
|
|
|
}
|
|
|
|
self.renderer.updateDrawableProperties(this.drawableID, this.effects);
|
|
|
|
};
|
|
|
|
|
2016-06-29 13:48:30 -04:00
|
|
|
module.exports = Clone;
|