Voglio risparmiare tempo e riutilizzare il codice comune tra le classi che estende le classi PIXI (una libreria di rendering 2d webGl).
Interfacce oggetto:
module Game.Core {
export interface IObject {}
export interface IManagedObject extends IObject{
getKeyInManager(key: string): string;
setKeyInManager(key: string): IObject;
}
}
Il mio problema è che il codice all'interno getKeyInManagere setKeyInManagernon cambierà e voglio riutilizzarlo, non duplicarlo, ecco l'implementazione:
export class ObjectThatShouldAlsoBeExtended{
private _keyInManager: string;
public getKeyInManager(key: string): string{
return this._keyInManager;
}
public setKeyInManager(key: string): DisplayObject{
this._keyInManager = key;
return this;
}
}
Quello che voglio fare è aggiungere automaticamente, tramite a Manager.add(), la chiave usata nel gestore per referenziare l'oggetto all'interno dell'oggetto stesso nella sua proprietà_keyInManager .
Quindi, facciamo un esempio con una Texture. Ecco il fileTextureManager
module Game.Managers {
export class TextureManager extends Game.Managers.Manager {
public createFromLocalImage(name: string, relativePath: string): Game.Core.Texture{
return this.add(name, Game.Core.Texture.fromImage("/" + relativePath)).get(name);
}
}
}
Quando lo faccio this.add(), voglio che il Game.Managers.Manager add()metodo chiami un metodo che esisterebbe sull'oggetto restituito da Game.Core.Texture.fromImage("/" + relativePath). Questo oggetto, in questo caso, sarebbe un Texture:
module Game.Core {
// I must extends PIXI.Texture, but I need to inject the methods in IManagedObject.
export class Texture extends PIXI.Texture {
}
}
So che IManagedObjectè un'interfaccia e non può contenere l'implementazione, ma non so cosa scrivere per iniettare la classe ObjectThatShouldAlsoBeExtendedall'interno della mia Textureclasse. Sapendo che lo stesso processo sarebbe richiesto per Sprite, TilingSprite, Layere altro ancora.
Ho bisogno di un feedback / consigli TypeScript con esperienza qui, deve essere possibile farlo, ma non per più estensioni poiché solo una è possibile al momento, non ho trovato nessun'altra soluzione.