|
| 1 | +const weightedSearchAlgorithm = require("../pathfindingAlgorithms/weightedSearchAlgorithm"); |
| 2 | +const unweightedSearchAlgorithm = require("../pathfindingAlgorithms/unweightedSearchAlgorithm"); |
| 3 | + |
| 4 | +function launchAnimations(board, success, type, object, algorithm, heuristic) { |
| 5 | + let nodes = object ? board.objectNodesToAnimate.slice(0) : board.nodesToAnimate.slice(0); |
| 6 | + let speed = board.speed === "fast" ? |
| 7 | + 0 : board.speed === "average" ? |
| 8 | + 100 : 500; |
| 9 | + let shortestNodes; |
| 10 | + function timeout(index) { |
| 11 | + setTimeout(function () { |
| 12 | + if (index === nodes.length) { |
| 13 | + if (object) { |
| 14 | + board.objectNodesToAnimate = []; |
| 15 | + if (success) { |
| 16 | + board.addShortestPath(board.object, board.start, "object"); |
| 17 | + board.clearNodeStatuses(); |
| 18 | + let newSuccess; |
| 19 | + if (board.currentAlgorithm === "bidirectional") { |
| 20 | + |
| 21 | + } else { |
| 22 | + if (type === "weighted") { |
| 23 | + newSuccess = weightedSearchAlgorithm(board.nodes, board.object, board.target, board.nodesToAnimate, board.boardArray, algorithm, heuristic); |
| 24 | + } else { |
| 25 | + newSuccess = unweightedSearchAlgorithm(board.nodes, board.object, board.target, board.nodesToAnimate, board.boardArray, algorithm); |
| 26 | + } |
| 27 | + } |
| 28 | + document.getElementById(board.object).className = "visitedObjectNode"; |
| 29 | + launchAnimations(board, newSuccess, type); |
| 30 | + return; |
| 31 | + } else { |
| 32 | + console.log("Failure."); |
| 33 | + board.reset(); |
| 34 | + board.toggleButtons(); |
| 35 | + return; |
| 36 | + } |
| 37 | + } else { |
| 38 | + board.nodesToAnimate = []; |
| 39 | + if (success) { |
| 40 | + if (document.getElementById(board.target).className !== "visitedTargetNodeBlue") { |
| 41 | + document.getElementById(board.target).className = "visitedTargetNodeBlue"; |
| 42 | + } |
| 43 | + if (board.isObject) { |
| 44 | + board.addShortestPath(board.target, board.object); |
| 45 | + board.drawShortestPathTimeout(board.target, board.object, type, "object"); |
| 46 | + board.objectShortestPathNodesToAnimate = []; |
| 47 | + board.shortestPathNodesToAnimate = []; |
| 48 | + board.reset("objectNotTransparent"); |
| 49 | + } else { |
| 50 | + board.drawShortestPathTimeout(board.target, board.start, type); |
| 51 | + board.objectShortestPathNodesToAnimate = []; |
| 52 | + board.shortestPathNodesToAnimate = []; |
| 53 | + board.reset(); |
| 54 | + } |
| 55 | + shortestNodes = board.objectShortestPathNodesToAnimate.concat(board.shortestPathNodesToAnimate); |
| 56 | + return; |
| 57 | + } else { |
| 58 | + console.log("Failure."); |
| 59 | + board.reset(); |
| 60 | + board.toggleButtons(); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + } else if (index === 0) { |
| 65 | + if (object) { |
| 66 | + document.getElementById(board.start).className = "visitedStartNodePurple"; |
| 67 | + } else { |
| 68 | + if (document.getElementById(board.start).className !== "visitedStartNodePurple") { |
| 69 | + document.getElementById(board.start).className = "visitedStartNodeBlue"; |
| 70 | + } |
| 71 | + } |
| 72 | + if (board.currentAlgorithm === "bidirectional") { |
| 73 | + document.getElementById(board.target).className = "visitedTargetNodeBlue"; |
| 74 | + } |
| 75 | + change(nodes[index]) |
| 76 | + } else if (index === nodes.length - 1 && board.currentAlgorithm === "bidirectional") { |
| 77 | + change(nodes[index], nodes[index - 1], "bidirectional"); |
| 78 | + } else { |
| 79 | + change(nodes[index], nodes[index - 1]); |
| 80 | + } |
| 81 | + timeout(index + 1); |
| 82 | + }, speed); |
| 83 | + } |
| 84 | + |
| 85 | + function change(currentNode, previousNode, bidirectional) { |
| 86 | + let currentHTMLNode = document.getElementById(currentNode.id); |
| 87 | + let relevantClassNames = ["start", "target", "object", "visitedStartNodeBlue", "visitedStartNodePurple", "visitedObjectNode", "visitedTargetNodePurple", "visitedTargetNodeBlue"]; |
| 88 | + if (!relevantClassNames.includes(currentHTMLNode.className)) { |
| 89 | + currentHTMLNode.className = !bidirectional ? |
| 90 | + "current" : currentNode.weight === 15 ? |
| 91 | + "visited weight" : "visited"; |
| 92 | + } |
| 93 | + if (currentHTMLNode.className === "visitedStartNodePurple" && !object) { |
| 94 | + currentHTMLNode.className = "visitedStartNodeBlue"; |
| 95 | + } |
| 96 | + if (currentHTMLNode.className === "target" && object) { |
| 97 | + currentHTMLNode.className = "visitedTargetNodePurple"; |
| 98 | + } |
| 99 | + if (previousNode) { |
| 100 | + let previousHTMLNode = document.getElementById(previousNode.id); |
| 101 | + if (!relevantClassNames.includes(previousHTMLNode.className)) { |
| 102 | + if (object) { |
| 103 | + previousHTMLNode.className = previousNode.weight === 15 ? "visitedobject weight" : "visitedobject"; |
| 104 | + } else { |
| 105 | + previousHTMLNode.className = previousNode.weight === 15 ? "visited weight" : "visited"; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + function shortestPathTimeout(index) { |
| 112 | + setTimeout(function () { |
| 113 | + if (index === shortestNodes.length){ |
| 114 | + board.reset(); |
| 115 | + if (object) { |
| 116 | + shortestPathChange(board.nodes[board.target], shortestNodes[index - 1]); |
| 117 | + board.objectShortestPathNodesToAnimate = []; |
| 118 | + board.shortestPathNodesToAnimate = []; |
| 119 | + board.clearNodeStatuses(); |
| 120 | + let newSuccess; |
| 121 | + if (type === "weighted") { |
| 122 | + newSuccess = weightedSearchAlgorithm(board.nodes, board.object, board.target, board.nodesToAnimate, board.boardArray, algorithm); |
| 123 | + } else { |
| 124 | + newSuccess = unweightedSearchAlgorithm(board.nodes, board.object, board.target, board.nodesToAnimate, board.boardArray, algorithm); |
| 125 | + } |
| 126 | + launchAnimations(board, newSuccess, type); |
| 127 | + return; |
| 128 | + } else { |
| 129 | + shortestPathChange(board.nodes[board.target], shortestNodes[index - 1]); |
| 130 | + board.objectShortestPathNodesToAnimate = []; |
| 131 | + board.shortestPathNodesToAnimate = []; |
| 132 | + return; |
| 133 | + } |
| 134 | + } else if (index === 0) { |
| 135 | + shortestPathChange(shortestNodes[index]) |
| 136 | + } else { |
| 137 | + shortestPathChange(shortestNodes[index], shortestNodes[index - 1]); |
| 138 | + } |
| 139 | + shortestPathTimeout(index + 1); |
| 140 | + }, 40); |
| 141 | + } |
| 142 | + |
| 143 | + function shortestPathChange(currentNode, previousNode) { |
| 144 | + let currentHTMLNode = document.getElementById(currentNode.id); |
| 145 | + if (type === "unweighted") { |
| 146 | + currentHTMLNode.className = "shortest-path-unweighted"; |
| 147 | + } else { |
| 148 | + if (currentNode.direction === "up") { |
| 149 | + currentHTMLNode.className = "shortest-path-up"; |
| 150 | + } else if (currentNode.direction === "down") { |
| 151 | + currentHTMLNode.className = "shortest-path-down"; |
| 152 | + } else if (currentNode.direction === "right") { |
| 153 | + currentHTMLNode.className = "shortest-path-right"; |
| 154 | + } else if (currentNode.direction === "left") { |
| 155 | + currentHTMLNode.className = "shortest-path-left"; |
| 156 | + } else if (currentNode.direction = "down-right") { |
| 157 | + currentHTMLNode.className = "wall" |
| 158 | + } |
| 159 | + } |
| 160 | + if (previousNode) { |
| 161 | + let previousHTMLNode = document.getElementById(previousNode.id); |
| 162 | + previousHTMLNode.className = "shortest-path"; |
| 163 | + } else { |
| 164 | + let element = document.getElementById(board.start); |
| 165 | + element.className = "shortest-path"; |
| 166 | + element.removeAttribute("style"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + timeout(0); |
| 171 | + |
| 172 | +}; |
| 173 | + |
| 174 | +module.exports = launchAnimations; |
0 commit comments