-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.33d05fb05ef33a9ae3ea.js
More file actions
1 lines (1 loc) · 8.63 KB
/
index.33d05fb05ef33a9ae3ea.js
File metadata and controls
1 lines (1 loc) · 8.63 KB
1
!function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}function r(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function i(t,e){return(i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function o(t){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t,e){return!e||"object"!==o(e)&&"function"!=typeof e?r(t):e}function c(t){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function u(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function s(t,e,n){return(s=u()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&i(o,n.prototype),o}).apply(null,arguments)}function l(t){var e="function"==typeof Map?new Map:void 0;return(l=function(t){if(null===t||(n=t,-1===Function.toString.call(n).indexOf("[native code]")))return t;var n;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return s(t,arguments,c(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),i(r,t)})(t)}function p(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var f=function(){function e(n){if(t(this,e),p(this,"cells",[]),p(this,"options",{grid:{horizontal:30,vertical:15}}),!(n instanceof HTMLCanvasElement))throw new Error("The received element is not a canvas");this.canvas=n,this.context=this.canvas.getContext("2d")}return n(e,[{key:"createGrid",value:function(t,e){this.clearGrid();var n=[this.canvas.width/Math.round(t),this.canvas.height/Math.round(e)],r=n[0],i=n[1];this.options={cell:{width:r,height:i},grid:{horizontal:t,vertical:e}};for(var o=0;o<t;o++)for(var a=0;a<e;a++){var c=r*o,u=i*a;this.cells.push(new h(c,u,this)),this.context.strokeRect(c,u,r,i)}}},{key:"redraw",value:function(){this.cells.forEach((function(t){t.isPaint?t.paint():t.unpaint()}))}},{key:"clearGrid",value:function(){this.cells=[],this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}}]),e}(),h=function(){function e(n,r,i){t(this,e),p(this,"isPaint",!1),this.x=n,this.y=r,this.painter=i}return n(e,[{key:"paint",value:function(){this.isPaint=!0,this.painter.context.fillRect(this.x,this.y,this.painter.options.cell.width,this.painter.options.cell.height)}},{key:"unpaint",value:function(){this.isPaint=!1,this.painter.context.clearRect(this.x,this.y,this.painter.options.cell.width,this.painter.options.cell.height),this.painter.context.strokeRect(this.x,this.y,this.painter.options.cell.width,this.painter.options.cell.height)}},{key:"getNeighbors",value:function(){var t=this;return this.painter.cells.reduce((function(e,n){return n===t||Math.round(n.x-t.x)<=Math.round(t.painter.options.cell.width)&&Math.round(n.x-t.x)>=-Math.round(t.painter.options.cell.width)&&Math.round(n.y-t.y)<=Math.round(t.painter.options.cell.height)&&Math.round(n.y-t.y)>=-Math.round(t.painter.options.cell.height)&&e.push(n),e}),[])}},{key:"getPaintNeighbors",value:function(){return this.getNeighbors().reduce((function(t,e){return e.isPaint&&t.push(e),t}),[])}}]),e}();var d,v,y,g=function(e){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&i(t,e)}(l,e);var o,u,s=(o=l,u=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,e=c(o);if(u){var n=c(this).constructor;t=Reflect.construct(e,arguments,n)}else t=e.apply(this,arguments);return a(this,t)});function l(e){var n;return t(this,l),p(r(n=s.call(this)),"speed",500),p(r(n),"process",null),p(r(n),"onPainterClick",(function(t){n.painter.cells.reduce((function(e,r){var i=[r.x,r.x+n.painter.options.cell.width,r.y,r.y+n.painter.options.cell.height],o=i[0],a=i[1],c=i[2],u=i[3];return t.offsetX>=o&&t.offsetX<=a&&t.offsetY>=c&&t.offsetY<=u&&(e=r),e}),null).paint()})),p(r(n),"run",(function(){n.painter.cells.every((function(t){return!t.isPaint}))||(n.dispatchEvent(new Event("run")),n.process?n.stop():n.process=setInterval(n.generateGeneration,n.speed))})),p(r(n),"clear",(function(){n.stop(),n.drawMap()})),p(r(n),"generateGeneration",(function(){var t=n.painter.cells.reduce((function(t,e){var r=new h(e.x,e.y,n.painter),i=e.getPaintNeighbors().length;return!e.isPaint&&3===i||e.isPaint&&(2===i||3===i)?r.paint():e.isPaint&&(i<2||i>3)&&r.unpaint(),t.push(r),t}),[]);t.every((function(t,e){return t.isPaint===n.painter.cells[e].isPaint}))&&n.stop("Ни одна из клеток не меняет своего состояния"),n.painter.cells=t,n.painter.redraw(),n.painter.cells.every((function(t){return!t.isPaint}))&&n.stop("На поле не осталось ни одной живой клетки")})),n.painter=new f(e),n.drawMap(),n.painter.canvas.addEventListener("click",n.onPainterClick),n}return n(l,[{key:"drawMap",value:function(){this.painter.createGrid(this.painter.options.grid.horizontal,this.painter.options.grid.vertical)}},{key:"stop",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=new Event("stop");e.reason=t,this.dispatchEvent(e),clearInterval(this.process),this.process=null}}]),l}(l(EventTarget)),b=function(){function e(){t(this,e)}return n(e,null,[{key:"getItem",value:function(t){return window.localStorage?localStorage.getItem(t):null}},{key:"setItem",value:function(t,e){window.localStorage&&localStorage.setItem(t,e)}}]),e}(),w=new g(document.querySelector("#game"));document.addEventListener("keydown",(function(t){"Space"===t.code?w.run():"KeyR"===t.code?w.clear():"KeyG"===t.code?x.click():"ArrowUp"===t.code?+m.value&&(m.value=+m.value+100,m.dispatchEvent(new Event("input"))):"ArrowDown"===t.code&&+m.value&&+m.value>100&&(m.value=+m.value-100,m.dispatchEvent(new Event("input")))})),document.querySelector("#start-game").addEventListener("click",w.run),document.querySelector("#refresh-game-button").addEventListener("click",w.clear);var m=document.querySelector("#game-speed");m.value=null!==(d=b.getItem("game-speed"))&&void 0!==d?d:100,m.setAttribute("disabled","disabled"),w.speed=m.value,m.addEventListener("input",(function(){var t=parseInt(m.value);t&&(b.setItem("game-speed",t.toString()),w.speed=t,w.stop(),w.run())}));var E=document.querySelector("#horizontal-count"),P=document.querySelector("#vertical-count");function k(){parseInt(E.value)&&parseInt(P.value)&&(b.setItem("horizontal-count",E.value),b.setItem("vertical-count",P.value),w.painter.options.grid.horizontal=+E.value,w.painter.options.grid.vertical=+P.value,w.drawMap())}E.value=null!==(v=b.getItem("horizontal-count"))&&void 0!==v?v:w.painter.options.grid.horizontal,P.value=null!==(y=b.getItem("vertical-count"))&&void 0!==y?y:w.painter.options.grid.vertical,k(),E.addEventListener("input",k),P.addEventListener("input",k);var x=document.querySelector("#random-cell-generation");x.addEventListener("click",(function(){w.painter.cells.forEach((function(t){Math.random()<.3&&t.paint()}))})),document.querySelector("#fullscreen").addEventListener("click",(function(){w.painter.canvas.requestFullscreen()})),w.painter.canvas.addEventListener("fullscreenchange",(function(){document.fullscreenElement?(w.painter.canvas.width=window.screen.width,w.painter.canvas.height=window.screen.height,alert("Используйте быстрые клавиши для управления")):(w.painter.canvas.width=800,w.painter.canvas.height=400),w.drawMap()})),w.addEventListener("run",(function(){m.removeAttribute("disabled"),E.setAttribute("disabled","disabled"),P.setAttribute("disabled","disabled")})),w.addEventListener("stop",(function(t){t.reason&&alert(t.reason),m.setAttribute("disabled","disabled"),E.removeAttribute("disabled"),P.removeAttribute("disabled")}))}();