* Resize the tilemap, preserving existing data at the specified anchor position * 调整瓦片地图大小,在指定锚点位置保留现有数据 * @param newWidth New width in tiles | 新宽度(图块数) * @param newHeight New height in tiles | 新高度(图块数) * @param anchor Anchor point for preserving data (default: 'bottom-left' for
(newWidth: number, newHeight: number, anchor: ResizeAnchor = 'bottom-left')
| 1046 | * @param anchor Anchor point for preserving data (default: 'bottom-left' for Y-up coordinate system) | 保留数据的锚点(默认:'bottom-left',适用于Y轴向上的坐标系) |
| 1047 | */ |
| 1048 | resize(newWidth: number, newHeight: number, anchor: ResizeAnchor = 'bottom-left'): void { |
| 1049 | if (newWidth === this._width && newHeight === this._height) { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | // Parse anchor to get X and Y alignment |
| 1054 | // 解析锚点获取X和Y方向的对齐方式 |
| 1055 | let xAnchor: 'start' | 'center' | 'end'; |
| 1056 | let yAnchor: 'start' | 'center' | 'end'; |
| 1057 | |
| 1058 | if (anchor.includes('left')) xAnchor = 'start'; |
| 1059 | else if (anchor.includes('right')) xAnchor = 'end'; |
| 1060 | else xAnchor = 'center'; |
| 1061 | |
| 1062 | if (anchor.includes('bottom')) yAnchor = 'end'; |
| 1063 | else if (anchor.includes('top')) yAnchor = 'start'; |
| 1064 | else yAnchor = 'center'; |
| 1065 | |
| 1066 | // Calculate offsets for placing old data in new array |
| 1067 | // 计算将旧数据放入新数组的偏移量 |
| 1068 | const offsetX = this.calculateAnchorOffset(this._width, newWidth, xAnchor); |
| 1069 | const offsetY = this.calculateAnchorOffset(this._height, newHeight, yAnchor); |
| 1070 | |
| 1071 | // 调整所有图层 |
| 1072 | for (const layer of this._layers) { |
| 1073 | const oldLayerData = this._layersData.get(layer.id); |
| 1074 | const newLayerData = new Uint32Array(newWidth * newHeight); |
| 1075 | const newDataArray = new Array(newWidth * newHeight).fill(0); |
| 1076 | |
| 1077 | if (oldLayerData) { |
| 1078 | for (let y = 0; y < this._height; y++) { |
| 1079 | for (let x = 0; x < this._width; x++) { |
| 1080 | const newX = x + offsetX; |
| 1081 | const newY = y + offsetY; |
| 1082 | |
| 1083 | // Check bounds |
| 1084 | if (newX >= 0 && newX < newWidth && newY >= 0 && newY < newHeight) { |
| 1085 | const value = oldLayerData[y * this._width + x]; |
| 1086 | newLayerData[newY * newWidth + newX] = value; |
| 1087 | newDataArray[newY * newWidth + newX] = value; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | this._layersData.set(layer.id, newLayerData); |
| 1094 | layer.data = newDataArray; |
| 1095 | } |
| 1096 | |
| 1097 | // 调整碰撞数据 |
| 1098 | if (this._collisionData.length > 0) { |
| 1099 | const newCollisionData = new Uint32Array(newWidth * newHeight); |
| 1100 | const newCollisionArray = new Array(newWidth * newHeight).fill(0); |
| 1101 | |
| 1102 | for (let y = 0; y < this._height; y++) { |
| 1103 | for (let x = 0; x < this._width; x++) { |
| 1104 | const newX = x + offsetX; |
| 1105 | const newY = y + offsetY; |