converted cache to constant lookup time

This commit is contained in:
jonny 2021-06-20 19:14:04 +02:00
parent 92485a02cf
commit 5bf943ce77

View file

@ -12,7 +12,7 @@ export class GameMap {
private lastProperties = new Map<string, string | boolean | number>(); private lastProperties = new Map<string, string | boolean | number>();
private callbacks = new Map<string, Array<PropertyChangeCallback>>(); private callbacks = new Map<string, Array<PropertyChangeCallback>>();
private tileSetPropertyMap: { [tilset_firstgid: number]: { [tile_id: number]: Array<ITiledMapLayerProperty> } } = {} private tileSetPropertyMap: { [tile_index: number]: Array<ITiledMapLayerProperty> } = {}
public readonly layersIterator: LayersIterator; public readonly layersIterator: LayersIterator;
public exitUrls: Array<string> = [] public exitUrls: Array<string> = []
@ -21,12 +21,9 @@ export class GameMap {
this.layersIterator = new LayersIterator(map); this.layersIterator = new LayersIterator(map);
for (const tileset of map.tilesets) { for (const tileset of map.tilesets) {
if (!this.tileSetPropertyMap[tileset.firstgid]) {
this.tileSetPropertyMap[tileset.firstgid] = {}
}
tileset?.tiles?.forEach(tile => { tileset?.tiles?.forEach(tile => {
if (tile.properties) { if (tile.properties) {
this.tileSetPropertyMap[tileset.firstgid][tile.id] = tile.properties this.tileSetPropertyMap[tileset.firstgid + tile.id] = tile.properties
tile.properties.forEach(prop => { tile.properties.forEach(prop => {
if (prop.name == "exitUrl" && typeof prop.value == "string") { if (prop.name == "exitUrl" && typeof prop.value == "string") {
this.exitUrls.push(prop.value); this.exitUrls.push(prop.value);
@ -105,18 +102,13 @@ export class GameMap {
} }
if (tileIndex) { if (tileIndex) {
const tileset = this.map.tilesets.find(tileset => tileset.firstgid + tileset.tilecount > (tileIndex as number)) this.tileSetPropertyMap[tileIndex]?.forEach(property => {
if (tileset) { if (property.value) {
const tileProperties = this.tileSetPropertyMap[tileset?.firstgid][tileIndex - tileset.firstgid] properties.set(property.name, property.value)
tileProperties?.forEach(property => { } else if (properties.has(property.name)) {
if (property.value) { properties.delete(property.name)
properties.set(property.name, property.value) }
} else if (properties.has(property.name)) { })
properties.delete(property.name)
}
})
}
} }
} }