From 92dd2136a3496da6148aad0dc961f0ff2df4fe47 Mon Sep 17 00:00:00 2001 From: "Blue (Lukas Rieger)" Date: Sat, 23 Jan 2021 11:49:05 +0100 Subject: [PATCH] Switch using Map for a map instead of an Object --- src/map/Map.js | 4 ++-- src/map/TileManager.js | 34 +++++++++++----------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/map/Map.js b/src/map/Map.js index 5ef30e7..218829f 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -293,10 +293,10 @@ export class Map { this.raycaster.layers.enableAll(); let hiresTileHash = hashTile(Math.floor((x - this.hires.translate.x) / this.hires.tileSize.x), Math.floor((z - this.hires.translate.z) / this.hires.tileSize.z)); - let tile = this.hiresTileManager.tiles[hiresTileHash]; + let tile = this.hiresTileManager.tiles.get(hiresTileHash); if (!tile || !tile.model) { let lowresTileHash = hashTile(Math.floor((x - this.lowres.translate.x) / this.lowres.tileSize.x), Math.floor((z - this.lowres.translate.z) / this.lowres.tileSize.z)); - tile = this.lowresTileManager.tiles[lowresTileHash]; + tile = this.lowresTileManager.tiles.get(lowresTileHash); } if (!tile || !tile.model){ diff --git a/src/map/TileManager.js b/src/map/TileManager.js index 045def7..093c45c 100644 --- a/src/map/TileManager.js +++ b/src/map/TileManager.js @@ -51,7 +51,7 @@ export class TileManager { this.loadTimeout = null; //map of loaded tiles - this.tiles = {}; + this.tiles = new Map(); // a canvas that keeps track of the loaded tiles, used for shaders this.tileMap = new TileMap(TileManager.tileMapSize, TileManager.tileMapSize); @@ -70,15 +70,11 @@ export class TileManager { this.removeFarTiles(); this.tileMap.setAll(TileMap.EMPTY); - let keys = Object.keys(this.tiles); - for (let i = 0; i < keys.length; i++) { - if (!this.tiles.hasOwnProperty(keys[i])) continue; - - let tile = this.tiles[keys[i]]; + this.tiles.forEach(tile => { if (!tile.loading) { this.tileMap.setTile(tile.x - this.centerTile.x + TileManager.tileMapHalfSize, tile.z - this.centerTile.y + TileManager.tileMapHalfSize, TileMap.LOADED); } - } + }); } this.loadCloseTiles(); @@ -90,11 +86,7 @@ export class TileManager { } removeFarTiles() { - let keys = Object.keys(this.tiles); - for (let i = 0; i < keys.length; i++) { - if (!this.tiles.hasOwnProperty(keys[i])) continue; - - let tile = this.tiles[keys[i]]; + this.tiles.forEach((tile, hash, map) => { if ( tile.x + this.viewDistanceX < this.centerTile.x || tile.x - this.viewDistanceX > this.centerTile.x || @@ -102,22 +94,18 @@ export class TileManager { tile.z - this.viewDistanceZ > this.centerTile.y ) { tile.unload(); - delete this.tiles[keys[i]]; + map.delete(hash); } - } + }); } removeAllTiles() { this.tileMap.setAll(TileMap.EMPTY); - let keys = Object.keys(this.tiles); - for (let i = 0; i < keys.length; i++) { - if (!this.tiles.hasOwnProperty(keys[i])) continue; - - let tile = this.tiles[keys[i]]; + this.tiles.forEach(tile => { tile.unload(); - delete this.tiles[keys[i]]; - } + }); + this.tiles.clear(); } loadCloseTiles = () => { @@ -166,13 +154,13 @@ export class TileManager { let tileHash = hashTile(x, z); - let tile = this.tiles[tileHash]; + let tile = this.tiles.get(tileHash); if (tile !== undefined) return false; this.currentlyLoading++; tile = new Tile(x, z, this.handleLoadedTile, this.handleUnloadedTile); - this.tiles[tileHash] = tile; + this.tiles.set(tileHash, tile); tile.load(this.tileLoader) .then(() => { this.tileMap.setTile(tile.x - this.centerTile.x + TileManager.tileMapHalfSize, tile.z - this.centerTile.y + TileManager.tileMapHalfSize, TileMap.LOADED);