Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- isRoof(tile, name) {
- if (!tile.buildings) return false;
- if (!tile.buildings[name]) return false;
- if (tile.buildings[name].roof || tile.buildings[name].walls) return true;
- return false;
- }
- roofPosition(tiles,level,x,y) {
- let tile = tiles[x][y];
- if (!tile.buildings) return undefined;
- if (!tile.buildings[level]) return undefined;
- if (!tile.buildings[level].roof) return undefined;
- let e = tile.buildings[level].roof.position;
- return e;
- }
- // Returns true if the roof is an 'edge' or 'inner' point
- // this impl is actually broken due to the left edge of the map not knowing about the neighbor
- shouldBeElevated(tiles,level,x,y) {
- let left = x > 0 ? x - 1 : x;
- let up = y > 0 ? y - 1 : y;
- let neighbors = 0;
- let tl = this.roofPosition(tiles,level,left,up);
- if (tl == 'full' || tl == 'br') neighbors++;
- let tr = this.roofPosition(tiles,level,x,up);
- if (tr == 'full' || tr == 'bl') neighbors++;
- let br = this.roofPosition(tiles,level,x,y);
- if (br == 'full' || br == 'tl') neighbors++;
- let bl = this.roofPosition(tiles,level,left,y);
- if (bl == 'full' || bl == 'tr') neighbors++;
- return neighbors == 4;
- //return true;
- }
- generateRoofs(tiles, name, yOff, W) {
- let materials = [];
- let materialMap = {};
- let geometry = new THREE.Geometry();
- let curId = 0;
- for (let x = 0; x <= wSIZE; x++) {
- for (let y = 0; y <= wSIZE; y++) {
- let tile = tiles[x][y];
- tile.x = x;
- tile.y = y;
- if (!tile.buildings) continue;
- if (!tile.buildings[name]) continue;
- if (!tile.buildings[name].roof) continue;
- let e0 = false, e1 = false, e2 = false, e3 = false;
- let ecount = 0;
- let v0 = new THREE.Vector3(x, tile.elevation + yOff, y);
- if (this.shouldBeElevated(tiles,name,x,y)) { v0.y += ROOF_HEIGHT; e0 = true; ecount++; }
- let v1 = new THREE.Vector3(x+1, tile.elevation + yOff, y);
- if (this.shouldBeElevated(tiles,name,x+1,y)) { v1.y += ROOF_HEIGHT; e1 = true; ecount++; }
- let v2 = new THREE.Vector3(x+1, tile.elevation + yOff, y+1);
- if (this.shouldBeElevated(tiles,name,x+1,y+1)) { v2.y += ROOF_HEIGHT; e2 = true; ecount++; }
- let v3 = new THREE.Vector3(x, tile.elevation + yOff, y+1);
- if (this.shouldBeElevated(tiles,name,x,y+1)) { v3.y += ROOF_HEIGHT; e3 = true; ecount++; }
- geometry.vertices.push(v0); let A = curId++;
- geometry.vertices.push(v1); let B = curId++;
- geometry.vertices.push(v2); let C = curId++;
- geometry.vertices.push(v3); let D = curId++;
- let tt = this.roofs[tile.buildings[name].roof.type];
- let topIndex = this.materialIndex(
- materials, materialMap, 'basic',
- tt.top);
- let sideIndex = this.materialIndex(
- materials, materialMap, 'basic',
- tt.side);
- //let materialIndex = ecount == 4 ? topIndex : sideIndex;
- // this is almost definitely wrong?
- let flipFaces = false;
- if (ecount == 1 && (e1 || e3)) flipFaces = true;
- if (ecount == 3 && (!e0 || !e2)) flipFaces = true;
- if (tile.buildings[name].roof.position == 'full') {
- //console.log([ecount, e0, e1, e2, e3]);
- if (flipFaces) {
- if (e0 && e1 && e3) {
- geometry.faces.push(face(A, B, D, topIndex));
- geometry.faceVertexUvs[0].push([u0, u1, u3]);
- } else {
- geometry.faces.push(face(A, B, D, sideIndex));
- if (ecount == 3) {
- geometry.faceVertexUvs[0].push([u0, uHalfLeft, uHalf]);
- } else if (e3) {
- geometry.faceVertexUvs[0].push([u0,u1,u3]);
- } else if (e1) {
- geometry.faceVertexUvs[0].push([u1,u2,u0]);
- } else {
- console.log("Sadness.");
- }
- }
- if (e1 && e2 && e3) {
- geometry.faces.push(face(B, C, D, topIndex));
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- } else {
- geometry.faces.push(face(B, C, D, sideIndex));
- if (ecount == 3) {
- geometry.faceVertexUvs[0].push([uHalf, u0,uHalfLeft]);
- } else if (e1) {
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- } else if (e3) {
- geometry.faceVertexUvs[0].push([u2, u3, u0]);
- } else {
- console.log("Sadness.");
- }
- }
- } else {
- if (e0 && e1 && e2) {
- geometry.faces.push(face(A, B, C,topIndex));
- geometry.faceVertexUvs[0].push([u0, u1, u2]);
- } else {
- geometry.faces.push(face(A, B, C,sideIndex));
- if (ecount == 1) {
- if (e0) {
- geometry.faceVertexUvs[0].push([u2,u1,u0]);
- } else if (e2) {
- geometry.faceVertexUvs[0].push([u1,u0,u3]);
- } else {
- console.log("Sadness");
- }
- } else if (ecount == 2) {
- if (e0 && e1) {
- geometry.faceVertexUvs[0].push([u3,u2,u1]);
- } else if (e1 && e2) {
- geometry.faceVertexUvs[0].push([u0,u3,u2]);
- } else if (e2 && e3) {
- geometry.faceVertexUvs[0].push([u1,u0,u3]);
- } else if (e0 && e3) {
- geometry.faceVertexUvs[0].push([u2, u1, u0]);
- } else {
- console.log("Sadness");
- }
- } else if (ecount == 3) {
- if (!e1) {
- geometry.faceVertexUvs[0].push([uHalfLeft, u0, uHalf]);
- } else {
- console.log("Sadness");
- }
- }
- }
- if (e0 && e2 && e3) {
- geometry.faces.push(face(A, C, D, topIndex));
- geometry.faceVertexUvs[0].push([u0, u2, u3]);
- } else {
- geometry.faces.push(face(A, C, D, sideIndex));
- if (ecount == 1) {
- if (e0) {
- geometry.faceVertexUvs[0].push([u3, u1, u0]);
- } else if (e2) {
- geometry.faceVertexUvs[0].push([u0, u2, u1]);
- } else {
- console.log("Sadness");
- }
- } else if (ecount == 2) {
- if (e0 && e1) {
- geometry.faceVertexUvs[0].push([u3,u1,u0]);
- } else if (e1 && e2) {
- geometry.faceVertexUvs[0].push([u0,u2,u1]);
- } else if (e2 && e3) {
- geometry.faceVertexUvs[0].push([u1,u3,u2]);
- } else if (e0 && e3) {
- geometry.faceVertexUvs[0].push([u2, u0, u3]);
- } else {
- console.log("Sadness");
- }
- } else if (ecount == 3) {
- if (!e3) {
- geometry.faceVertexUvs[0].push([uHalf,uHalfLeft, u0]);
- } else {
- console.log("Sadness");
- }
- }
- }
- }
- } else if (tile.buildings[name].roof.position == 'tl') {
- geometry.faces.push(face(A, B, D, sideIndex));
- geometry.faceVertexUvs[0].push([uHalf, u1, u0]);
- } else if (tile.buildings[name].roof.position == 'tr') {
- geometry.faces.push(face(A, B, C, sideIndex));
- geometry.faceVertexUvs[0].push([u0, uHalf, u1]);
- } else if (tile.buildings[name].roof.position == 'bl') {
- geometry.faces.push(face(A, C, D, sideIndex));
- geometry.faceVertexUvs[0].push([u1, u0, uHalf]);
- } else if (tile.buildings[name].roof.position == 'br') {
- geometry.faces.push(face(B, C, D, sideIndex));
- geometry.faceVertexUvs[0].push([u0, uHalf, u1]);
- }
- }
- }
- let mesh = new THREE.Mesh(geometry, materials);
- mesh.matrixAutoUpdate = false;
- W.add(mesh);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement