| | const RenderedTarget = require('./rendered-target'); |
| | const Blocks = require('../engine/blocks'); |
| | const {loadSoundFromAsset} = require('../import/load-sound'); |
| | const {loadCostumeFromAsset} = require('../import/load-costume'); |
| | const newBlockIds = require('../util/new-block-ids'); |
| | const StringUtil = require('../util/string-util'); |
| | const StageLayering = require('../engine/stage-layering'); |
| |
|
| | class Sprite { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | constructor (blocks, runtime) { |
| | this.runtime = runtime; |
| | if (!blocks) { |
| | |
| | blocks = new Blocks(runtime); |
| | } |
| | this.blocks = blocks; |
| | |
| | |
| | |
| | |
| | this.name = ''; |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | this.costumes_ = []; |
| | |
| | |
| | |
| | this.sounds = []; |
| | |
| | |
| | |
| | |
| | this.clones = []; |
| |
|
| | this.soundBank = null; |
| | if (this.runtime && this.runtime.audioEngine) { |
| | this.soundBank = this.runtime.audioEngine.createBank(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | set costumes (costumes) { |
| | this.costumes_ = []; |
| | for (const costume of costumes) { |
| | this.addCostumeAt(costume, this.costumes_.length); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | get costumes () { |
| | return this.costumes_; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | addCostumeAt (costumeObject, index) { |
| | if (!costumeObject.name) { |
| | costumeObject.name = ''; |
| | } |
| | const usedNames = this.costumes_.map(costume => costume.name); |
| | costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames); |
| | this.costumes_.splice(index, 0, costumeObject); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | deleteCostumeAt (index) { |
| | return this.costumes.splice(index, 1)[0]; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | createClone (optLayerGroup) { |
| | const newClone = new RenderedTarget(this, this.runtime); |
| | newClone.isOriginal = this.clones.length === 0; |
| | this.clones.push(newClone); |
| | newClone.initAudio(); |
| | if (newClone.isOriginal) { |
| | |
| | const layerGroup = typeof optLayerGroup === 'string' ? optLayerGroup : StageLayering.SPRITE_LAYER; |
| | newClone.initDrawable(layerGroup); |
| | this.runtime.fireTargetWasCreated(newClone); |
| | } else { |
| | this.runtime.fireTargetWasCreated(newClone, this.clones[0]); |
| | } |
| | return newClone; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | removeClone (clone) { |
| | this.runtime.fireTargetWasRemoved(clone); |
| | const cloneIndex = this.clones.indexOf(clone); |
| | if (cloneIndex >= 0) { |
| | this.clones.splice(cloneIndex, 1); |
| | } |
| | } |
| |
|
| | duplicate () { |
| | const newSprite = new Sprite(null, this.runtime); |
| | const blocksContainer = this.blocks._blocks; |
| | const originalBlocks = Object.keys(blocksContainer).map(key => blocksContainer[key]); |
| | const copiedBlocks = JSON.parse(JSON.stringify(originalBlocks)); |
| | newBlockIds(copiedBlocks); |
| | copiedBlocks.forEach(block => { |
| | newSprite.blocks.createBlock(block); |
| | }); |
| |
|
| |
|
| | const allNames = this.runtime.targets.map(t => t.sprite.name); |
| | newSprite.name = StringUtil.unusedName(this.name, allNames); |
| |
|
| | const assetPromises = []; |
| |
|
| | newSprite.costumes = this.costumes_.map(costume => { |
| | const newCostume = Object.assign({}, costume); |
| | assetPromises.push(loadCostumeFromAsset(newCostume, this.runtime)); |
| | return newCostume; |
| | }); |
| |
|
| | newSprite.sounds = this.sounds.map(sound => { |
| | const newSound = Object.assign({}, sound); |
| | const soundAsset = sound.asset; |
| | assetPromises.push(loadSoundFromAsset(newSound, soundAsset, this.runtime, newSprite.soundBank)); |
| | return newSound; |
| | }); |
| |
|
| | return Promise.all(assetPromises).then(() => newSprite); |
| | } |
| |
|
| | dispose () { |
| | if (this.soundBank) { |
| | this.soundBank.dispose(); |
| | } |
| | } |
| | } |
| |
|
| | module.exports = Sprite; |
| |
|