maze.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var Maze = function (scene, cells, width, height) {
  2. this.scene = scene;
  3. this.elements = [];
  4. this.width = width;
  5. this.height = height;
  6. this.horizCells = cells;
  7. this.vertCells = cells;
  8. this.generator = new MazeGenerator(this.horizCells, this.vertCells);
  9. this.cellWidth = this.width / this.horizCells;
  10. this.cellHeight = this.height / this.vertCells;
  11. var self = this;
  12. return {
  13. width: function () {
  14. return self.width;
  15. },
  16. height: function () {
  17. return self.height;
  18. },
  19. generate: function () {
  20. self.generator.generate();
  21. },
  22. draw: function () {
  23. this.drawBorders();
  24. this.drawMaze();
  25. },
  26. solve: function () {
  27. self.generator.solve();
  28. this.drawSolution();
  29. },
  30. drawBorders: function () {
  31. this.drawLine(self.cellWidth, 0, self.width, 0);
  32. this.drawLine(self.width, 0, self.width, self.height);
  33. this.drawLine(self.width - self.cellWidth, self.height, 0, self.height);
  34. this.drawLine(0, self.height, 0, 0);
  35. },
  36. drawSolution: function () {
  37. var path = self.generator.path;
  38. for (var i = 0; i < path.length; i++) {
  39. (function () {
  40. var cell = path[i];
  41. var x = cell.x * self.cellWidth;
  42. var y = cell.y * self.cellHeight;
  43. setTimeout(function () {
  44. self.ctx.fillRect(x, y, self.cellWidth, self.cellHeight);
  45. }, 80 * i);
  46. })();
  47. }
  48. },
  49. drawMaze: function () {
  50. var graph = self.generator.graph;
  51. var drawnEdges = [];
  52. var edgeAlreadyDrawn = function (cell1, cell2) {
  53. return _.detect(drawnEdges, function (edge) {
  54. return _.include(edge, cell1) && _.include(edge, cell2);
  55. }) != undefined;
  56. };
  57. for (var i = 0; i < graph.width; i++) {
  58. for (var j = 0; j < graph.height; j++) {
  59. var cell = graph.cells[i][j];
  60. var topCell = graph.getCellAt(cell.x, cell.y - 1);
  61. var leftCell = graph.getCellAt(cell.x - 1, cell.y);
  62. var rightCell = graph.getCellAt(cell.x + 1, cell.y);
  63. var bottomCell = graph.getCellAt(cell.x, cell.y + 1);
  64. if (!edgeAlreadyDrawn(cell, topCell) && graph.areConnected(cell, topCell)) {
  65. var x1 = cell.x * self.cellWidth;
  66. var y1 = cell.y * self.cellHeight;
  67. var x2 = x1 + self.cellWidth;
  68. var y2 = y1;
  69. this.drawLine(x1, y1, x2, y2);
  70. drawnEdges.push([cell, topCell]);
  71. }
  72. if (!edgeAlreadyDrawn(cell, leftCell) && graph.areConnected(cell, leftCell)) {
  73. var x2 = x1;
  74. var y2 = y1 + self.cellHeight;
  75. this.drawLine(x1, y1, x2, y2);
  76. drawnEdges.push([cell, leftCell]);
  77. }
  78. if (!edgeAlreadyDrawn(cell, rightCell) && graph.areConnected(cell, rightCell)) {
  79. var x1 = (cell.x * self.cellWidth) + self.cellWidth;
  80. var y1 = cell.y * self.cellHeight;
  81. var x2 = x1;
  82. var y2 = y1 + self.cellHeight;
  83. this.drawLine(x1, y1, x2, y2);
  84. drawnEdges.push([cell, rightCell]);
  85. }
  86. if (!edgeAlreadyDrawn(cell, bottomCell) && graph.areConnected(cell, bottomCell)) {
  87. var x1 = cell.x * self.cellWidth;
  88. var y1 = (cell.y * self.cellHeight) + self.cellHeight;
  89. var x2 = x1 + self.cellWidth;
  90. var y2 = y1;
  91. this.drawLine(x1, y1, x2, y2);
  92. drawnEdges.push([cell, bottomCell]);
  93. }
  94. }
  95. }
  96. },
  97. getElements: function() {
  98. return self.elements;
  99. },
  100. drawLine: function (x1, y1, x2, y2) {
  101. var lengthX = Math.abs(x1 - x2);
  102. var lengthY = Math.abs(y1 - y2);
  103. // since only 90 degrees angles, so one of these is always 0
  104. // to add a certain thickness to the wall, set to 0.5
  105. if (lengthX === 0) lengthX = 0.5;
  106. if (lengthY === 0) lengthY = 0.5;
  107. // create a cube to represent the wall segment
  108. var wallGeom = new THREE.BoxGeometry(lengthX, 3, lengthY);
  109. var wallMaterial = new THREE.MeshPhongMaterial({color: 0xff0000, opacity: 0.8, transparent: true});
  110. // and create the complete wall segment
  111. var wallMesh = new THREE.Mesh(wallGeom, wallMaterial);
  112. // finally position it correctly
  113. wallMesh.position = new THREE.Vector3(x1 - ((x1 - x2) / 2) -(self.height/2), wallGeom.height/2, y1 - ((y1 - y2)) / 2 - (self.width /2));
  114. self.elements.push(wallMesh);
  115. scene.add(wallMesh);
  116. }
  117. };
  118. };