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 Variable = require('../engine/variable');
const MonitorRecord = require('../engine/monitor-record');
const StageLayering = require('../engine/stage-layering');
const {loadCostume} = require('../import/load-costume.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.
const blocks = new Blocks();
// @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.
if (object.hasOwnProperty('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.
const target = sprite.createClone();
const target = sprite.createClone(topLevel ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
const getVariableId = generateVariableIdGetter(target.id, topLevel);

View file

@ -8,6 +8,7 @@ const vmPackage = require('../../package.json');
const Blocks = require('../engine/blocks');
const Sprite = require('../sprites/sprite');
const Variable = require('../engine/variable');
const StageLayering = require('../engine/stage-layering');
const log = require('../util/log');
const uid = require('../util/uid');
@ -684,7 +685,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
const blocks = new Blocks();
// @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.
if (object.hasOwnProperty('name')) {
@ -779,7 +780,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
// process has been completed.
});
// 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.
if (object.hasOwnProperty('tempo')) {
target.tempo = object.tempo;

View file

@ -156,13 +156,11 @@ class RenderedTarget extends Target {
/**
* Create a drawable with the this.renderer.
* @param {boolean} isStage Whether the drawable should be initialized as
* the stage or a sprite
* @param {boolean} layerGroup The layer group this drawable should be added to
*/
initDrawable (isStage) {
initDrawable (layerGroup) {
if (this.renderer) {
this.drawableID = this.renderer.createDrawable(isStage ?
StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
this.drawableID = this.renderer.createDrawable(layerGroup);
}
// If we're a clone, start the hats.
if (!this.isOriginal) {
@ -920,7 +918,7 @@ class RenderedTarget extends Target {
newClone.effects = JSON.parse(JSON.stringify(this.effects));
newClone.variables = JSON.parse(JSON.stringify(this.variables));
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();
// Place behind the current target.
newClone.goBehindOther(this);

View file

@ -3,6 +3,7 @@ const Blocks = require('../engine/blocks');
const {loadSoundFromAsset} = require('../import/load-sound');
const {loadCostumeFromAsset} = require('../import/load-costume');
const StringUtil = require('../util/string-util');
const StageLayering = require('../engine/stage-layering');
class Sprite {
/**
@ -10,10 +11,9 @@ class Sprite {
* 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.
* @param {boolean=} isStage Whether or not this sprite is a stage
* @constructor
*/
constructor (blocks, runtime, isStage) {
constructor (blocks, runtime) {
this.runtime = runtime;
if (!blocks) {
// Shared set of blocks for all clones.
@ -47,10 +47,6 @@ class Sprite {
* @type {Array.<!RenderedTarget>}
*/
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.
* @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.
*/
createClone () {
createClone (optLayerGroup) {
const newClone = new RenderedTarget(this, this.runtime);
newClone.isOriginal = this.clones.length === 0;
this.clones.push(newClone);
newClone.initAudio();
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);
} else {
this.runtime.fireTargetWasCreated(newClone, this.clones[0]);