"use strict";vartslib=require("tslib"),props={};functionprepareProps(){vart=getProps();engine.injectProp(props,t)}functioninjectProps(t){engine.injectProp(props,t)}varMAX_COL,MAX_ROW,W,H,GAP,GAME_TIME,w,h,picMap={},posMap={},qietu=function(n,r,o,a){if(picMap[r]){for(vart=0,e=picMap[r];t<e.length;t++){vari=e[t];n.addChild(i)}return[picMap[r],posMap[r]]}for(varc=props.W,h=props.H,p=props.GAP,u=[],d=[],s=function(s){for(vart=function(e){vari=engine.Sprite.fromImage(r);u.push(i),i.scaleX=1/o,i.scaleY=1/a,n.addChild(i),i.x=e*(c/o+p),i.y=s*(h/a+p),d.push([i.x,i.y]),i.addEventListener(engine.Event.COMPLETE,function(){vart=newFloat32Array([e/o,s/a,(e+1)/o,s/a,(e+1)/o,(s+1)/a,e/o,(s+1)/a]);i.uvs=t})},e=0;e<o;e++)t(e)},g=0;g<a;g++)s(g);returnpicMap[r]=u.concat([]),posMap[r]=d.concat([]),[u,d]};functiongetTexture(t){returnengine.Texture.from(getAssetByUUID(t).uuid)}functiongetTextureByName(t){returngetTexture(engine.getAssetByName(t).uuid)}functiongetIndexFromRC(t,e,i){returnt*i+e}functiongetRandomArray(t){t.sort(function(){return.5-Math.random()})}varGameView=function(e){functiont(){vart=e.call(this)||this;returnt._timeCounter=0,t.once(engine.Event.ADDED_TO_STAGE,t.setup,t),t}returntslib.__extends(t,e),t.prototype.start=function(){vart=this;if(this.pictures)for(vare=0,i=this.pictures;e<i.length;e++){vars=i[e];s&&s.parent&&s.parent.removeChild(s)}console.log("on start"),engine.globalEvent.dispatchEvent("pictures-time-update",{second:this.getSecond()});varn=qietu(this.picturesWrapper,props.picUrl,MAX_COL,MAX_ROW);this.picturesWrapper.addChild(this.guideHole),console.log(this.picturesWrapper),this.pictures=n[0],this.rightList=this.pictures.concat([]);varr=n[1];getRandomArray(this.pictures);varo,a=0;for(o=this.pictures.length;a<o;a++){this.dragPic=this.pictures[a],this.pictures[a].addEventListener(engine.MouseEvent.MOUSE_DOWN,this.onDown,this);varc=r[a],h=c[0],p=c[1];this.dragPic.x=h,this.dragPic.y=p}this._timer=setInterval(function(){t.onTimer()},10)},t.prototype.onTimer=function(){this._timeCounter+=.01,this._timeCounter=this.afterPointTwo(this._timeCounter),engine.globalEvent.dispatchEvent("pictures-time-update",{second:this.getSecond()}),0==this.getSecond()&&(this.stop(),engine.globalEvent.dispatchEvent("pictures-game-fail",{reason:1}))},t.prototype.afterPointTwo=function(t){vare=parseFloat(t);if(!isNaN(e))returne=Math.round(100*e)/100},t.prototype.getSecond=function(){returnGAME_TIME-this._timeCounter},t.prototype.stop=function(){this._timeCounter=0,clearInterval(this._timer)},t.prototype.createRects=function(){},t.prototype.setup=function(){this.guideHole=newengine.Image(getTextureByName("遮罩")),this.guideHole.mouseChildren=this.guideHole.mouseEnabled=!1,MAX_COL=props.MAX_COL,MAX_ROW=props.MAX_ROW,GAME_TIME=props.GAME_TIME,W=props.W,H=props.H,GAP=props.GAP,w=W/MAX_COL,h=H/MAX_ROW,console.log("onSteup",props);vart=newengine.Sprite;this.picturesWrapper=t,this.addChild(t)},t.prototype.onDown=function(t){this.dragPic=t.target,this.picturesWrapper.addChild(this.dragPic),this.localPicX=t.localX/MAX_COL,this.localPicY=t.localY/MAX_ROW,this.distanceX=this.dragPic.x,this.distanceY=this.dragPic.y,this.indexJ=Math.floor(this.distanceX/(w+GAP)),this.indexI=Math.floor(this.distanceY/(h+GAP)),this.index=this.indexI*MAX_COL+this.indexJ,this.centerX=Math.floor(t.clientX/w)*w+w/2,this.centerY=Math.floor(t.clientY/h)*h+w/2,this.stage.addEventListener(engine.MouseEvent.MOUSE_MOVE,this.onMove,this),this.stage.once(engine.MouseEvent.MOUSE_UP,this.stageOnUp,this)},t.prototype.stageOnUp=function(t){this.stage.removeEventListener(engine.MouseEvent.MOUSE_MOVE,this.onMove,this),(this.centerY<0||this.centerX<0)&&(this.dragPic.x=this.distanceX,this.dragPic.y=this.distanceY),this.picturesWrapper.addChild(this.guideHole);vare=Math.floor(this.centerX/(w+GAP)),i=Math.floor(this.centerY/(h+GAP));if(e<MAX_COL&&i<MAX_ROW){vars=getIndexFromRC(i,e,MAX_COL),n=this.pictures[s],r=n.x,o=n.y;n.x=this.distanceX,n.y=this.distanceY,this.dragPic.x=r,this.dragPic.y=o;vara=this.pictures.indexOf(n),c=this.pictures.indexOf(this.dragPic);this.pictures[a]=this.dragPic,this.pictures[c]=n,c===a&&(this.dragPic.x=this.distanceX,this.dragPic.y=this.distanceY);for(varp=!0,u=0;u<this.rightList.length;u++)if(this.rightList[u]!=this.pictures[u]){p=!1;break}p&&this.onSuccess()}elsethis.dragPic.x=this.distanceX,this.dragPic.y=this.distanceY},t.prototype.onSuccess=function(){console.log("拼图成功!"),engine.globalEvent.dispatchEvent("pictures-game-success",{time:this._timeCounter}),this.stop()},t.prototype.onMove=function(t){this.dragPic.x=t.stageX-this.localPicX-(750-props.W)/2,this.dragPic.y=t.stageY-this.localPicY-(this.stage.height-props.H)/2,console.log("fuck on this.stage.height",this.stage.height),this.centerX=this.dragPic.x+w/2,this.centerY=this.dragPic.y+h/2},t}(engine.Container),GameWrapper=function(i){functiont(){vart=i.call(this)||this;engine.globalEvent.addEventListener("pictures-start",t.start,t),engine.globalEvent.addEventListener("pictures-stop",t.stop,t);vare=t._gameView=newGameView;returnt.addChild(e),t}returntslib.__extends(t,i),t.prototype.start=function(t){injectProps(t.data),this._gameView.start()},t.prototype.stop=function(t){this._gameView.stop()},t}(engine.Container);functionindex(t){returnprepareProps(),injectProps(t),newGameWrapper}module.exports=index;