2019-06-04 09:34:50 +02:00
|
|
|
/* ES Lint */
|
|
|
|
/* globals PIXI, console*/
|
2019-03-21 09:57:27 +01:00
|
|
|
|
2019-06-01 09:56:29 +02:00
|
|
|
const registeredTiles = new Map()
|
|
|
|
/** Implements a baseTexture cache. The last textures are kept for reuse */
|
2019-06-03 08:55:33 +02:00
|
|
|
let keepTextures = 0
|
2019-06-01 09:56:29 +02:00
|
|
|
const keptTextures = []
|
2019-06-18 16:01:20 +02:00
|
|
|
const lateTextures = new Map()
|
2019-06-18 16:07:07 +02:00
|
|
|
let lastSweepTime = 0
|
|
|
|
let sweepInterval = 2000.0
|
2019-03-21 09:57:27 +01:00
|
|
|
|
|
|
|
/** The current Tile implementation simply uses PIXI.Sprites.
|
|
|
|
*
|
|
|
|
* BTW: PIXI.extras.TilingSprite is not appropriate. It should be used for
|
|
|
|
* repeating patterns.
|
|
|
|
**/
|
2019-06-01 09:56:29 +02:00
|
|
|
export default class Tile extends PIXI.Sprite {
|
2019-03-21 09:57:27 +01:00
|
|
|
constructor(texture, url) {
|
|
|
|
super(texture)
|
|
|
|
this.url = url
|
|
|
|
this.register(url)
|
|
|
|
}
|
|
|
|
|
2019-06-01 09:56:29 +02:00
|
|
|
/**
|
|
|
|
* Static method to enable keeping of base textures
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {*} value
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
static enableKeepTextures(value = 1000) {
|
|
|
|
keepTextures = value
|
|
|
|
}
|
|
|
|
|
2019-06-04 13:09:38 +02:00
|
|
|
/**
|
|
|
|
* Returns true iff the url is pending
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {*} url
|
|
|
|
* @returns
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
/*static isPending(url) {
|
|
|
|
return pendingTiles.has(url) && pendingTiles.get(url) > 0
|
|
|
|
} */
|
|
|
|
|
|
|
|
static isObsolete(url) {
|
|
|
|
if (registeredTiles.has(url) && registeredTiles.get(url) > 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2019-06-01 09:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a tile from image using the PIXI.Texture.fromImage method.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {*} imageId
|
|
|
|
* @param {*} crossorigin
|
|
|
|
* @param {*} scaleMode
|
|
|
|
* @returns
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
2019-03-21 09:57:27 +01:00
|
|
|
static fromImage(imageId, crossorigin, scaleMode) {
|
|
|
|
return new Tile(PIXI.Texture.fromImage(imageId, crossorigin, scaleMode), imageId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the tile in the global reference counter for textures
|
|
|
|
*
|
|
|
|
* @param {*} url
|
|
|
|
* @param {boolean} [debug=false]
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
register(url, debug = false) {
|
2019-06-04 13:09:38 +02:00
|
|
|
//Tile.unschedule(url)
|
2019-06-01 09:56:29 +02:00
|
|
|
if (registeredTiles.has(url)) {
|
|
|
|
let tiles = registeredTiles.get(url)
|
2019-03-21 09:57:27 +01:00
|
|
|
tiles.add(this)
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Tile.register', url, tiles.size)
|
|
|
|
} else {
|
2019-06-01 09:56:29 +02:00
|
|
|
registeredTiles.set(url, new Set([this]))
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Tile.register', url, 1)
|
2019-03-21 09:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters the rile in the global reference counter for textures
|
|
|
|
*
|
|
|
|
* @returns {number} The number of how often a texture is used.
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
unregister() {
|
2019-06-01 09:56:29 +02:00
|
|
|
let tiles = registeredTiles.get(this.url)
|
2019-03-21 09:57:27 +01:00
|
|
|
tiles.delete(this)
|
|
|
|
if (tiles.size == 0) {
|
2019-06-01 09:56:29 +02:00
|
|
|
registeredTiles.delete(this.url)
|
2019-06-03 08:55:33 +02:00
|
|
|
return 0
|
2019-03-21 09:57:27 +01:00
|
|
|
}
|
|
|
|
return tiles.size
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this sprite and optionally its texture and children
|
|
|
|
*
|
|
|
|
* @param {*} options Part of the PIXI API, but ignored in the implementation
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
2019-06-03 10:49:56 +02:00
|
|
|
destroy(options, debug = false) {
|
2019-03-21 09:57:27 +01:00
|
|
|
let count = this.unregister()
|
2019-06-01 09:56:29 +02:00
|
|
|
|
|
|
|
if (keepTextures > 0) {
|
2019-06-03 10:24:10 +02:00
|
|
|
keptTextures.push({ url: this.url, texture: this.texture })
|
2019-06-01 09:56:29 +02:00
|
|
|
|
|
|
|
let opts = { children: true, texture: false, baseTexture: false }
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Tile.destroy', registeredTiles.size, opts)
|
2019-03-21 09:57:27 +01:00
|
|
|
super.destroy(opts)
|
2019-06-01 09:56:29 +02:00
|
|
|
|
2019-06-03 10:24:10 +02:00
|
|
|
while (keptTextures.length > keepTextures) {
|
|
|
|
let { url, texture } = keptTextures.shift()
|
2019-06-03 08:55:33 +02:00
|
|
|
if (Tile.isObsolete(url)) {
|
2019-06-01 09:56:29 +02:00
|
|
|
texture.destroy(true) // Destroy base as well
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Destroying texture and baseTexture', url)
|
2019-06-01 09:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 15:34:57 +02:00
|
|
|
} else {
|
2019-06-01 09:56:29 +02:00
|
|
|
// No longer registered and not pending
|
2019-08-06 15:34:57 +02:00
|
|
|
if (count <= 0) {
|
|
|
|
// && !Tile.isPending(this.url)
|
2019-06-01 09:56:29 +02:00
|
|
|
let opts = { children: true, texture: true, baseTexture: true }
|
|
|
|
super.destroy(opts)
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Tile.destroy', registeredTiles.size, opts)
|
|
|
|
} else {
|
2019-06-01 09:56:29 +02:00
|
|
|
let opts = { children: true, texture: false, baseTexture: false }
|
2019-08-06 15:34:57 +02:00
|
|
|
if (debug) console.log('Tile.destroy', registeredTiles.size, opts)
|
2019-06-01 09:56:29 +02:00
|
|
|
super.destroy(opts)
|
|
|
|
}
|
|
|
|
if (this.parent != null) {
|
|
|
|
// UO: Emit warning and remove
|
2019-08-06 15:34:57 +02:00
|
|
|
console.warn('Destroying tile with parent. Hiding instead')
|
2019-06-01 09:56:29 +02:00
|
|
|
this.visible = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an available texture that can be reused
|
|
|
|
*
|
|
|
|
* @param {*} url
|
|
|
|
* @returns
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
static textureAvailable(url) {
|
|
|
|
if (registeredTiles.has(url)) {
|
|
|
|
let tiles = registeredTiles.get(url)
|
|
|
|
for (let tile of tiles.values()) {
|
|
|
|
//console.log("Reusing cached texture", tile.parent)
|
|
|
|
return tile.texture
|
|
|
|
}
|
2019-03-21 09:57:27 +01:00
|
|
|
}
|
2019-06-01 09:56:29 +02:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:21:47 +02:00
|
|
|
/**
|
|
|
|
* Specialized renderWebGL to avoid freezing system
|
|
|
|
*
|
|
|
|
* @param {*} renderer
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
renderWebGL(renderer) {
|
|
|
|
try {
|
|
|
|
super.renderWebGL(renderer)
|
2019-08-06 15:34:57 +02:00
|
|
|
} catch (e) {
|
2019-06-17 17:21:47 +02:00
|
|
|
// We want persistent logging here
|
2019-08-06 15:34:57 +02:00
|
|
|
Logging.error('Error in Tile.renderWebGL: ' + e.message)
|
2019-06-17 17:21:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:01:20 +02:00
|
|
|
/**
|
|
|
|
* Removes lately arrived texture if they have not been touched in the meanwhile.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberof Tile
|
|
|
|
*/
|
|
|
|
static sweepLateTextures() {
|
|
|
|
let now = performance.now()
|
|
|
|
if (now > lastSweepTime + sweepInterval) {
|
|
|
|
lastSweepTime = now
|
|
|
|
let count = 0
|
|
|
|
for (let [url, texture] of lateTextures.entries()) {
|
|
|
|
if (texture) {
|
|
|
|
let base = texture.baseTexture
|
|
|
|
if (base != null && base.touched == 0) {
|
|
|
|
texture.destroy(true)
|
|
|
|
//console.info("Sweeping ", url)
|
|
|
|
count += 1
|
|
|
|
lateTextures.delete(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 15:34:57 +02:00
|
|
|
if (count > 0) console.log('Sweeping textures', count)
|
2019-06-18 16:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:38 +02:00
|
|
|
/**
|
|
|
|
* Texture received too late. We do not need it.
|
2019-08-06 15:34:57 +02:00
|
|
|
* @param {*} url
|
|
|
|
* @param {*} texture
|
2019-06-01 12:31:38 +02:00
|
|
|
*/
|
|
|
|
static lateTexture(url, texture) {
|
2019-06-18 16:01:20 +02:00
|
|
|
lateTextures.set(url, texture)
|
|
|
|
//console.warn("Tile.lateTexture")
|
|
|
|
// We cannot destroy the texture since we got errors in t.renderWebGL.
|
|
|
|
// Perhaps we can destroy unsed textures later on
|
|
|
|
Tile.sweepLateTextures()
|
2019-06-01 12:31:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 09:56:29 +02:00
|
|
|
static printInfos() {
|
|
|
|
let references = new Map()
|
|
|
|
let multiples = 0
|
|
|
|
for (let [url, tiles] of registeredTiles.entries()) {
|
|
|
|
let count = tiles.size
|
|
|
|
references.set(url, count)
|
|
|
|
if (count > 1) {
|
|
|
|
multiples += 1
|
|
|
|
}
|
2019-05-31 16:10:46 +02:00
|
|
|
}
|
2019-06-01 09:56:29 +02:00
|
|
|
console.log({ multiples, references })
|
2019-03-21 09:57:27 +01:00
|
|
|
}
|
|
|
|
}
|