Refactor so that layer group is optionally passed into createClone (and initDrawable) instead of isStage property getting stored on a sprite.

This commit is contained in:
Karishma Chadha 2018-05-18 10:11:59 -04:00
parent e2ce047fa1
commit 64c8484066
4 changed files with 18 additions and 18 deletions

View file

@ -15,6 +15,7 @@ const StringUtil = require('../util/string-util');
const specMap = require('./sb2_specmap'); const specMap = require('./sb2_specmap');
const Variable = require('../engine/variable'); const Variable = require('../engine/variable');
const MonitorRecord = require('../engine/monitor-record'); const MonitorRecord = require('../engine/monitor-record');
const StageLayering = require('../engine/stage-layering');
const {loadCostume} = require('../import/load-costume.js'); const {loadCostume} = require('../import/load-costume.js');
const {loadSound} = require('../import/load-sound.js'); const {loadSound} = require('../import/load-sound.js');
@ -327,7 +328,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
// Blocks container for this object. // Blocks container for this object.
const blocks = new Blocks(); const blocks = new Blocks();
// @todo: For now, load all Scratch objects (stage/sprites) as a Sprite. // @todo: For now, load all Scratch objects (stage/sprites) as a Sprite.
const sprite = new Sprite(blocks, runtime, topLevel /* whether this sprite is a stge or not */); const sprite = new Sprite(blocks, runtime);
// Sprite/stage name from JSON. // Sprite/stage name from JSON.
if (object.hasOwnProperty('objName')) { if (object.hasOwnProperty('objName')) {
sprite.name = topLevel ? 'Stage' : object.objName; sprite.name = topLevel ? 'Stage' : object.objName;
@ -399,7 +400,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
} }
// Create the first clone, and load its run-state from JSON. // Create the first clone, and load its run-state from JSON.
const target = sprite.createClone(); const target = sprite.createClone(topLevel ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
const getVariableId = generateVariableIdGetter(target.id, topLevel); const getVariableId = generateVariableIdGetter(target.id, topLevel);

View file

@ -8,6 +8,7 @@ const vmPackage = require('../../package.json');
const Blocks = require('../engine/blocks'); const Blocks = require('../engine/blocks');
const Sprite = require('../sprites/sprite'); const Sprite = require('../sprites/sprite');
const Variable = require('../engine/variable'); const Variable = require('../engine/variable');
const StageLayering = require('../engine/stage-layering');
const log = require('../util/log'); const log = require('../util/log');
const uid = require('../util/uid'); const uid = require('../util/uid');
@ -684,7 +685,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
const blocks = new Blocks(); const blocks = new Blocks();
// @todo: For now, load all Scratch objects (stage/sprites) as a Sprite. // @todo: For now, load all Scratch objects (stage/sprites) as a Sprite.
const sprite = new Sprite(blocks, runtime, object.isStage); const sprite = new Sprite(blocks, runtime);
// Sprite/stage name from JSON. // Sprite/stage name from JSON.
if (object.hasOwnProperty('name')) { if (object.hasOwnProperty('name')) {
@ -779,7 +780,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
// process has been completed. // process has been completed.
}); });
// Create the first clone, and load its run-state from JSON. // Create the first clone, and load its run-state from JSON.
const target = sprite.createClone(); const target = sprite.createClone(object.isStage ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
// Load target properties from JSON. // Load target properties from JSON.
if (object.hasOwnProperty('tempo')) { if (object.hasOwnProperty('tempo')) {
target.tempo = object.tempo; target.tempo = object.tempo;

View file

@ -156,13 +156,11 @@ class RenderedTarget extends Target {
/** /**
* Create a drawable with the this.renderer. * Create a drawable with the this.renderer.
* @param {boolean} isStage Whether the drawable should be initialized as * @param {boolean} layerGroup The layer group this drawable should be added to
* the stage or a sprite
*/ */
initDrawable (isStage) { initDrawable (layerGroup) {
if (this.renderer) { if (this.renderer) {
this.drawableID = this.renderer.createDrawable(isStage ? this.drawableID = this.renderer.createDrawable(layerGroup);
StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
} }
// If we're a clone, start the hats. // If we're a clone, start the hats.
if (!this.isOriginal) { if (!this.isOriginal) {
@ -920,7 +918,7 @@ class RenderedTarget extends Target {
newClone.effects = JSON.parse(JSON.stringify(this.effects)); newClone.effects = JSON.parse(JSON.stringify(this.effects));
newClone.variables = JSON.parse(JSON.stringify(this.variables)); newClone.variables = JSON.parse(JSON.stringify(this.variables));
newClone.lists = JSON.parse(JSON.stringify(this.lists)); newClone.lists = JSON.parse(JSON.stringify(this.lists));
newClone.initDrawable(false); // this,sprite is not a stage if we're calling makeClone on it newClone.initDrawable(StageLayering.SPRITE_LAYER); // TODO should sprite clones be in their own layer group?
newClone.updateAllDrawableProperties(); newClone.updateAllDrawableProperties();
// Place behind the current target. // Place behind the current target.
newClone.goBehindOther(this); newClone.goBehindOther(this);

View file

@ -3,6 +3,7 @@ const Blocks = require('../engine/blocks');
const {loadSoundFromAsset} = require('../import/load-sound'); const {loadSoundFromAsset} = require('../import/load-sound');
const {loadCostumeFromAsset} = require('../import/load-costume'); const {loadCostumeFromAsset} = require('../import/load-costume');
const StringUtil = require('../util/string-util'); const StringUtil = require('../util/string-util');
const StageLayering = require('../engine/stage-layering');
class Sprite { class Sprite {
/** /**
@ -10,10 +11,9 @@ class Sprite {
* All clones of a sprite have shared blocks, shared costumes, shared variables. * All clones of a sprite have shared blocks, shared costumes, shared variables.
* @param {?Blocks} blocks Shared blocks object for all clones of sprite. * @param {?Blocks} blocks Shared blocks object for all clones of sprite.
* @param {Runtime} runtime Reference to the runtime. * @param {Runtime} runtime Reference to the runtime.
* @param {boolean=} isStage Whether or not this sprite is a stage
* @constructor * @constructor
*/ */
constructor (blocks, runtime, isStage) { constructor (blocks, runtime) {
this.runtime = runtime; this.runtime = runtime;
if (!blocks) { if (!blocks) {
// Shared set of blocks for all clones. // Shared set of blocks for all clones.
@ -47,10 +47,6 @@ class Sprite {
* @type {Array.<!RenderedTarget>} * @type {Array.<!RenderedTarget>}
*/ */
this.clones = []; this.clones = [];
// Needed for figuring out whether the associated drawable should
// go in the background layer or sprite layer
this.isStage = isStage || false;
} }
/** /**
@ -100,15 +96,19 @@ class Sprite {
/** /**
* Create a clone of this sprite. * Create a clone of this sprite.
* @param {string=} optLayerGroup Optional layer group the clone's drawable should be added to
* Defaults to the sprite layer group
* @returns {!RenderedTarget} Newly created clone. * @returns {!RenderedTarget} Newly created clone.
*/ */
createClone () { createClone (optLayerGroup) {
const newClone = new RenderedTarget(this, this.runtime); const newClone = new RenderedTarget(this, this.runtime);
newClone.isOriginal = this.clones.length === 0; newClone.isOriginal = this.clones.length === 0;
this.clones.push(newClone); this.clones.push(newClone);
newClone.initAudio(); newClone.initAudio();
if (newClone.isOriginal) { if (newClone.isOriginal) {
newClone.initDrawable(this.isStage); // Default to the sprite layer group if optLayerGroup is not provided
const layerGroup = typeof optLayerGroup === 'string' ? optLayerGroup : StageLayering.SPRITE_LAYER;
newClone.initDrawable(layerGroup);
this.runtime.fireTargetWasCreated(newClone); this.runtime.fireTargetWasCreated(newClone);
} else { } else {
this.runtime.fireTargetWasCreated(newClone, this.clones[0]); this.runtime.fireTargetWasCreated(newClone, this.clones[0]);