All files / ts/controller/assets assetsController.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 11/11
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                    1x                     12x 12x 12x 12x 12x       2x 1x   1x 1x       2x 1x   1x 1x       1x       1x       1x       1x       1x       1x       1x       1x    
import { IAudioSource } from "./sources/audioSource.js";
import { IAudioSources } from "./sources/audioSources.js";
import { IImageSource } from "./sources/imageSource.js";
import { IImageSources } from "./sources/imageSources.js";
 
export interface IAssetsController {
    isAudioMuted: boolean;
    addImage(...image: Array<string>): void;
    addAudio(...audio: Array<string>): void;
    removeImage(...image: Array<string>): void;
    removeAudio(...audio: Array<string>): void;
    setImage(image: string, source: IImageSource): void
    setAudio(sound: string, source: IAudioSource): void
    getImage(image: string): IImageSource;
    getAudio(sound: string): IAudioSource;
    loadImagesAsync(): Promise<void>;
    loadAudiosAsync(): Promise<void>;
}
export class AssetsController implements IAssetsController {
 
    private readonly _images: IImageSources;
    private readonly _audios: IAudioSources;
 
    private _audiosLoaded: boolean;
    private _imagesLoaded: boolean;
 
    public isAudioMuted: boolean;
 
    public constructor(images: IImageSources, audios: IAudioSources) {
        this._images = images;
        this._audios = audios;
        this._audiosLoaded = false;
        this._imagesLoaded = false;
        this.isAudioMuted = true;
    }
 
    public async loadImagesAsync(): Promise<void> {
        if (this._imagesLoaded)
            return
 
        this._imagesLoaded = true;
        await this._images.loadAsync();
    }
 
    public async loadAudiosAsync(): Promise<void> {
        if (this._audiosLoaded)
            return
 
        this._audiosLoaded = true;
        await this._audios.loadAsync();
    }
 
    public addImage(...image: Array<string>): void {
        this._images.add(...image);
    }
 
    public addAudio(...audio: Array<string>): void {
        this._audios.add(...audio);
    }
 
    public removeImage(...image: Array<string>): void {
        this._images.remove(...image);
    }
 
    public removeAudio(...audio: Array<string>): void {
        this._audios.remove(...audio);
    }
 
    public setImage(image: string, source: IImageSource): void {
        this._images.set(image, source);
    }
 
    public setAudio(sound: string, source: IAudioSource): void {
        this._audios.set(sound, source);
    }
 
    public getImage(image: string): IImageSource {
        return <IImageSource>this._images.get(image);
    }
 
    public getAudio(sound: string): IAudioSource {
        return <IAudioSource>this._audios.get(sound);
    }
}