-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from NicDoesCode/release_1-1-1
Release 1.1.1
- Loading branch information
Showing
10 changed files
with
446 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Tile from "./../models/tile.js"; | ||
import Palette from "./../models/palette.js"; | ||
import PaintUtil from "./../util/paintUtil.js"; | ||
|
||
|
||
/** | ||
* Acts as a shared tile image cache. | ||
*/ | ||
export default class TileImageManager { | ||
|
||
|
||
/** @type {Object.<string, Object<string, Object<string, HTMLCanvasElement>>>} */ | ||
#tileCanvases = {}; | ||
|
||
|
||
/** | ||
* Constructor for the class. | ||
*/ | ||
constructor() { | ||
} | ||
|
||
|
||
/** | ||
* Gets the image for the tile. | ||
* @param {Tile} tile - Tile that we get the image for. | ||
* @param {Palette} palette - Colour palette to use. | ||
* @param {number?} transparencyColour | ||
* @returns {HTMLCanvasElement} | ||
*/ | ||
getTileImage(tile, palette, transparencyColour) { | ||
if (typeof transparencyColour !== 'number' || transparencyColour < 0 || transparencyColour >= 16) { | ||
transparencyColour = -1; | ||
} | ||
|
||
let tileRec = this.#tileCanvases[tile.tileId]; | ||
if (!tileRec) { | ||
tileRec = {}; | ||
this.#tileCanvases[tile.tileId] = tileRec; | ||
} | ||
|
||
let paletteRec = tileRec[palette.paletteId]; | ||
if (!paletteRec) { | ||
paletteRec = {}; | ||
this.#tileCanvases[tile.tileId][palette.paletteId] = paletteRec; | ||
} | ||
|
||
let transId = `${transparencyColour}`; | ||
let transRec = paletteRec[transId] | ||
if (!transRec) { | ||
transRec = PaintUtil.createTileCanvas(tile, palette, transparencyColour); | ||
this.#tileCanvases[tile.tileId][palette.paletteId][transId] = transRec; | ||
} | ||
|
||
return transRec; | ||
} | ||
|
||
|
||
/** | ||
* Clears all cached tile images. | ||
*/ | ||
clear() { | ||
const keys = Object.keys(this.#tileCanvases); | ||
keys.forEach((tileId) => this.clearByTile(tileId)); | ||
} | ||
|
||
/** | ||
* Clears cached tile images for a particular tile. | ||
* @param {string|string[]} value - Array or single unique ID of the tile whose cached images are to be discarded. | ||
*/ | ||
clearByTile(value) { | ||
if (!value) return; | ||
if (Array.isArray(value)) { | ||
value.forEach((tileId) => this.clearByTile(tileId)); | ||
} else if (value && typeof value === 'string' && value.length > 0) { | ||
if (this.#tileCanvases[value]) { | ||
delete this.#tileCanvases[value]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Clears cached tile images for a particular palette. | ||
* @param {string|string[]} value - Array or single unique ID of the palette whose cached images are to be discarded. | ||
*/ | ||
clearByPalette(value) { | ||
if (!value) return; | ||
if (Array.isArray(value)) { | ||
value.forEach((paletteId) => this.clearByPalette(paletteId)); | ||
} else if (value && typeof value === 'string' && value.length > 0) { | ||
Object.keys(this.#tileCanvases).forEach((tileId) => { | ||
if (this.#tileCanvases[tileId][value]) { | ||
delete this.#tileCanvases[tileId][value]; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.