All files tilemap.mjs

81.13% Statements 43/53
68.75% Branches 11/16
100% Functions 4/4
81.13% Lines 43/53

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 531x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x     1x     1x     1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
 
 
 
 
class Tile {
    constructor(tileDef) {
        this.index = tileDef.index || 0;
        this.srcX = tileDef.srcX || 0;
        this.srcY = tileDef.srcY || 0;
        this.width = tileDef.width || 32;
        this.height = tileDef.height || 32;
        this.frameCount = tileDef.frameCount || 1;
        this.frameLayout = tileDef.frameLayout || 'horizontal';
        this.frameRate = tileDef.frameRate || 15;
    }
}
 
 
 
class Tilemap {
    constructor(mapDef) {
        if(this.validateMapDef(mapDef)){
            this.importMapDef(mapDef);
        } else {
            throw new Error('Invalid map definition provided to Tilemap.');
        }
    }
 
    validateMapDef(mapDef) {
        if (typeof mapDef !== 'object' || mapDef === null) {
            throw new Error('Invalid map definition: must be a non-null object.');
        }
        if (typeof mapDef.srcImage !== 'object' || mapDef.srcImage === null) {
            throw new Error('Invalid map definition: srcImage must be an Image() object.');
        }
        if (mapDef.srcImage.complete === false) {
            throw new Error('Invalid map definition: srcImage must be fully loaded.');
        }
        if (!Array.isArray(mapDef.tiles) || mapDef.tiles.length === 0) {
            throw new Error('Invalid map definition: tiles must be a non-empty array.');
        }
        return true;
    }
 
    importMapDef(mapDef) {
        this.srcImage = mapDef.srcImage;
        this.mapWidth = this.srcImage.naturalWidth;
        this.mapHeight = this.srcImage.naturalHeight;
        this.tiles = mapDef.tiles.map(tileDef => new Tile(tileDef));
    }
}
 
export { Tile, Tilemap };