difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 https://www.cocos.com/
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated engine source code (the "Software"), a limited,
 worldwide, royalty-free, non-assignable, revocable and non-exclusive license
 to use Cocos Creator solely to develop games on your target platforms. You shall
 not use Cocos Creator software for developing other software or tools that's
 used for developing games. You are not granted to publish, distribute,
 sublicense, and/or sell copies of Cocos Creator.
 
 The software or tools in this License Agreement are licensed, not sold.
 Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
 
const TiledLayer = require('./CCTiledLayer');
const TiledMap = require('./CCTiledMap');
 
const renderEngine = require('../core/renderer/render-engine');
const RenderFlow = require('../core/renderer/render-flow');
 
const Orientation = TiledMap.Orientation;
const TileFlag = TiledMap.TileFlag;
const FLIPPED_MASK = TileFlag.FLIPPED_MASK;
const StaggerAxis = TiledMap.StaggerAxis;
const StaggerIndex = TiledMap.StaggerIndex;
 
const math = renderEngine.math;
const mat4 = math.mat4;
const vec3 = math.vec3;
 
let _mat4_temp = mat4.create();
let _mat4_temp2 = mat4.create();
let _vec3_temp = vec3.create();
 
let tmxAssembler = {
    updateRenderData (comp) {
        let renderData = comp._renderData;
        if (!renderData) {
            renderData = comp._renderData = comp.requestRenderData();
        }
 
        let size = comp.node._contentSize;
        let anchor = comp.node._anchorPoint;
        renderData.updateSizeNPivot(size.width, size.height, anchor.x, anchor.y);
        renderData.material = comp.getMaterial();
        
        this.updateVertices(comp);
    },
 
    fillBuffers (comp, renderer) {
        let renderData = comp._renderData;
        let data = renderData._data;
 
        let buffer = renderer._meshBuffer,
            vertexCount = renderData.vertexCount;
            
        let offsetInfo = buffer.request(vertexCount, renderData.indiceCount);
 
        // buffer data may be realloc, need get reference after request.
        let indiceOffset = offsetInfo.indiceOffset,
            vertexOffset = offsetInfo.byteOffset >> 2,
            vertexId = offsetInfo.vertexOffset,
            vbuf = buffer._vData,
            ibuf = buffer._iData,
            uintbuf = buffer._uintVData;
        
        for (let i = 0, l = renderData.vertexCount; i < l; i++) {
            let vert = data[i];
            vbuf[vertexOffset++] = vert.x;
            vbuf[vertexOffset++] = vert.y;
            vbuf[vertexOffset++] = vert.u;
            vbuf[vertexOffset++] = vert.v;
            uintbuf[vertexOffset++] = vert.color;
        }
 
        for (let i = 0, l = renderData.indiceCount; i < l; i+=6) {
            ibuf[indiceOffset++] = vertexId;
            ibuf[indiceOffset++] = vertexId+1;
            ibuf[indiceOffset++] = vertexId+2;
            ibuf[indiceOffset++] = vertexId+1;
            ibuf[indiceOffset++] = vertexId+3;
            ibuf[indiceOffset++] = vertexId+2;
            vertexId += 4;
        }
 
        comp.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
    },
 
    updateVertices (comp) {
        let node = comp.node;
        let renderData = comp._renderData;
        let data = renderData._data;
        let color = node._color._val;
        let opacity = node._color.a;
 
        renderData.dataLength = renderData.vertexCount = renderData.indiceCount = 0;
 
        let layerOrientation = comp._layerOrientation,
        tiles = comp._tiles;
 
        if (!tiles || !comp._tileset) {
            return;
        }
        
        let appx = node._anchorPoint.x * node._contentSize.width,
            appy = node._anchorPoint.y * node._contentSize.height;
 
        mat4.copy(_mat4_temp, node._worldMatrix);
        vec3.set(_vec3_temp, -appx, -appy, 0);
        mat4.translate(_mat4_temp, _mat4_temp, _vec3_temp);
 
        let a = _mat4_temp.m00, b = _mat4_temp.m01, c = _mat4_temp.m04, d = _mat4_temp.m05,
            tx = _mat4_temp.m12, ty = _mat4_temp.m13;
 
        let maptw = comp._mapTileSize.width,
            mapth = comp._mapTileSize.height,
            tilew = comp._tileset._tileSize.width,
            tileh = comp._tileset._tileSize.height,
            extw = tilew - maptw,
            exth = tileh - mapth,
            winw = cc.winSize.width,
            winh = cc.winSize.height,
            rows = comp._layerSize.height,
            cols = comp._layerSize.width,
            grids = comp._texGrids,
            tiledTiles = comp._tiledTiles,
            ox = comp._offset.x,
            oy = comp._offset.y,
            w = tilew * a, h = tileh * d;
 
        tx += ox * a + oy * c;
        ty += ox * b + oy * d;
        // Culling
        let startCol = 0, startRow = 0,
            maxCol = cols, maxRow = rows;
 
        let cullingA = a, cullingD = d,
            cullingMapx = tx, cullingMapy = ty,
            cullingW = w, cullingH = h;
        let enabledCulling = cc.macro.ENABLE_TILEDMAP_CULLING;
        
        if (enabledCulling) {
            let camera = cc.Camera.findCamera(comp.node);
            if (camera) {
                camera.getWorldToCameraMatrix(_mat4_temp2);
                mat4.mul(_mat4_temp, _mat4_temp2, _mat4_temp);
                cullingA = _mat4_temp.m00;
                cullingD = _mat4_temp.m05;
                cullingMapx = ox * cullingA + oy * _mat4_temp.m04 + _mat4_temp.m12;
                cullingMapy = ox * _mat4_temp.m01 + oy * cullingD + _mat4_temp.m13;
                cullingW = tilew * cullingA;
                cullingH = tileh * cullingD;
            }
                
            if (layerOrientation === Orientation.ORTHO) {
                mat4.invert(_mat4_temp, _mat4_temp);
 
                let rect = cc.visibleRect;
                let a = _mat4_temp.m00, b = _mat4_temp.m01, c = _mat4_temp.m04, d = _mat4_temp.m05,
                    tx = _mat4_temp.m12, ty = _mat4_temp.m13;
                let v0x = rect.topLeft.x * a + rect.topLeft.y * c + tx;
                let v0y = rect.topLeft.x * b + rect.topLeft.y * d + ty;
                let v1x = rect.bottomLeft.x * a + rect.bottomLeft.y * c + tx;
                let v1y = rect.bottomLeft.x * b + rect.bottomLeft.y * d + ty;
                let v2x = rect.topRight.x * a + rect.topRight.y * c + tx;
                let v2y = rect.topRight.x * b + rect.topRight.y * d + ty;
                let v3x = rect.bottomRight.x * a + rect.bottomRight.y * c + tx;
                let v3y = rect.bottomRight.x * b + rect.bottomRight.y * d + ty;
                let minx = Math.min(v0x, v1x, v2x, v3x),
                    maxx = Math.max(v0x, v1x, v2x, v3x),
                    miny = Math.min(v0y, v1y, v2y, v3y),
                    maxy = Math.max(v0y, v1y, v2y, v3y);
                
                startCol = Math.floor(minx / maptw);
                startRow = rows - Math.ceil(maxy / mapth);
                maxCol = Math.ceil((maxx + extw) / maptw);
                maxRow = rows - Math.floor((miny - exth) / mapth);
 
                // Adjustment
                if (startCol < 0) startCol = 0;
                if (startRow < 0) startRow = 0;
                if (maxCol > cols) maxCol = cols;
                if (maxRow > rows) maxRow = rows;
            }
        }
 
        let colOffset = startRow * cols, gid, grid,
            top, left, bottom, right, 
            gt, gl, gb, gr,
            axis, tileOffset, diffX1, diffY1, odd_even;
 
        if (layerOrientation === Orientation.HEX) {
            let hexSideLength = comp._hexSideLength;
            axis = comp._staggerAxis;
            tileOffset = comp._tileset.tileOffset;
            odd_even = (comp._staggerIndex === StaggerIndex.STAGGERINDEX_ODD) ? 1 : -1;
            diffX1 = (axis === StaggerAxis.STAGGERAXIS_X) ? ((maptw - hexSideLength)/2) : 0;
            diffY1 = (axis === StaggerAxis.STAGGERAXIS_Y) ? ((mapth - hexSideLength)/2) : 0;
        }
 
        let dataOffset = 0;
        let a2, b2, c2, d2, tx2, ty2, color2;
        for (let row = startRow; row < maxRow; ++row) {
            for (let col = startCol; col < maxCol; ++col) {
                let index = colOffset + col;
                let flippedX = false, flippedY = false;
 
                let tiledTile = tiledTiles[index];
                if (tiledTile) {
                    gid = tiledTile.gid;
                }
                else {
                    gid = comp._tiles[index];
                }
                
                grid = grids[(gid & FLIPPED_MASK) >>> 0];
                if (!grid) {
                    continue;
                }
 
                switch (layerOrientation) {
                    case Orientation.ORTHO:
                        left = col * maptw;
                        bottom = (rows - row - 1) * mapth;
                        break;
                    case Orientation.ISO:
                        left = maptw / 2 * ( cols + col - row - 1);
                        bottom = mapth / 2 * ( rows * 2 - col - row - 2);
                        break;
                    case Orientation.HEX:
                        let diffX2 = (axis === StaggerAxis.STAGGERAXIS_Y && row % 2 === 1) ? (maptw / 2 * odd_even) : 0;
                        left = col * (maptw - diffX1) + diffX2 + tileOffset.x;
                        let diffY2 = (axis === StaggerAxis.STAGGERAXIS_X && col % 2 === 1) ? (mapth/2 * -odd_even) : 0;
                        bottom = (rows - row - 1) * (mapth -diffY1) + diffY2 - tileOffset.y;
                        break;
                }
 
                if (tiledTile) {
                    let tiledNode = tiledTile.node;
 
                    // use tiled tile properties
 
                    // color
                    color2 = color;
                    let newOpacity = (tiledNode.opacity * opacity) / 255;
                    color = tiledNode.color.setA(newOpacity)._val;
 
                    // transform
                    a2 = a; b2 = b; c2 = c; d2 = d; tx2 = tx; ty2 = ty;
                    tiledNode._updateLocalMatrix();
                    mat4.copy(_mat4_temp, tiledNode._matrix);
                    vec3.set(_vec3_temp, -left, -bottom, 0);
                    mat4.translate(_mat4_temp, _mat4_temp, _vec3_temp);
                    mat4.multiply(_mat4_temp, node._worldMatrix, _mat4_temp);
                    a = _mat4_temp.m00; b = _mat4_temp.m01; c = _mat4_temp.m04; d = _mat4_temp.m05;
                    tx = _mat4_temp.m12; ty = _mat4_temp.m13;
                }
 
                right = left + tilew;
                top = bottom + tileh;
 
                // TMX_ORIENTATION_ISO trim
                if (enabledCulling && layerOrientation === Orientation.ISO) {
                    gb = cullingMapy + bottom*cullingD;
                    if (gb > winh+cullingH) {
                        col += Math.floor((gb-winh)*2/cullingH) - 1;
                        continue;
                    }
                    gr = cullingMapx + right*cullingA;
                    if (gr < -cullingW) {
                        col += Math.floor((-gr)*2/cullingW) - 1;
                        continue;
                    }
                    gl = cullingMapx + left*cullingA;
                    gt = cullingMapy + top*cullingD;
                    if (gl > winw || gt < 0) {
                        col = maxCol;
                        continue;
                    }
                }
 
                // Rotation and Flip
                if (gid > TileFlag.DIAGONAL) {
                    flippedX = (gid & TileFlag.HORIZONTAL) >>> 0;
                    flippedY = (gid & TileFlag.VERTICAL) >>> 0;
                }
 
                renderData.vertexCount += 4;
                renderData.indiceCount += 6;
                renderData.dataLength = renderData.vertexCount;
 
                // tl
                data[dataOffset].x = left * a + top * c + tx;
                data[dataOffset].y = left * b + top * d + ty;
                data[dataOffset].u = flippedX ? grid.r : grid.l;
                data[dataOffset].v = flippedY ? grid.b : grid.t;
                data[dataOffset].color = color;
                dataOffset++;
 
                // bl
                data[dataOffset].x = left * a + bottom * c + tx;
                data[dataOffset].y = left * b + bottom * d + ty;
                data[dataOffset].u = flippedX ? grid.r : grid.l;
                data[dataOffset].v = flippedY ? grid.t : grid.b;
                data[dataOffset].color = color;
                dataOffset++;
 
                // tr
                data[dataOffset].x = right * a + top * c + tx;
                data[dataOffset].y = right * b + top * d + ty;
                data[dataOffset].u = flippedX ? grid.l : grid.r;
                data[dataOffset].v = flippedY ? grid.b : grid.t;
                data[dataOffset].color = color;
                dataOffset++;
 
                // br
                data[dataOffset].x = right * a + bottom * c + tx;
                data[dataOffset].y = right * b + bottom * d + ty;
                data[dataOffset].u = flippedX ? grid.l : grid.r;
                data[dataOffset].v = flippedY ? grid.t : grid.b;
                data[dataOffset].color = color;
                dataOffset++;
 
                if (tiledTile) {
                    color = color2;
                    a = a2; b = b2; c = c2; d = d2; tx = tx2; ty = ty2;
                }
            }
            colOffset += cols;
        }
    },
};
 
module.exports = TiledLayer._assembler = tmxAssembler;