|
| 1 | +import { Container, Graphics } from 'pixi.js'; |
| 2 | +import type { Game } from '../types/game'; |
| 3 | +import gameHelper from '../services/gameHelper' |
| 4 | +import type Star from './star'; |
| 5 | +import type Carrier from './carrier'; |
| 6 | +import type { MapObject } from './mapObject'; |
| 7 | + |
| 8 | +const CHUNK_SIZE = 256 |
| 9 | + |
| 10 | +class Chunk extends Container { |
| 11 | + mapObjects: MapObject[] = []; |
| 12 | + visualizer: Graphics | undefined = undefined; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + |
| 17 | + this.sortableChildren = true; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export class Chunks { |
| 22 | + chunks: Chunk[][] = []; |
| 23 | + numof_chunkX: number = 0; |
| 24 | + numof_chunkY: number = 0; |
| 25 | + firstChunkX: number = 0; |
| 26 | + firstChunkY: number = 0; |
| 27 | + lastChunkX: number = 0; |
| 28 | + lastChunkY: number = 0; |
| 29 | + minMouseChunkX: number = 0; |
| 30 | + maxMouseChunkX: number = 0; |
| 31 | + minMouseChunkY: number = 0; |
| 32 | + maxMouseChunkY: number = 0; |
| 33 | + chunksContainer: Container; |
| 34 | + |
| 35 | + constructor() { |
| 36 | + this.chunksContainer = new Container() |
| 37 | + this.chunksContainer.zIndex = 7; |
| 38 | + } |
| 39 | + |
| 40 | + setup(game: Game, stars: Star[], carriers: Carrier[]) { |
| 41 | + this.chunksContainer.removeChildren(); |
| 42 | + |
| 43 | + const carrierMinX = gameHelper.calculateMinCarrierX(game) |
| 44 | + const carrierMinY = gameHelper.calculateMinCarrierY(game) |
| 45 | + const carrierMaxX = gameHelper.calculateMaxCarrierX(game) |
| 46 | + const carrierMaxY = gameHelper.calculateMaxCarrierY(game) |
| 47 | + |
| 48 | + const starMinX = gameHelper.calculateMinStarX(game) |
| 49 | + const starMinY = gameHelper.calculateMinStarY(game) |
| 50 | + const starMaxX = gameHelper.calculateMaxStarX(game) |
| 51 | + const starMaxY = gameHelper.calculateMaxStarY(game) |
| 52 | + |
| 53 | + const minX = Math.min(carrierMinX, starMinX) |
| 54 | + const minY = Math.min(carrierMinY, starMinY) |
| 55 | + const maxX = Math.max(carrierMaxX, starMaxX) |
| 56 | + const maxY = Math.max(carrierMaxY, starMaxY) |
| 57 | + |
| 58 | + this.firstChunkX = Math.floor(minX / CHUNK_SIZE) |
| 59 | + this.firstChunkY = Math.floor(minY / CHUNK_SIZE) |
| 60 | + |
| 61 | + this.lastChunkX = Math.floor(maxX / CHUNK_SIZE) |
| 62 | + this.lastChunkY = Math.floor(maxY / CHUNK_SIZE) |
| 63 | + |
| 64 | + this.numof_chunkX = this.lastChunkX - this.firstChunkX + 1 |
| 65 | + this.numof_chunkY = this.lastChunkY - this.firstChunkY + 1 |
| 66 | + |
| 67 | + let chunkColumns = Array(this.numof_chunkX) |
| 68 | + for (let i = 0; i < this.numof_chunkX; i++) { chunkColumns[i] = Array(this.numof_chunkY) } |
| 69 | + |
| 70 | + this.chunks = chunkColumns |
| 71 | + |
| 72 | + for (let ix = 0; ix < this.numof_chunkX; ix++) { |
| 73 | + for (let iy = 0; iy < this.numof_chunkY; iy++) { |
| 74 | + this.chunks[ix][iy] = new Chunk(); |
| 75 | + this.chunksContainer!.addChild(this.chunks[ix][iy]) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + stars.forEach(s => this.addContainerToChunk(s, this.firstChunkX, this.firstChunkY)) |
| 80 | + carriers.forEach(c => this.addContainerToChunk(c, this.firstChunkX, this.firstChunkY)) |
| 81 | + } |
| 82 | + |
| 83 | + onTick (positionChanging: boolean, zoomChanging: boolean, zoomPercent: number, viewport: { left: number, right: number, bottom: number, top: number }) { |
| 84 | + const firstX = Math.floor(viewport.left/CHUNK_SIZE) |
| 85 | + const firstY = Math.floor(viewport.top/CHUNK_SIZE) |
| 86 | + |
| 87 | + const lastX = Math.floor(viewport.right/CHUNK_SIZE) |
| 88 | + const lastY = Math.floor(viewport.bottom/CHUNK_SIZE) |
| 89 | + |
| 90 | + if (!positionChanging && !zoomChanging) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + for(let ix=0; ix<this.numof_chunkX; ix++) { |
| 95 | + for(let iy=0; iy<this.numof_chunkY; iy++) { |
| 96 | + if( |
| 97 | + (ix>=(firstX-this.firstChunkX))&&(ix<=(lastX-this.firstChunkX)) && |
| 98 | + (iy>=(firstY-this.firstChunkY))&&(iy<=(lastY-this.firstChunkY)) |
| 99 | + ) |
| 100 | + { |
| 101 | + if( !this.chunks[ix][iy].visible ) { |
| 102 | + this.chunks[ix][iy].visible = true |
| 103 | + this.chunks[ix][iy].interactiveChildren = true |
| 104 | + //this.chunks[ix][iy].visualizer.visible = true |
| 105 | + for( let mapObject of this.chunks[ix][iy].mapObjects ) { |
| 106 | + mapObject.onZoomChanging(zoomPercent) |
| 107 | + } |
| 108 | + } |
| 109 | + else { |
| 110 | + if( zoomChanging ) { |
| 111 | + for( let mapObject of this.chunks[ix][iy].mapObjects ) { |
| 112 | + mapObject.onZoomChanging(zoomPercent) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + else { |
| 118 | + this.chunks[ix][iy].visible = false |
| 119 | + this.chunks[ix][iy].interactiveChildren = false |
| 120 | + //this.chunks[ix][iy].visualizer.visible = false |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + addContainerToChunk(mapObject: MapObject, firstX: number, firstY: number) { // Star or carrier |
| 127 | + const chunkX = Math.floor(mapObject.getLocation().x / CHUNK_SIZE) |
| 128 | + const chunkY = Math.floor(mapObject.getLocation().y / CHUNK_SIZE) |
| 129 | + const ix = chunkX - firstX |
| 130 | + const iy = chunkY - firstY |
| 131 | + |
| 132 | + this.chunks[ix][iy].addChild(mapObject.getContainer()) |
| 133 | + this.chunks[ix][iy].mapObjects.push(mapObject) |
| 134 | + } |
| 135 | + |
| 136 | + removeContainerFromChunk(mapObject: MapObject, chunks: Chunk[][], firstX: number, firstY: number) { |
| 137 | + const chunkX = Math.floor(mapObject.getLocation().x / CHUNK_SIZE) |
| 138 | + const chunkY = Math.floor(mapObject.getLocation().y / CHUNK_SIZE) |
| 139 | + const ix = chunkX - firstX |
| 140 | + const iy = chunkY - firstY |
| 141 | + |
| 142 | + chunks[ix][iy].removeChild(mapObject.getContainer()) |
| 143 | + const index = chunks[ix][iy].mapObjects.indexOf(mapObject) |
| 144 | + if (index > -1) { chunks[ix][iy].mapObjects.splice(index, 1) } |
| 145 | + } |
| 146 | + |
| 147 | + removeMapObjectFromChunks(mapObject: MapObject) { |
| 148 | + for (let chunkX of this.chunks) { |
| 149 | + for (let chunkY of chunkX) { |
| 150 | + if (chunkY.mapObjects.indexOf(mapObject) > -1) { |
| 151 | + chunkY.mapObjects.splice(chunkY.mapObjects.indexOf(mapObject), 1) |
| 152 | + chunkY.removeChild(mapObject.getContainer()) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments