UNPKG

jocly

Version:

Board games 3D/2D UI and AI web library

1 lines 14.6 kB
exports.view=View={Game:{},Board:{},Move:{}},function(){var e,t,a,i;View.Game.xdInitExtra=function(e){},View.Game.xdPreInit=function(e){},View.Game.xdInit=function(n){function r(t){var a=this;return this.getResource("smoothedfilegeo|0|"+e+"/res/xd-view/meshes/stade-screen.js",function(e,i){for(var n=[],r=0;r<i.length;r++)if("mat.screen"==i[r].name){var s=i[r].clone();s.map=t,s.overdraw=!0,n.push(s)}else n.push(i[r]);var o=new THREE.Mesh(e,new THREE.MultiMaterial(n)),h=new THREE.SpotLight(16777215,5);h.position.set(0,0,2),h.target=o,o.add(h),o.visible=!1,a.objectReady(o)}),null}this.g.huntGameData={},this.g.huntGameData.sceneXtras=[],this.g.huntGameData.preyJumpsAtWalk=!1,this.g.huntGameData.clipwidth=155,this.g.huntGameData.clipheight=155,this.g.huntGameData.lightsIntensity=.55,this.g.huntGameData.jumpEatScale=1,this.g.huntGameData.jumpMoveScale=.5,this.g.huntGameData.killPieceZTempo=-.3,this.g.huntGameData.killPieceZFinal=-1,this.g.huntGameData.boardColOffset=0,this.g.huntGameData.boardRowOffset=0,this.g.huntGameData.targetColorSelected=14548957,this.g.huntGameData.soundMove=null,this.g.huntGameData.soundJump=null,this.g.huntGameData.soundEndJump=null,this.g.huntGameData.boardSize=1,this.g.huntGameData.boardStretch=!0,this.xdPreInit();var s=this.g.huntGameData,o=this.g.huntLayout,h=o.left+o.boardWidth+o.right,l=o.header+o.boardHeight+o.footer;i=Math.floor(Math.min(12e3/h,12e3/l)),t=h*i,a=l*i,this.HuntMakeCoord();for(var d=0;d<4;d++){var u=Math.PI/4+2*Math.PI/4*d;n.createGadget("blight#"+d,{"3d":{type:"custom3d",create:function(){var e=new THREE.SpotLight(16777215,s.lightsIntensity);e.castShadow=!1,e.shadow.camera.near=3,e.shadow.camera.far=20,e.shadow.camera.fov=90,e.shadow.mapSize.width=1024,e.shadow.mapSize.height=1024;var t=new THREE.Object3D,a=new THREE.Object3D;return t.add(a),e.target=a,t.add(e),t},y:8e3*Math.sin(u),x:8e3*Math.cos(u),z:6e3}})}for(var c=0;c<s.sceneXtras.length;c++)n.createGadget(s.sceneXtras[c].id,s.sceneXtras[c].gadgetData);var p=(o.header+o.boardHeight+o.footer)/(o.left+o.boardWidth+o.right);e=this.mViewOptions.fullPath,n.createGadget("board",{"2d":{type:"image"},"3d":{type:"custommesh3d",rotateX:-90,create:function(){function t(){if(++n==i){var e=new THREE.MeshPhongMaterial({color:16777215,map:o,specularMap:h,specular:l,shininess:d}),t=new THREE.Mesh(r,e);t.rotation.z=Math.PI/2,t.receiveShadow=!0,a.objectReady(t)}}if(void 0!==s.fieldMap){var a=this,i=1;void 0!==s.fieldSpecMap&&i++;var n=0,r=new THREE.PlaneGeometry(12/(p>1?p:1),12*(p<1?p:1),1,1),o=null,h=null,l=0,d=0;if((new THREE.TextureLoader).load(e+s.fieldMap,function(e){o=e,o.wrapS=o.wrapT=THREE.RepeatWrapping,o.format=THREE.RGBFormat,t()}),void 0!==s.fieldSpecMap){(new THREE.TextureLoader).loadTexture(e+s.fieldSpecMap,function(e){h=e,h.wrapS=h.wrapT=THREE.RepeatWrapping,h.format=THREE.RGBFormat,d=170,l=16777215,t()})}return null}return null}}}),n.createGadget("boardframe",{"3d":{type:"meshfile",file:e+s.boardJSFile,flatShading:!0,z:s.boardZ,receiveShadow:!0,scale:[s.boardSize/(p>1&&s.boardStretch?p:1),s.boardSize*(p<1&&s.boardStretch?p:1),s.boardSize]}});for(var g=0,m=0;m<2;m++)for(var c=0;c<this.g.initialPos[m].length;c++){var f=this.g.initialPos[m][c],v=1==this.g.catcher&&1==m||-1==this.g.catcher&&0==m,E=v?this.mViewOptions.fullPath+s.hunterJSFile:this.mViewOptions.fullPath+s.preyJSFile,G=v?s.hunterScale:s.preyScale,b=v?s.hunterMorph:s.preyMorph,S=v?s.hunterZ0:s.preyZ0,w=v?s.hunterMaterialsPreload:s.preyMaterialsPreload,D=this;!function(t,a){n.createGadget("piece#"+g++,{"2d":{type:"sprite",clipx:v?155:0,clipy:0,clipwidth:s.clipwidth,clipheight:s.clipheight,width:.8*i,height:.8*i,file:D.mViewOptions.fullPath+s.sprites,z:v?3:4},"3d":{type:"custommesh3d",scale:[G,G,G],morphing:b,z:S,create:function(i){var n="smoothedfilegeo|0|"+t,r=this;return this.getResource(n,function(t,n){function s(){if(0==--h){var e=new THREE.Mesh(t,new THREE.MultiMaterial(o));i(e)}}for(var o=[],h=1,l=0;l<n.length;l++)!function(t){h++;var i=n[t].clone();i.name===a.matName?r.getMaterialMap(e+a.matMap,function(e){i.map=e,o[t]=i,s()}):(o[t]=i,s())}(l);s()}),null}}})}(E,w)}for(var f=0;f<this.g.Graph.length;f++){var y=this.getCCoord(f);n.createGadget("cell#"+f,{base:{x:y[0],y:y[1]},"2d":{type:"element",x:y[0],y:y[1],z:2,width:.9*i,height:.9*i,classes:""},"3d":{type:"meshfile",file:e+s.targetJSFile,flatShading:!0,smooth:0,z:-50,castShadow:!1,scale:[.8,.8,.8],materials:{square:{transparent:!0,opacity:0},ring:{color:16777215,opacity:0,transparent:!0}}}}),function(t){n.createGadget("text#"+t,{"2d":{type:"element",initialClasses:"xd-notation",display:function(e,a,i){e.css({"font-size":.6*i+"pt","line-height":1*i+"px"}).text(t+1)},x:y[0]-.3*i,y:y[1]-.3*i,z:1,width:.2*i,height:.2*i},"3d":{type:"custommesh3d",rotateX:-90,create:function(){var a=this;return this.getResource("font|"+e+"/res/xd-view/fonts/helvetiker_regular.typeface.json",function(e){var i=new THREE.TextGeometry(""+(t+1),{size:.2,height:.05,curveSegments:6,font:e}),n=new THREE.MeshPhongMaterial,r=new THREE.Mesh(i,n);a.objectReady(r)}),null},color:8947848,x:y[0]-.2*i,y:y[1]-.2*i,z:100}})}(f)}n.createGadget("videoa",{"3d":{type:"video3d",makeMesh:function(e){r.call(this,e)}}}),n.createGadget("videob",{"3d":{type:"video3d",makeMesh:function(e){r.call(this,e)}}}),this.xdInitExtra(n)},View.Game.HuntMakeCoord=function(){var e=this.g.huntLayout,t=e.left+e.boardWidth+e.right,a=e.header+e.boardHeight+e.footer;this.g.Coord=[];for(var n=0;n<this.g.RC.length;n++){var r=this.g.RC[n],s=r[0],o=r[1];s+=this.g.huntGameData.boardRowOffset,o+=this.g.huntGameData.boardColOffset;var h;h=this.mViewAs==JocGame.PLAYER_A?[(o+1)*i-t*i*.5,(s+1)*i-a*i*.5]:[t*i*.5-(o+1)*i,a*i*.5-(s+1)*i],this.g.Coord.push(h)}},View.Game.xdBuildScene=function(e){for(var i=this.g.huntGameData,n=0;n<4;n++)e.updateGadget("blight#"+n,{"3d":{visible:!0}});for(var r=0;r<i.sceneXtras.length;r++)e.updateGadget(i.sceneXtras[r].id,i.sceneXtras[r].gadgetUpdateData);e.updateGadget("boardframe",{"3d":{visible:!0}}),e.updateGadget("board",{base:{visible:!0},"2d":{rotate:this.mViewAs==JocGame.PLAYER_A?0:180,x:0,y:0,width:t,height:a,file:this.mViewOptions.fullPath+i.boardField2D},"3d":{receiveShadow:!0,visible:!(void 0===i.fieldMap)}});for(var s=0;s<this.g.Graph.length;s++)e.updateGadget("text#"+s,{base:{visible:this.mNotation}});var o=i.screenScale;e.updateGadget("videoa",{"3d":{visible:!0,scale:[o,o,o],rotate:1==this.mViewAs?180:0,rotateX:1==this.mViewAs?30:-30,z:3e3,y:1==this.mViewAs?i.screenDist:-i.screenDist,playerSide:1}}),e.updateGadget("videob",{"3d":{visible:!0,scale:[o,o,o],rotate:1==this.mViewAs?0:180,rotateX:1==this.mViewAs?-30:30,z:3e3,y:1==this.mViewAs?-i.screenDist:i.screenDist,playerSide:-1}})},View.Game.getCCoord=function(e){return this.g.Coord[e]},View.Board.xdDisplay=function(e,t){for(var a=0;a<this.pieces.length;a++){var n=this.pieces[a];switch(n.p){case-2:var r=t.GetOffBoardCoord(a,i);e.updateGadget("piece#"+a,{base:{visible:!0,x:r[0],y:r[1],rotate:180}});break;case-1:e.updateGadget("piece#"+a,{base:{visible:!1}});break;default:var s=t.g.catcher!=n.s,o=s?t.g.huntGameData.hunterZ0:t.g.huntGameData.preyZ0,r=t.getCCoord(n.p);e.updateGadget("piece#"+a,{base:{visible:!0,x:r[0],y:r[1]},"2d":{opacity:1},"3d":{rotate:n.a,z:o}})}}},View.Board.xdBuildHTStateMachine=function(e,t,a){function i(i,n,r){function s(){t.smQueueEvent(d,{pos:i,capt:n,type:r})}var o=null,h=-1;m>=0&&i==p.p[p.p.length-1]?h=m:c.board[i]&&(h=c.board[i].i),h>=0&&(o=c.pieces[h]);var l,d,u;switch(r){case"normal":d="E_CLICKED",l=a.g.huntGameData.targetColorSelected,u="highlight";break;case"cancel":d="E_CANCEL",l=16729088,u="back";break;case"confirm":d="E_CONFIRM",l=65280,u="confirm"}var g=a.mShowMoves||"cancel"==r||"confirm"==r;e.updateGadget("cell#"+i,{base:{visible:!0,click:s},"2d":{classes:u,opacity:a.mShowMoves||"cancel"==r||"confirm"==r?.5:0},"3d":{castShadow:!1,receiveShadow:!0,materials:{square:{},ring:{color:l,opacity:g?1:0,transparent:!g}}}}),o&&e.updateGadget("piece#"+h,{base:{visible:!0,click:function(){s()}},"2d":{}})}function n(e){}function r(t){for(var i=0;i<a.g.Coord.length;i++)e.updateGadget("cell#"+i,{base:{visible:!1,click:null},"2d":{classes:""},"3d":{materials:{ring:{opacity:0,transparent:!0}}}});for(var n=0;n<c.pieces.length;n++){c.pieces[n];e.updateGadget("piece#"+n,{base:{click:null}})}}function s(e){for(var a=[],n=0;n<g.length;n++){for(var r=g[n],s=!0,o=0;o<p.p.length;o++)if(r.p[o]!=p.p[o]){s=!1;break}s&&a.push(r)}if(1==a.length&&p.p.length==a[0].p.length)return void t.smQueueEvent("E_DONE",{move:a[0]});p.p.length>1?(i(p.p[p.p.length-2],null,"cancel"),i(p.p[p.p.length-1],null,"confirm")):p.p.length>0&&i(p.p[p.p.length-1],null,"cancel"),nextPoss={};for(var n=0;n<a.length;n++){var r=a[n];nextPoss[r.p[p.p.length]]=p.p.length<1?null:void 0!==r.c?r.c[p.p.length-1]:null}for(var h in nextPoss)nextPoss.hasOwnProperty(h)&&i(h,nextPoss[h],"normal")}function o(i){if(p.p.length<=1)t.smQueueEvent("E_ANIM_DONE",{});else{var n=c.HuntAngle(a,p.p[p.p.length-2],p.p[p.p.length-1]);c.HuntAnimatePiece(e,a,m,p.p[p.p.length-2],p.p[p.p.length-1],n,f,function(){t.smQueueEvent("E_ANIM_DONE",{})})}}function h(t){m<0&&(m=c.board[t.pos].i),e.saveGadgetProps("piece#"+m,["x","y","z","rotate","opacity"],"piece-"+p.p.length),p.p.push(t.pos),null!==t.capt&&(void 0===p.c&&(p.c=[]),f=c.board[t.capt].i,e.saveGadgetProps("piece#"+f,["z"],"alive"),p.c.push(t.capt))}function l(e){a.MakeMove(p)}function d(t){p.p.splice(p.p.length-1),e.restoreGadgetProps("piece#"+m,"piece-"+p.p.length),0==p.p.length&&(m=-1),void 0!==p.c&&(f>=0&&e.restoreGadgetProps("piece#"+f,"alive"),p.c.splice(p.c.length-1),0==p.c.length?(f=-1,delete p.c):f=c.board[p.c[p.c.length-1]].i)}function u(i){c.HuntAnimateVanish(e,a,p.c,function(){t.smQueueEvent("E_ANIM_DONE",{})})}var c=this,p={p:[]},g=[];a.g.useDrop&&(g=this.HuntGetAllDropMoves(a)),0==g.length&&(g=this.HuntGetAllMoves(a));var m=-1,f=-1;t.smTransition("S_INIT","E_INIT","S_SELECT",[n]),t.smEntering("S_SELECT",[s]),t.smTransition("S_SELECT","E_CONFIRM","S_ANIMATING_VANISH",[u]),t.smTransition("S_SELECT","E_ENTER","S_ANIMATING",[o]),t.smTransition("S_SELECT","E_CLICKED","S_ANIMATING",[h,o]),t.smTransition("S_SELECT","E_CANCEL",null,[r,d,s]),t.smTransition("S_SELECT","E_DONE","S_ANIMATING_VANISH",[u]),t.smLeaving("S_SELECT",[r]),t.smTransition("S_ANIMATING","E_ANIM_DONE","S_SELECT",[]),t.smTransition("S_ANIMATING_VANISH","E_ANIM_DONE",null,[l]),t.smTransition(["S_SELECT","S_ANIMATING","S_ANIMATING_VANISH"],"E_END","S_DONE",[]),t.smEntering("S_DONE",[r])},View.Board.xdPlayedMove=function(e,t,a){function i(){if(++s==a.p.length)t.mOldBoard.HuntAnimateVanish(e,t,a.c,function(){t.MoveShown()});else{var o=n.HuntAngle(t,a.p[s-1],a.p[s]),h=-1;void 0!==a.c&&a.c.length>s-1&&(h=t.mOldBoard.board[a.c[s-1]].i),n.HuntAnimatePiece(e,t,r,a.p[s-1],a.p[s],o,h,i)}}var n=this,r=t.mOldBoard.board[a.p[0]].i;if(0==a.p.length)this.HuntAnimateDrop(e,t,r,a.p[0],function(){t.mOldBoard.HuntAnimateVanish(e,t,a.c,function(){t.MoveShown()})});else{var s=0;i()}return!1},View.Board.HuntAnimatePiece=function(e,t,a,n,r,s,o,h){function l(){var i=g[u++];u<=f?e.updateGadget("piece#"+a,{base:{x:C[0]+(d[0]-C[0])*(u/g.length),y:C[1]+(d[1]-C[1])*(u/g.length)},"3d":{positionEasing:"undefined"==typeof TWEEN?null:TWEEN.Easing.Linear.EaseNone,morphingEasing:"undefined"==typeof TWEEN?null:TWEEN.Easing.Linear.EaseNone,morphing:i,positionEasingUpdate:function(e){var t=(u-1)/f+1*e/f,a=(x*t*t+_*t+S)*this.SCALE3D;this.object3d.position.y=a}}},I,l):(G&&t.PlaySound(p.soundEndJump),e.updateGadget("piece#"+a,{base:{x:d[0],y:d[1]},"3d":{morphing:m,z:b}},I,h))}var d=t.getCCoord(r),u=0,c=t.g.catcher!=this.mWho,p=t.g.huntGameData,g=p.sequences[c?"hunter":"prey"][!c&&o>=0?"jump":"walk"],m=c?p.hunterMorph:p.preyMorph,f=g.length,v=c?t.g.huntGameData.hunterZ0:t.g.huntGameData.preyZ0,E=v,G=o>=0;c||(G?E+=i*p.jumpEatScale:p.preyJumpsAtWalk&&(E+=i*p.jumpMoveScale));var b=v,S=v,w=S-E,D=S-b,y=4*w-2*D,M=-D*D,T=y*y- -4*M,A=(-y-Math.sqrt(T))/-2,N=(-y+Math.sqrt(T))/-2,x=A,_=-x-D;(0==x||-_/(2*x)<0||-_/(2*x)>1)&&(x=N,_=-x-D);var C=t.getCCoord(n),I=550/f;e.updateGadget("piece#"+a,{"3d":{rotate:s}},250,function(){o>=0&&e.updateGadget("piece#"+o,{"3d":{z:p.killPieceZTempo*i,positionEasingUpdate:null}},400),G||t.PlaySound(p.soundMove),l()})},View.Board.HuntAnimateDrop=function(e,t,a,i,n){var r=t.getCCoord(i);e.updateGadget("piece#"+a,{base:{x:r[0],y:r[1]}},500,n)},View.Board.HuntAnimateVanish=function(e,t,a,n){function r(){0==--s&&n()}if(void 0===a||0==a.length)n();else{for(var s=0,o={},h=0;h<a.length;h++)o[a[h]]=!0;for(var l in o)o.hasOwnProperty(l)&&(s++,e.updateGadget("piece#"+this.board[l].i,{base:{opacity:0},"3d":{z:t.g.huntGameData.killPieceZFinal*i}},500,r))}}}(),View.Game.xdPreInit=function(e){this.g.huntLayout={header:1,boardHeight:4,footer:1,left:1,boardWidth:4,right:1},this.g.huntGameData.sprites="/res/images/liebre/tokens.png",this.g.huntGameData.boardJSFile="/res/xd-view/meshes/board.js",this.g.huntGameData.hunterJSFile="/res/xd-view/meshes/liebre/hunter2.js",this.g.huntGameData.hunterMaterialsPreload={matName:"mat.short",matMap:"/res/xd-view/meshes/liebre/hunter.jpg"},this.g.huntGameData.preyJSFile="/res/xd-view/meshes/liebre/liebre6.js",this.g.huntGameData.preyMaterialsPreload={matName:"Material",matMap:"/res/xd-view/meshes/liebre/rabbitskin4.jpg"},this.g.huntGameData.targetJSFile="/res/xd-view/meshes/target.js",this.g.huntGameData.preyScale=.1,this.g.huntGameData.hunterScale=.25,this.g.huntGameData.boardSize=1.25,this.g.huntGameData.boardZ=0,this.g.huntGameData.preyZ0=300,this.g.huntGameData.hunterZ0=0,this.g.huntGameData.screenDist=12e3,this.g.huntGameData.screenScale=4.5,this.g.huntGameData.fieldMap="/res/xd-view/meshes/liebre/liebreboardsimple.jpg",this.g.huntGameData.boardField2D="/res/images/liebre/liebreboard.jpg",this.g.huntGameData.preyMorph=[1,0,0,0,0],this.g.huntGameData.hunterMorph=[1,0,0,0,0,0,0,0,0,0,0,0],this.g.huntGameData.killPieceZTempo=-.5,this.g.huntGameData.jumpEatScale=1.5,this.g.huntGameData.preyJumpsAtWalk=!0,this.g.huntGameData.sequences=[],this.g.huntGameData.sequences.prey=[],this.g.huntGameData.sequences.prey.walk=[[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]],this.g.huntGameData.sequences.prey.jump=this.g.huntGameData.sequences.prey.walk,this.g.huntGameData.sequences.hunter=[],this.g.huntGameData.sequences.hunter.walk=[[0,0,0,1,0,0,0,0,0,0,0,0],[0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,0,0],[0,0,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,0,0,0,1,0,0,0],[0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0],[1,0,0,0,0,0,0,0,0,0,0,0]]},View.Game.xdInitExtra=function(e){};