import { SymbolData } from '../types'; import { ObjectBase, Image } from "dsg-engine"; export class Symbol extends ObjectBase { private _symbolImg: Image; private _symbolCode: number; public get symbolCode() { return this._symbolData.id; } public get spritesheetName() { return this._symbolData.spritesheet; } public get symbolData() { return this._symbolData; } /** * @param _symbolData Object containing symbol data (like id and spritesheet) * @param name OPTIONAL name of the symbol, used for debugging. If not provided, the name of the spritesheet is used. */ constructor( private _symbolData: SymbolData, name?: string ) { super({name}); this.create(_symbolData.spritesheet); } /** * Toggle the fade effect on the symbol * @param fade True to fade the symbol, false to show it */ public toggleFade(fade: boolean) { if (fade) { this._symbolImg.alpha = 0.6; } else { this._symbolImg.alpha = 1; } } /** * Start the win animation */ public playWinAnimation() { this.toggleFade(false); this._symbolImg.srcAnimatedSprite.animationSpeed = 0.7; this._symbolImg.srcAnimatedSprite.gotoAndPlay(0); } /** * Stop the win animation */ public stopWinAnimation() { this._symbolImg.srcAnimatedSprite.gotoAndStop(0); } /** * Change symbol, use a loaded spritesheet * @param symbolCode Identifying code of the symbol * @param src The name of the source spritesheet */ // public change(symbolCode: number, src: string) { // this._symbolImg.destroy(); // this.removeChild(this._symbolImg); // this._symbolCode = symbolCode; // this.create(src); // } public change(symbolData: SymbolData) { this._symbolImg.destroy(); this.removeChild(this._symbolImg); this._symbolData = symbolData; this.create(symbolData.spritesheet); } /** * Change this symbol to another symbol * @param symbol the symbol object to copy data from */ public copy(symbol: Symbol) { this.change(symbol.symbolData); } /** * Create the symbol image * @param src The name of the source spritesheet */ private create(src: string) { this._symbolImg = Image.CreateAnimated(src, this.name); this._symbolImg.srcAnimatedSprite.anchor.set(0.5, 0.5); this._symbolImg.srcAnimatedSprite.loop = true; this.addChild(this._symbolImg); } }