Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     isRoof(tile, name) {
  2.         if (!tile.buildings) return false;
  3.         if (!tile.buildings[name]) return false;
  4.         if (tile.buildings[name].roof || tile.buildings[name].walls) return true;
  5.         return false;
  6.     }
  7.  
  8.     roofPosition(tiles,level,x,y) {
  9.         let tile = tiles[x][y];
  10.         if (!tile.buildings) return undefined;
  11.         if (!tile.buildings[level]) return undefined;
  12.         if (!tile.buildings[level].roof) return undefined;
  13.        
  14.         let e = tile.buildings[level].roof.position;
  15.         return e;
  16.     }
  17.  
  18.     // Returns true if the roof is an 'edge' or 'inner' point
  19.     // this impl is actually broken due to the left edge of the map not knowing about the neighbor
  20.     shouldBeElevated(tiles,level,x,y) {
  21.         let left = x > 0 ? x - 1 : x;
  22.         let up = y > 0 ? y - 1 : y;
  23.  
  24.         let neighbors = 0;
  25.  
  26.         let tl = this.roofPosition(tiles,level,left,up);
  27.         if (tl == 'full' || tl == 'br') neighbors++;
  28.  
  29.         let tr = this.roofPosition(tiles,level,x,up);
  30.         if (tr == 'full' || tr == 'bl') neighbors++;
  31.  
  32.         let br = this.roofPosition(tiles,level,x,y);
  33.         if (br == 'full' || br == 'tl') neighbors++;
  34.  
  35.         let bl = this.roofPosition(tiles,level,left,y);
  36.         if (bl == 'full' || bl == 'tr') neighbors++;
  37.  
  38.         return neighbors == 4;
  39.         //return true;
  40.     }
  41.  
  42.     generateRoofs(tiles, name, yOff, W) {
  43.         let materials = [];
  44.         let materialMap = {};
  45.  
  46.         let geometry = new THREE.Geometry();
  47.         let curId = 0;
  48.         for (let x = 0; x <= wSIZE; x++) {
  49.             for (let y = 0; y <= wSIZE; y++) {
  50.                 let tile = tiles[x][y];
  51.                 tile.x = x;
  52.                 tile.y = y;
  53.  
  54.                 if (!tile.buildings) continue;
  55.                 if (!tile.buildings[name]) continue;
  56.                 if (!tile.buildings[name].roof) continue;
  57.  
  58.                 let e0 = false, e1 = false, e2 = false, e3 = false;
  59.                 let ecount = 0;
  60.                 let v0 = new THREE.Vector3(x, tile.elevation + yOff, y);
  61.                 if (this.shouldBeElevated(tiles,name,x,y)) { v0.y += ROOF_HEIGHT; e0 = true; ecount++; }
  62.                 let v1 = new THREE.Vector3(x+1, tile.elevation + yOff, y);
  63.                 if (this.shouldBeElevated(tiles,name,x+1,y)) { v1.y += ROOF_HEIGHT; e1 = true; ecount++; }
  64.                 let v2 = new THREE.Vector3(x+1, tile.elevation + yOff, y+1);
  65.                 if (this.shouldBeElevated(tiles,name,x+1,y+1)) { v2.y += ROOF_HEIGHT; e2 = true; ecount++; }
  66.                 let v3 = new THREE.Vector3(x, tile.elevation + yOff, y+1);
  67.                 if (this.shouldBeElevated(tiles,name,x,y+1)) { v3.y += ROOF_HEIGHT; e3 = true; ecount++; }
  68.  
  69.                 geometry.vertices.push(v0); let A = curId++;
  70.                 geometry.vertices.push(v1); let B = curId++;
  71.                 geometry.vertices.push(v2); let C = curId++;
  72.                 geometry.vertices.push(v3); let D = curId++;
  73.  
  74.                 let tt = this.roofs[tile.buildings[name].roof.type];
  75.                 let topIndex = this.materialIndex(
  76.                     materials, materialMap, 'basic',
  77.                     tt.top);
  78.                 let sideIndex = this.materialIndex(
  79.                     materials, materialMap, 'basic',
  80.                     tt.side);
  81.  
  82.                 //let materialIndex = ecount == 4 ? topIndex : sideIndex;
  83.  
  84.                 // this is almost definitely wrong?
  85.                 let flipFaces = false;
  86.                 if (ecount == 1 && (e1 || e3)) flipFaces = true;
  87.                 if (ecount == 3 && (!e0 || !e2)) flipFaces = true;
  88.  
  89.                 if (tile.buildings[name].roof.position == 'full') {
  90.                     //console.log([ecount, e0, e1, e2, e3]);
  91.                     if (flipFaces) {
  92.                         if (e0 && e1 && e3) {
  93.                             geometry.faces.push(face(A, B, D, topIndex));
  94.                             geometry.faceVertexUvs[0].push([u0, u1, u3]);
  95.                         } else {
  96.                             geometry.faces.push(face(A, B, D, sideIndex));
  97.                             if (ecount == 3) {
  98.                                 geometry.faceVertexUvs[0].push([u0, uHalfLeft, uHalf]);
  99.                             } else if (e3) {
  100.                                 geometry.faceVertexUvs[0].push([u0,u1,u3]);
  101.                             } else if (e1) {
  102.                                 geometry.faceVertexUvs[0].push([u1,u2,u0]);
  103.                             } else {
  104.                                 console.log("Sadness.");
  105.                             }
  106.                         }
  107.  
  108.                         if (e1 && e2 && e3) {
  109.                             geometry.faces.push(face(B, C, D, topIndex));
  110.                             geometry.faceVertexUvs[0].push([u1, u2, u3]);
  111.                         } else {
  112.                             geometry.faces.push(face(B, C, D, sideIndex));
  113.                             if (ecount == 3) {
  114.                                 geometry.faceVertexUvs[0].push([uHalf, u0,uHalfLeft]);
  115.                             } else if (e1) {
  116.                                 geometry.faceVertexUvs[0].push([u1, u2, u3]);
  117.                             } else if (e3) {
  118.                                 geometry.faceVertexUvs[0].push([u2, u3, u0]);
  119.                             } else {
  120.                                 console.log("Sadness.");
  121.                             }
  122.                         }
  123.                     } else {
  124.                         if (e0 && e1 && e2) {
  125.                             geometry.faces.push(face(A, B, C,topIndex));
  126.                             geometry.faceVertexUvs[0].push([u0, u1, u2]);
  127.                         } else {
  128.                             geometry.faces.push(face(A, B, C,sideIndex));
  129.                             if (ecount == 1) {
  130.                                 if (e0) {
  131.                                     geometry.faceVertexUvs[0].push([u2,u1,u0]);  
  132.                                 } else if (e2) {
  133.                                     geometry.faceVertexUvs[0].push([u1,u0,u3]);
  134.                                 } else {
  135.                                     console.log("Sadness");
  136.                                 }
  137.                             } else if (ecount == 2) {
  138.                                 if (e0 && e1) {
  139.                                     geometry.faceVertexUvs[0].push([u3,u2,u1]);
  140.                                 } else if (e1 && e2) {
  141.                                     geometry.faceVertexUvs[0].push([u0,u3,u2]);
  142.                                 } else if (e2 && e3) {
  143.                                     geometry.faceVertexUvs[0].push([u1,u0,u3]);
  144.                                 } else if (e0 && e3) {
  145.                                     geometry.faceVertexUvs[0].push([u2, u1, u0]);
  146.                                 } else {
  147.                                     console.log("Sadness");
  148.                                 }
  149.                             } else if (ecount == 3) {
  150.                                 if (!e1) {
  151.                                     geometry.faceVertexUvs[0].push([uHalfLeft, u0, uHalf]);
  152.                                 } else {
  153.                                     console.log("Sadness");
  154.                                 }
  155.                             }
  156.                         }
  157.  
  158.                         if (e0 && e2 && e3) {
  159.                             geometry.faces.push(face(A, C, D, topIndex));
  160.                             geometry.faceVertexUvs[0].push([u0, u2, u3]);
  161.                         } else {
  162.                             geometry.faces.push(face(A, C, D, sideIndex));
  163.                             if (ecount == 1) {
  164.                                 if (e0) {
  165.                                     geometry.faceVertexUvs[0].push([u3, u1, u0]);
  166.                                 } else if (e2) {
  167.                                     geometry.faceVertexUvs[0].push([u0, u2, u1]);
  168.                                 } else {
  169.                                     console.log("Sadness");
  170.                                 }
  171.                             } else if (ecount == 2) {
  172.                                 if (e0 && e1) {
  173.                                     geometry.faceVertexUvs[0].push([u3,u1,u0]);
  174.                                 } else if (e1 && e2) {
  175.                                     geometry.faceVertexUvs[0].push([u0,u2,u1]);
  176.                                 } else if (e2 && e3) {
  177.                                     geometry.faceVertexUvs[0].push([u1,u3,u2]);
  178.                                 } else if (e0 && e3) {
  179.                                     geometry.faceVertexUvs[0].push([u2, u0, u3]);
  180.                                 } else {
  181.                                     console.log("Sadness");
  182.                                 }
  183.                             } else if (ecount == 3) {
  184.                                 if (!e3) {
  185.                                     geometry.faceVertexUvs[0].push([uHalf,uHalfLeft, u0]);
  186.                                 } else {
  187.                                     console.log("Sadness");
  188.                                 }
  189.                             }
  190.                         }
  191.                     }
  192.                 } else if (tile.buildings[name].roof.position == 'tl') {
  193.                     geometry.faces.push(face(A, B, D, sideIndex));
  194.                     geometry.faceVertexUvs[0].push([uHalf, u1, u0]);
  195.                 } else if (tile.buildings[name].roof.position == 'tr') {
  196.                     geometry.faces.push(face(A, B, C, sideIndex));
  197.                     geometry.faceVertexUvs[0].push([u0, uHalf, u1]);
  198.                 } else if (tile.buildings[name].roof.position == 'bl') {
  199.                     geometry.faces.push(face(A, C, D, sideIndex));
  200.                     geometry.faceVertexUvs[0].push([u1, u0, uHalf]);
  201.                 } else if (tile.buildings[name].roof.position == 'br') {
  202.                     geometry.faces.push(face(B, C, D, sideIndex));
  203.                     geometry.faceVertexUvs[0].push([u0, uHalf, u1]);
  204.                 }
  205.             }
  206.         }
  207.  
  208.         let mesh = new THREE.Mesh(geometry, materials);
  209.         mesh.matrixAutoUpdate = false;
  210.         W.add(mesh);
  211.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement