-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockrain.jquery.min.js
More file actions
1 lines (1 loc) · 32.2 KB
/
blockrain.jquery.min.js
File metadata and controls
1 lines (1 loc) · 32.2 KB
1
!function(t){"function"==typeof define&&define.amd?define(["jquery"],t):t(jQuery)}(function(t){var e=0,i=Array.prototype.slice;t.cleanData=function(e){return function(i){var o,n,s;for(s=0;null!=(n=i[s]);s++)try{(o=t._data(n,"events"))&&o.remove&&t(n).triggerHandler("remove")}catch(t){}e(i)}}(t.cleanData),t.widget=function(e,i,o){var n,s,r,a,h={},l=e.split(".")[0];return e=e.split(".")[1],n=l+"-"+e,o||(o=i,i=t.Widget),t.expr[":"][n.toLowerCase()]=function(e){return!!t.data(e,n)},t[l]=t[l]||{},s=t[l][e],r=t[l][e]=function(t,e){return this._createWidget?void(arguments.length&&this._createWidget(t,e)):new r(t,e)},t.extend(r,s,{version:o.version,_proto:t.extend({},o),_childConstructors:[]}),(a=new i).options=t.widget.extend({},a.options),t.each(o,function(e,o){return t.isFunction(o)?void(h[e]=function(){var t=function(){return i.prototype[e].apply(this,arguments)},n=function(t){return i.prototype[e].apply(this,t)};return function(){var e,i=this._super,s=this._superApply;return this._super=t,this._superApply=n,e=o.apply(this,arguments),this._super=i,this._superApply=s,e}}()):void(h[e]=o)}),r.prototype=t.widget.extend(a,{widgetEventPrefix:s&&a.widgetEventPrefix||e},h,{constructor:r,namespace:l,widgetName:e,widgetFullName:n}),s?(t.each(s._childConstructors,function(e,i){var o=i.prototype;t.widget(o.namespace+"."+o.widgetName,r,i._proto)}),delete s._childConstructors):i._childConstructors.push(r),t.widget.bridge(e,r),r},t.widget.extend=function(e){for(var o,n,s=i.call(arguments,1),r=0,a=s.length;a>r;r++)for(o in s[r])n=s[r][o],s[r].hasOwnProperty(o)&&void 0!==n&&(e[o]=t.isPlainObject(n)?t.isPlainObject(e[o])?t.widget.extend({},e[o],n):t.widget.extend({},n):n);return e},t.widget.bridge=function(e,o){var n=o.prototype.widgetFullName||e;t.fn[e]=function(s){var r="string"==typeof s,a=i.call(arguments,1),h=this;return s=!r&&a.length?t.widget.extend.apply(null,[s].concat(a)):s,r?this.each(function(){var i,o=t.data(this,n);return"instance"===s?(h=o,!1):o?t.isFunction(o[s])&&"_"!==s.charAt(0)?(i=o[s].apply(o,a))!==o&&void 0!==i?(h=i&&i.jquery?h.pushStack(i.get()):i,!1):void 0:t.error("no such method '"+s+"' for "+e+" widget instance"):t.error("cannot call methods on "+e+" prior to initialization; attempted to call method '"+s+"'")}):this.each(function(){var e=t.data(this,n);e?(e.option(s||{}),e._init&&e._init()):t.data(this,n,new o(s,this))}),h}},t.Widget=function(){},t.Widget._childConstructors=[],t.Widget.prototype={widgetName:"widget",widgetEventPrefix:"",defaultElement:"<div>",options:{disabled:!1,create:null},_createWidget:function(i,o){o=t(o||this.defaultElement||this)[0],this.element=t(o),this.uuid=e++,this.eventNamespace="."+this.widgetName+this.uuid,this.bindings=t(),this.hoverable=t(),this.focusable=t(),o!==this&&(t.data(o,this.widgetFullName,this),this._on(!0,this.element,{remove:function(t){t.target===o&&this.destroy()}}),this.document=t(o.style?o.ownerDocument:o.document||o),this.window=t(this.document[0].defaultView||this.document[0].parentWindow)),this.options=t.widget.extend({},this.options,this._getCreateOptions(),i),this._create(),this._trigger("create",null,this._getCreateEventData()),this._init()},_getCreateOptions:t.noop,_getCreateEventData:t.noop,_create:t.noop,_init:t.noop,destroy:function(){this._destroy(),this.element.unbind(this.eventNamespace).removeData(this.widgetFullName).removeData(t.camelCase(this.widgetFullName)),this.widget().unbind(this.eventNamespace).removeAttr("aria-disabled").removeClass(this.widgetFullName+"-disabled ui-state-disabled"),this.bindings.unbind(this.eventNamespace),this.hoverable.removeClass("ui-state-hover"),this.focusable.removeClass("ui-state-focus")},_destroy:t.noop,widget:function(){return this.element},option:function(e,i){var o,n,s,r=e;if(0===arguments.length)return t.widget.extend({},this.options);if("string"==typeof e)if(r={},o=e.split("."),e=o.shift(),o.length){for(n=r[e]=t.widget.extend({},this.options[e]),s=0;o.length-1>s;s++)n[o[s]]=n[o[s]]||{},n=n[o[s]];if(e=o.pop(),1===arguments.length)return void 0===n[e]?null:n[e];n[e]=i}else{if(1===arguments.length)return void 0===this.options[e]?null:this.options[e];r[e]=i}return this._setOptions(r),this},_setOptions:function(t){var e;for(e in t)this._setOption(e,t[e]);return this},_setOption:function(t,e){return this.options[t]=e,"disabled"===t&&(this.widget().toggleClass(this.widgetFullName+"-disabled",!!e),e&&(this.hoverable.removeClass("ui-state-hover"),this.focusable.removeClass("ui-state-focus"))),this},enable:function(){return this._setOptions({disabled:!1})},disable:function(){return this._setOptions({disabled:!0})},_on:function(e,i,o){var n,s=this;"boolean"!=typeof e&&(o=i,i=e,e=!1),o?(i=n=t(i),this.bindings=this.bindings.add(i)):(o=i,i=this.element,n=this.widget()),t.each(o,function(o,r){function a(){return e||!0!==s.options.disabled&&!t(this).hasClass("ui-state-disabled")?("string"==typeof r?s[r]:r).apply(s,arguments):void 0}"string"!=typeof r&&(a.guid=r.guid=r.guid||a.guid||t.guid++);var h=o.match(/^([\w:-]*)\s*(.*)$/),l=h[1]+s.eventNamespace,c=h[2];c?n.delegate(c,l,a):i.bind(l,a)})},_off:function(e,i){i=(i||"").split(" ").join(this.eventNamespace+" ")+this.eventNamespace,e.unbind(i).undelegate(i),this.bindings=t(this.bindings.not(e).get()),this.focusable=t(this.focusable.not(e).get()),this.hoverable=t(this.hoverable.not(e).get())},_delay:function(t,e){var i=this;return setTimeout(function(){return("string"==typeof t?i[t]:t).apply(i,arguments)},e||0)},_hoverable:function(e){this.hoverable=this.hoverable.add(e),this._on(e,{mouseenter:function(e){t(e.currentTarget).addClass("ui-state-hover")},mouseleave:function(e){t(e.currentTarget).removeClass("ui-state-hover")}})},_focusable:function(e){this.focusable=this.focusable.add(e),this._on(e,{focusin:function(e){t(e.currentTarget).addClass("ui-state-focus")},focusout:function(e){t(e.currentTarget).removeClass("ui-state-focus")}})},_trigger:function(e,i,o){var n,s,r=this.options[e];if(o=o||{},(i=t.Event(i)).type=(e===this.widgetEventPrefix?e:this.widgetEventPrefix+e).toLowerCase(),i.target=this.element[0],s=i.originalEvent)for(n in s)n in i||(i[n]=s[n]);return this.element.trigger(i,o),!(t.isFunction(r)&&!1===r.apply(this.element[0],[i].concat(o))||i.isDefaultPrevented())}},t.each({show:"fadeIn",hide:"fadeOut"},function(e,i){t.Widget.prototype["_"+e]=function(o,n,s){"string"==typeof n&&(n={effect:n});var r,a=n?!0===n||"number"==typeof n?i:n.effect||i:e;"number"==typeof(n=n||{})&&(n={duration:n}),r=!t.isEmptyObject(n),n.complete=s,n.delay&&o.delay(n.delay),r&&t.effects&&t.effects.effect[a]?o[e](n):a!==e&&o[a]?o[a](n.duration,n.easing,s):o.queue(function(i){t(this)[e](),s&&s.call(o[0]),i()})}}),t.widget}),function(t){"use strict";t.widget("aerolab.blockrain",{options:{autoplay:!1,autoplayRestart:!0,showFieldOnStart:!0,theme:null,blockWidth:10,autoBlockWidth:!1,autoBlockSize:24,difficulty:"normal",speed:20,asdwKeys:!0,playText:"Let's play KUBERTETRIS!",playButtonText:"kubectl run",gameOverText:"Game Over",restartButtonText:"Ctrl+Alt+Delete",scoreText:"Kubelets",onStart:function(){},onRestart:function(){},onGameOver:function(t){},onPlaced:function(){},onLine:function(t,e,i){}},start:function(){this._doStart(),this.options.onStart.call(this.element)},restart:function(){this._doStart(),this.options.onRestart.call(this.element)},gameover:function(){this.showGameOverMessage(),this._board.gameover=!0,this.options.onGameOver.call(this.element,this._filled.score)},_doStart:function(){this._filled.clearAll(),this._filled._resetScore(),this._board.cur=this._board.nextShape(),this._board.started=!0,this._board.gameover=!1,this._board.dropDelay=5,this._board.render(!0),this._board.animate(),this._$start.fadeOut(150),this._$gameover.fadeOut(150),this._$score.fadeIn(150)},pause:function(){this._board.paused=!0},resume:function(){this._board.paused=!1},autoplay:function(t){"boolean"!=typeof t&&(t=!0),this.options.autoplay=t,t&&!this._board.started&&this._doStart(),this._setupControls(!t),this._setupTouchControls(!t)},controls:function(t){"boolean"!=typeof t&&(t=!0),this._setupControls(t)},touchControls:function(t){"boolean"!=typeof t&&(t=!0),this._setupTouchControls(t)},score:function(t){return void 0!==t&&parseInt(t)>=0&&(this._filled.score=parseInt(t),this._$scoreText.text(this._filled_score)),this._filled.score},freesquares:function(){return this._filled.getFreeSpaces()},showStartMessage:function(){this._$start.show()},showGameOverMessage:function(){this._$gameover.show()},updateSizes:function(){this._PIXEL_WIDTH=this.element.innerWidth(),this._PIXEL_HEIGHT=this.element.innerHeight(),this._BLOCK_WIDTH=this.options.blockWidth,this._BLOCK_HEIGHT=Math.floor(this.element.innerHeight()/this.element.innerWidth()*this._BLOCK_WIDTH),this._block_size=Math.floor(this._PIXEL_WIDTH/this._BLOCK_WIDTH),this._border_width=2,this._PIXEL_WIDTH=this._block_size*this._BLOCK_WIDTH,this._PIXEL_HEIGHT=this._block_size*this._BLOCK_HEIGHT,this._$canvas.attr("width",this._PIXEL_WIDTH).attr("height",this._PIXEL_HEIGHT)},theme:function(e){if(void 0===e)return this.options.theme||this._theme;"string"==typeof e?(this.options.theme=e,this._theme=t.extend(!0,{},BlockrainThemes[e])):(this.options.theme=null,this._theme=e),void 0!==this._theme&&null!==this._theme||(this._theme=t.extend(!0,{},BlockrainThemes.retro),this.options.theme="retro"),(isNaN(parseInt(this._theme.strokeWidth))||"number"!=typeof parseInt(this._theme.strokeWidth))&&(this._theme.strokeWidth=2),this._preloadThemeAssets(),null!==this._board&&("string"==typeof this._theme.background&&this._$canvas.css("background-color",this._theme.background),this._board.render())},_theme:{},_$game:null,_$canvas:null,_$gameholder:null,_$start:null,_$gameover:null,_$score:null,_$scoreText:null,_canvas:null,_ctx:null,_create:function(){var e=this;this.theme(this.options.theme),this._createHolder(),this._createUI(),this._refreshBlockSizes(),this.updateSizes(),t(window).resize(function(){}),this._SetupShapeFactory(),this._SetupFilled(),this._SetupInfo(),this._SetupBoard(),this._info.init(),this._board.init();var i=function(){requestAnimationFrame(i),e._board.render()};i(),this.options.autoplay?(this.autoplay(!0),this._setupTouchControls(!1)):(this._setupControls(!0),this._setupTouchControls(!1))},_checkCollisions:function(t,e,i,o){for(var n,s,r=0,a=i.length;r<a;r+=2){if(n=t+i[r],(s=e+i[r+1])>=this._BLOCK_HEIGHT||this._filled.check(n,s))return!0;if(!o&&n<0||n>=this._BLOCK_WIDTH)return!0}return!1},_board:null,_info:null,_filled:null,_drawBackground:function(){if("string"==typeof this._theme.background){if(this._theme.backgroundGrid instanceof Image){if(0===this._theme.backgroundGrid.width||0===this._theme.backgroundGrid.height)return;this._ctx.globalAlpha=1;for(var t=0;t<this._BLOCK_WIDTH;t++)for(var e=0;e<this._BLOCK_HEIGHT;e++){var i=t*this._block_size,o=e*this._block_size;this._ctx.drawImage(this._theme.backgroundGrid,0,0,this._theme.backgroundGrid.width,this._theme.backgroundGrid.height,i,o,this._block_size,this._block_size)}}else if("string"==typeof this._theme.backgroundGrid){var n=this._theme.strokeWidth;Math.round(.23*this._block_size),Math.round(.3*this._block_size);this._ctx.globalAlpha=1,this._ctx.fillStyle=this._theme.backgroundGrid;for(t=0;t<this._BLOCK_WIDTH;t++)for(e=0;e<this._BLOCK_HEIGHT;e++){i=t*this._block_size,o=e*this._block_size;this._ctx.fillRect(i+n,o+n,this._block_size-2*n,this._block_size-2*n)}}this._ctx.globalAlpha=1}},_shapeFactory:null,_shapes:{line:[[0,-1,0,-2,0,-3,0,-4],[2,-2,1,-2,0,-2,-1,-2],[0,-4,0,-3,0,-2,0,-1],[-1,-2,0,-2,1,-2,2,-2]],square:[[0,0,1,0,0,-1,1,-1],[1,0,1,-1,0,0,0,-1],[1,-1,0,-1,1,0,0,0],[0,-1,0,0,1,-1,1,0]],arrow:[[0,-1,1,-1,2,-1,1,-2],[1,0,1,-1,1,-2,0,-1],[2,-1,1,-1,0,-1,1,0],[1,-2,1,-1,1,0,2,-1]],rightHook:[[2,0,1,0,1,-1,1,-2],[2,-2,2,-1,1,-1,0,-1],[0,-2,1,-2,1,-1,1,0],[0,0,0,-1,1,-1,2,-1]],leftHook:[[0,0,1,0,1,-1,1,-2],[2,0,2,-1,1,-1,0,-1],[2,-2,1,-2,1,-1,1,0],[0,-2,0,-1,1,-1,2,-1]],leftZag:[[0,0,0,-1,1,-1,1,-2],[2,-1,1,-1,1,-2,0,-2],[1,-2,1,-1,0,-1,0,0],[0,-2,1,-2,1,-1,2,-1]],rightZag:[[1,0,1,-1,0,-1,0,-2],[2,-1,1,-1,1,0,0,0],[0,-2,0,-1,1,-1,1,0],[0,0,1,0,1,-1,2,-1]]},_SetupShapeFactory:function(){var e=this;function i(e,i,o,n){return t.extend(this,{x:0,y:0,symmetrical:o,init:function(){return t.extend(this,{orientation:0,x:Math.floor(e._BLOCK_WIDTH/2)-1,y:-1}),this},blockType:n,blockVariation:null,blocksLen:i[0].length,orientations:i,orientation:0,rotate:function(t){var i=(this.orientation+("left"===t?1:-1)+4)%4;if(e._checkCollisions(this.x,this.y,this.getBlocks(i))){var o=this.orientation,n=this.x,s=this.y;for(this.orientation=i;this.x>=e._BLOCK_WIDTH-2;)this.x--;for(;this.x<0;)this.x++;"line"===this.blockType&&0===this.x&&this.x++,e._checkCollisions(this.x,this.y,this.getBlocks(i))&&(this.y--,e._checkCollisions(this.x,this.y,this.getBlocks(i))&&(this.x=n,this.y=s,this.orientation=o)),e._board.renderChanged=!0}else this.orientation=i,e._board.renderChanged=!0},moveRight:function(){e._checkCollisions(this.x+1,this.y,this.getBlocks())||(this.x++,e._board.renderChanged=!0)},moveLeft:function(){e._checkCollisions(this.x-1,this.y,this.getBlocks())||(this.x--,e._board.renderChanged=!0)},drop:function(){e._checkCollisions(this.x,this.y+1,this.getBlocks())||(this.y++,e._board.dropCount=-1,e._board.animate(),e._board.renderChanged=!0)},getBlocks:function(t){return this.orientations[void 0!==t?t:this.orientation]},draw:function(t,i,o){for(var n=this.getBlocks(o),s=void 0===t?this.x:t,r=void 0===i?this.y:i,a=0,h=0;a<this.blocksLen;a+=2)e._board.drawBlock(s+n[a],r+n[a+1],this.blockType,this.blockVariation,h,this.orientation,!0),h++},getBounds:function(e){for(var i=t.isArray(e)?e:this.getBlocks(e),o=0,n=i.length,s=999,r=-999,a=999,h=-999;o<n;o+=2)i[o]<s&&(s=i[o]),i[o]>r&&(r=i[o]),i[o+1]<a&&(a=i[o+1]),i[o+1]>h&&(h=i[o+1]);return{left:s,right:r,top:a,bottom:h,width:r-s,height:h-a}}}),this.init()}null===this._shapeFactory&&(this._shapeFactory={line:function(){return new i(e,e._shapes.line,!1,"line")},square:function(){return new i(e,e._shapes.square,!1,"square")},arrow:function(){return new i(e,e._shapes.arrow,!1,"arrow")},leftHook:function(){return new i(e,e._shapes.leftHook,!1,"leftHook")},rightHook:function(){return new i(e,e._shapes.rightHook,!1,"rightHook")},leftZag:function(){return new i(e,e._shapes.leftZag,!1,"leftZag")},rightZag:function(){return new i(e,e._shapes.rightZag,!1,"rightZag")}})},_SetupFilled:function(){var t=this;null===this._filled&&(this._filled={data:new Array(t._BLOCK_WIDTH*t._BLOCK_HEIGHT),score:0,toClear:{},check:function(t,e){return this.data[this.asIndex(t,e)]},add:function(e,i,o,n,s,r){e>=0&&e<t._BLOCK_WIDTH&&i>=0&&i<t._BLOCK_HEIGHT&&(this.data[this.asIndex(e,i)]={blockType:o,blockVariation:n,blockIndex:s,blockOrientation:r})},getFreeSpaces:function(){for(var t=0;t<this.data.length;t++)this.data[t]?1:0},asIndex:function(e,i){return e+i*t._BLOCK_WIDTH},asX:function(e){return e%t._BLOCK_WIDTH},asY:function(e){return Math.floor(e/t._BLOCK_WIDTH)},clearAll:function(){delete this.data,this.data=new Array(t._BLOCK_WIDTH*t._BLOCK_HEIGHT)},_popRow:function(e){for(var i=t._BLOCK_WIDTH*(e+1)-1;i>=0;i--)this.data[i]=i>=t._BLOCK_WIDTH?this.data[i-t._BLOCK_WIDTH]:void 0},checkForClears:function(){var e,i,o,n,s=t._board.lines,r=[];for(e=0,i=this.data.length;e<i;e++)0==(n=this.asX(e))&&(o=0),this.data[e]&&void 0!==this.data[e]&&"string"==typeof this.data[e].blockType&&(o+=1),n==t._BLOCK_WIDTH-1&&o==t._BLOCK_WIDTH&&r.push(this.asY(e));for(e=0,i=r.length;e<i;e++)this._popRow(r[e]),t._board.lines++,t._board.lines%10==0&&t._board.dropDelay>1&&(t._board.dropDelay*=.9);var a=t._board.lines-s;this._updateScore(a)},_updateScore:function(e){if(!(e<=0)){var i=[0,400,1e3,3e3,12e3];e>=i.length&&(e=i.length-1),this.score+=i[e],t._$scoreText.text(this.score),t.options.onLine.call(t.element,e,i[e],this.score)}},_resetScore:function(){this.score=0,t._$scoreText.text(this.score)},draw:function(){for(var e,i=0,o=this.data.length;i<o;i++)if(void 0!==this.data[i]){e=this.asY(i);var n=this.data[i];t._board.drawBlock(this.asX(i),e,n.blockType,n.blockVariation,n.blockIndex,n.blockOrientation)}}})},_SetupInfo:function(){var t=this;this._info={mode:t.options.difficulty,modes:["normal","nice","evil"],modesY:170,autopilotY:null,init:function(){this.mode=t.options.difficulty},setMode:function(e){this.mode=e,t._board.nextShape(!0)}}},_SetupBoard:function(){var e=this,i=this._info;this._board={animateDelay:1e3/e.options.speed,animateTimeoutId:null,cur:null,lines:0,dropCount:0,dropDelay:5,holding:{left:null,right:null,drop:null},holdingThreshold:200,started:!1,gameover:!1,renderChanged:!0,init:function(){this.cur=this.nextShape(),e.options.showFieldOnStart&&(e._drawBackground(),e._board.createRandomBoard(),e._board.render()),this.showStartMessage()},showStartMessage:function(){e._$start.show()},showGameOverMessage:function(){e._$gameover.show()},nextShape:function(o){var n,s,r,a=this.next;if(n="nice"==i.mode||"evil"==i.mode?e._niceShapes:e._randomShapes(),e.options.no_preview){if(this.next=null,o)return null;if(!(s=n(e._filled,e._checkCollisions,e._BLOCK_WIDTH,e._BLOCK_HEIGHT,i.mode)))throw new Error("No shape returned from shape function!",n);s.init(),r=s}else{if(!(s=n(e._filled,e._checkCollisions,e._BLOCK_WIDTH,e._BLOCK_HEIGHT,i.mode)))throw new Error("No shape returned from shape function!",n);if(s.init(),this.next=s,o)return null;r=a||this.nextShape()}return e.options.autoplay&&(e._niceShapes(e._filled,e._checkCollisions,e._BLOCK_WIDTH,e._BLOCK_HEIGHT,"normal",r),r.orientation=r.best_orientation,r.x=r.best_x),void 0!==e._theme.complexBlocks?t.isArray(e._theme.complexBlocks[r.blockType])?r.blockVariation=e._randInt(0,e._theme.complexBlocks[r.blockType].length-1):r.blockVariation=null:void 0!==e._theme.blocks&&(t.isArray(e._theme.blocks[r.blockType])?r.blockVariation=e._randInt(0,e._theme.blocks[r.blockType].length-1):r.blockVariation=null),r},animate:function(){var t=!1,i=!1,o=!1,n=Date.now();if(this.animateTimeoutId&&clearTimeout(this.animateTimeoutId),!this.paused&&!this.gameover&&(this.dropCount++,(this.dropCount>=this.dropDelay||e.options.autoplay||this.holding.drop&&n-this.holding.drop>=this.holdingThreshold)&&(t=!0,i=!0,this.dropCount=0),this.holding.left&&n-this.holding.left>=this.holdingThreshold&&(i=!0,this.cur.moveLeft()),this.holding.right&&n-this.holding.right>=this.holdingThreshold&&(i=!0,this.cur.moveRight()),t)){var s=this.cur,r=s.x,a=s.y,h=s.getBlocks();if(e._checkCollisions(r,a+1,h,!0)){t=!1;for(var l=0,c=0;c<s.blocksLen;c+=2)e._filled.add(r+h[c],a+h[c+1],s.blockType,s.blockVariation,l,s.orientation),a+h[c]<0&&(o=!0),l++;e._filled.checkForClears(),this.cur=this.nextShape(),this.renderChanged=!0,this.holding.left=null,this.holding.right=null,this.holding.drop=null,e.options.onPlaced.call(e.element)}}t&&(i=!0,this.cur.y++),(t||i)&&(this.renderChanged=!0),o?(this.gameover=!0,e.gameover(),e.options.autoplay&&e.options.autoplayRestart&&e.restart(),this.renderChanged=!0):(this.animateDelay=1e3/e.options.speed,this.animateTimeoutId=window.setTimeout(function(){e._board.animate()},this.animateDelay))},createRandomBoard:function(){var t,i,o,n,s,r;for(t=Object.keys(e._shapeFactory),i=0,o=e._BLOCK_WIDTH;i<o;i++)for(n=0,s=e._randChoice([e._randInt(0,8),e._randInt(5,9)]);n<s;n++)r&&e._randInt(0,3)||(r=e._randChoice(t)),e._filled.add(i,e._BLOCK_HEIGHT-n,r,e._randInt(0,3),null,e._randInt(0,3));e._board.render(!0)},render:function(t){(this.renderChanged||t)&&(this.renderChanged=!1,e._ctx.clearRect(0,0,e._PIXEL_WIDTH,e._PIXEL_HEIGHT),e._drawBackground(),e._filled.draw(),this.cur.draw())},drawBlock:function(t,i,o,n,s,r,a){t*=e._block_size,i*=e._block_size,a="boolean"==typeof a&&a;var h=e._theme.strokeWidth,l=Math.round(.23*e._block_size),c=Math.round(.3*e._block_size),d=this.getBlockColor(o,n,s,a);if(e._ctx.globalAlpha=1,d instanceof Image){if(e._ctx.globalAlpha=1,0===d.width||0===d.height)return;if(void 0!==e._theme.blocks&&null!==e._theme.blocks)e._ctx.drawImage(d,0,0,d.width,d.height,t,i,e._block_size,e._block_size);else if(void 0!==e._theme.complexBlocks&&null!==e._theme.complexBlocks){null==s&&(s=0);var u=function(t,i,o){var n=e._shapes[i][0],s=Math.min(n[0],n[2],n[4],n[6]),r=Math.max(n[0],n[2],n[4],n[6]),a=Math.min(n[1],n[3],n[5],n[7]),h=r-s+1,l=Math.max(n[1],n[3],n[5],n[7])-a+1,c=t.width/h,d=t.height/l;return{x:c*(n[2*o]-s),y:d*Math.abs(a-n[2*o+1]),w:c,h:d}}(d,o,s);e._ctx.save(),e._ctx.translate(t,i),e._ctx.translate(e._block_size/2,e._block_size/2),e._ctx.rotate(-Math.PI/2*r),e._ctx.drawImage(d,u.x,u.y,u.w,u.h,-e._block_size/2,-e._block_size/2,e._block_size,e._block_size),e._ctx.restore()}else e._ctx.fillStyle="#ff0000",e._ctx.fillRect(t,i,e._block_size,e._block_size)}else"string"==typeof d&&(e._ctx.fillStyle=d,e._ctx.fillRect(t,i,e._block_size,e._block_size),"string"==typeof e._theme.innerShadow&&(e._ctx.globalAlpha=1,e._ctx.strokeStyle=e._theme.innerShadow,e._ctx.lineWidth=1,e._ctx.strokeRect(t+1,i+1,e._block_size-2,e._block_size-2)),"string"==typeof e._theme.stroke&&(e._ctx.globalAlpha=1,e._ctx.fillStyle=e._theme.stroke,e._ctx.strokeStyle=e._theme.stroke,e._ctx.lineWidth=h,e._ctx.strokeRect(t,i,e._block_size,e._block_size)),"string"==typeof e._theme.innerStroke&&(e._ctx.fillStyle=e._theme.innerStroke,e._ctx.fillRect(t+l,i+l,e._block_size-2*l,h),e._ctx.fillRect(t+l,i+l+h,h,e._block_size-2*l-h)),"string"==typeof e._theme.innerSquare&&(e._ctx.fillStyle=e._theme.innerSquare,e._ctx.globalAlpha=.2,e._ctx.fillRect(t+c,i+c,e._block_size-2*c,e._block_size-2*c)));e._ctx.globalAlpha=1},getBlockColor:function(i,o,n,s){var r=function(e,i){return t.isArray(e)?null!==i&&void 0!==e[i]?e[i]:e.length>0?e[0]:null:e};return"boolean"!=typeof s&&(s=!0),s?"string"==typeof e._theme.primary&&""!==e._theme.primary?e._theme.primary:void 0!==e._theme.blocks&&null!==e._theme.blocks?r(e._theme.blocks[i],o):r(e._theme.complexBlocks[i],o):"string"==typeof e._theme.secondary&&""!==e._theme.secondary?e._theme.secondary:void 0!==e._theme.blocks&&null!==e._theme.blocks?r(e._theme.blocks[i],o):r(e._theme.complexBlocks[i],o)}},e._niceShapes=e._getNiceShapes()},_randInt:function(t,e){return t+Math.floor(Math.random()*(1+e-t))},_randSign:function(){return 2*this._randInt(0,1)-1},_randChoice:function(t){return t[this._randInt(0,t.length-1)]},_preloadThemeAssets:function(){var e=this,i=new RegExp("^#[A-F0-9+]{3,6}","i"),o=(new RegExp("^data:image/(png|gif|jpg);base64,","i"),function(){e._board&&e._board.render(!0)}),n=function(t){var e=t;return i.test(e)?t=e:((t=new Image).src=e,t.onload=o),t},s=function(e){if(t.isArray(e)&&e.length>0)for(var i=0;i<e.length;i++)e[i]=n(e[i]);else"string"==typeof e&&(e=n(e));return e};if(void 0!==this._theme.complexBlocks)for(var r=Object.keys(this._theme.complexBlocks),a=0;a<r.length;a++)this._theme.complexBlocks[r[a]]=s(this._theme.complexBlocks[r[a]]);else if(void 0!==this._theme.blocks)for(r=Object.keys(this._theme.blocks),a=0;a<r.length;a++)this._theme.blocks[r[a]]=s(this._theme.blocks[r[a]]);if(void 0!==this._theme.backgroundGrid&&"string"==typeof this._theme.backgroundGrid&&!i.test(this._theme.backgroundGrid)){var h=this._theme.backgroundGrid;this._theme.backgroundGrid=new Image,this._theme.backgroundGrid.src=h,this._theme.backgroundGrid.onload=o}},_createHolder:function(){this._$gameholder=t('<div class="blockrain-game-holder"></div>'),this._$gameholder.css("position","relative").css("width","100%").css("height","100%"),this.element.html("").append(this._$gameholder),this._$canvas=t('<canvas style="display:block; width:100%; height:100%; padding:0; margin:0; border:none;" />'),"string"==typeof this._theme.background&&this._$canvas.css("background-color",this._theme.background),this._$gameholder.append(this._$canvas),this._canvas=this._$canvas.get(0),this._ctx=this._canvas.getContext("2d")},_createUI:function(){var e=this,i=document.createElement("audio");i.src="backgroundaudio.ogg",i.volume=.1,i.autoPlay=!1,i.preLoad=!0,e._$score=t('<div class="blockrain-score-holder" style="position:absolute;"><div class="blockrain-score"><div class="blockrain-score-msg">'+this.options.scoreText+'</div><div class="blockrain-score-num">0</div></div></div>').hide(),e._$scoreText=e._$score.find(".blockrain-score-num"),e._$gameholder.append(e._$score),e._$start=t('<div class="blockrain-start-holder" style="position:absolute;"><div class="blockrain-start"><div class="blockrain-start-msg">'+this.options.playText+'</div><a class="blockrain-btn blockrain-start-btn">'+this.options.playButtonText+"</a></div></div>").hide(),e._$gameholder.append(e._$start),e._$start.find(".blockrain-start-btn").click(function(t){t.preventDefault(),i.play(),e.start()}),e._$gameover=t('<div class="blockrain-game-over-holder" style="position:absolute;"><div class="blockrain-game-over"><div class="blockrain-game-over-msg">'+this.options.gameOverText+'</div><a class="blockrain-btn blockrain-game-over-btn">'+this.options.restartButtonText+"</a></div></div>").hide(),e._$gameover.find(".blockrain-game-over-btn").click(function(t){t.preventDefault(),e.restart()}),e._$gameholder.append(e._$gameover),this._createControls()},_createControls:function(){this._$touchLeft=t('<a class="blockrain-touch blockrain-touch-left" />').appendTo(this._$gameholder),this._$touchRight=t('<a class="blockrain-touch blockrain-touch-right" />').appendTo(this._$gameholder),this._$touchRotateRight=t('<a class="blockrain-touch blockrain-touch-rotate-right" />').appendTo(this._$gameholder),this._$touchRotateLeft=t('<a class="blockrain-touch blockrain-touch-rotate-left" />').appendTo(this._$gameholder),this._$touchDrop=t('<a class="blockrain-touch blockrain-touch-drop" />').appendTo(this._$gameholder)},_refreshBlockSizes:function(){this.options.autoBlockWidth&&(this.options.blockWidth=Math.ceil(this.element.width()/this.options.autoBlockSize))},_getNiceShapes:function(){var t=this,e={};for(var i in this._shapeFactory)e[i]=this._shapeFactory[i]();function o(e,i,o,n,s,r,a){var h,l,c,d,u=i.length,_=0,f={};for(h=0;h<u;h+=2)_+=e[t._filled.asIndex(o+i[h],n+i[h+1])]||0;for(h=0;h<u;h+=2)l=i[h],c=i[h+1],(void 0===f[l]||f[l]<c)&&(f[l]=c);for(l in d=0,f)for(c=f[l=parseInt(l)]+1,h=0;n+c<a;c++,h++)if(!t._filled.check(o+l,n+c)){d+=0==h?2:1;break}return _-=d}var n=function(i,n,s,r,a,h){h||function(){for(var t in e)e[t].x=0,e[t].y=-1}();var l,c,d,u,_,f,g,p,b,m,k,v,y,w,x,C=new Array(s*r),B="evil"==a,I=999*(B?1:-1);for(l=0;l<s;l++)for(c=0;c<=r;c++)if(c==r||i.check(l,c)){for(d=c-4;d<c;d++)C[i.asIndex(l,d)]=d;break}var T=void 0===h?e:{cur:h};for(u in T){for(_=T[u],y=-999,f=0;f<(_.symmetrical?2:4);f++)for(g=_.getBlocks(f),l=-(p=_.getBounds(g)).left;l<s-p.width;l++)for(c=-1;c<r-p.bottom;c++)if(t._checkCollisions(l,c+1,g,!0)){(b=o(C,g,l,c,0,0,r))>y&&(y=b,w=f,x=l);break}(B&&y<I||!B&&y>I)&&(m=_,I=y,k=w,v=x)}return m.best_orientation=k,m.best_x=v,m};return n.no_preview=!0,n},_randomShapes:function(){var e=[];return t.each(this._shapeFactory,function(t,i){e.push(i)}),this._randChoice(e)},_setupControls:function(e){var i=this,o=function(t){t?i._board.holding.left||(i._board.cur.moveLeft(),i._board.holding.left=Date.now(),i._board.holding.right=null):i._board.holding.left=null},n=function(t){t?i._board.holding.right||(i._board.cur.moveRight(),i._board.holding.right=Date.now(),i._board.holding.left=null):i._board.holding.right=null},s=function(t){t?i._board.holding.drop||(i._board.cur.drop(),i._board.holding.drop=Date.now()):i._board.holding.drop=null},r=function(t){if(!i._board.cur)return!0;var e=!1;if(e=!0,i.options.asdwKeys)switch(t.keyCode){case 65:o(!0);break;case 68:n(!0);break;case 83:s(!0);break;case 87:i._board.cur.rotate("right")}switch(t.keyCode){case 37:o(!0);break;case 39:n(!0);break;case 40:s(!0);break;case 38:case 88:i._board.cur.rotate("right");break;case 90:i._board.cur.rotate("left");break;default:e=!1}return e&&t.preventDefault(),!e},a=function(t){if(!i._board.cur)return!0;var e=!1;if(e=!0,i.options.asdwKeys)switch(t.keyCode){case 65:o(!1);break;case 68:n(!1);break;case 83:s(!1)}switch(t.keyCode){case 37:o(!1);break;case 39:n(!1);break;case 40:s(!1);break;default:e=!1}return e&&t.preventDefault(),!e};function h(t){return"safekeypress."+t.keyCode}t(document).unbind("keydown.blockrain").unbind("keyup.blockrain"),i.options.autoplay||e&&t(document).bind("keydown.blockrain",function(e){var i=h(e);return t.data(this,i,(t.data(this,i)||0)-1),r.call(this,e)}).bind("keyup.blockrain",function(e){return t.data(this,h(e),0),a.call(this,e),function(t){var e={stopKeys:{37:1,38:1,39:1,40:1}},i=e.stopKeys[t.keyCode]||e.moreStopKeys&&e.moreStopKeys[t.keyCode];return i&&t.preventDefault(),i}(e)})},_setupTouchControls:function(t){var e=this;e._$touchLeft.unbind("touchstart touchend click"),e._$touchRight.unbind("touchstart touchend click"),e._$touchRotateLeft.unbind("touchstart touchend click"),e._$touchRotateRight.unbind("touchstart touchend click"),e._$touchDrop.unbind("touchstart touchend click"),!e.options.autoplay&&t?(e._$touchLeft.show().bind("touchstart click",function(t){t.preventDefault(),e._board.cur.moveLeft(),e._board.holding.left=Date.now(),e._board.holding.right=null,e._board.holding.drop=null}).bind("touchend",function(t){t.preventDefault(),e._board.holding.left=null}),e._$touchRight.show().bind("touchstart click",function(t){t.preventDefault(),e._board.cur.moveRight(),e._board.holding.right=Date.now(),e._board.holding.left=null,e._board.holding.drop=null}).bind("touchend",function(t){t.preventDefault(),e._board.holding.right=null}),e._$touchDrop.show().bind("touchstart click",function(t){t.preventDefault(),e._board.cur.drop(),e._board.holding.drop=Date.now()}).bind("touchend",function(t){t.preventDefault(),e._board.holding.drop=null}),e._$touchRotateLeft.show().bind("touchstart click",function(t){t.preventDefault(),e._board.cur.rotate("left")}),e._$touchRotateRight.show().bind("touchstart click",function(t){t.preventDefault(),e._board.cur.rotate("right")})):(e._$touchLeft.hide(),e._$touchRight.hide(),e._$touchRotateLeft.hide(),e._$touchRotateRight.hide(),e._$touchDrop.hide())}})}(jQuery),window.BlockrainThemes={custom:{background:"#040304",backgroundGrid:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAHHElEQVR4XsVZgZbkqgqkTO77//+dbuvtgQ7QcGwnO3PPZTPG1dhWASIxwP8OEcqTYhJ3ypsAuLqsB7KSNrQ14uMoXAXsnwNihoUDInKKbCdDf2YjPuL+KDRSyOpE1Q5k6JBJV7IJSfnvJUzf8RhyAOh9ADqN3vtz+am+zIXWHIK9l1D5ISuSTbv3aUAJZKfvmMYYBn3O6Y3W/lt2IFmmIHmbQDcCgOM4DCjJqeKsNgQAIe9ag13I4NNHoUWhomMn4BoiubXAqn27qAoNm9HLwhMAfQ10lgYxc5gqvgxcfuw8sdhMHKtD99IrGfCpkXZjBG9x9r8SizJ/JHF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line_sb.png"],square:["assets/blocks/custom/square_sb.png"],arrow:"assets/blocks/custom/arrow_sb.png",rightHook:["assets/blocks/custom/rightHook_sb.png"],leftHook:"assets/blocks/custom/leftHook_sb.png",rightZag:["assets/blocks/custom/rightZag_sb.png"],leftZag:"assets/blocks/custom/leftZag_sb.png"}}};