jocly
Version:
Board games 3D/2D UI and AI web library
1 lines • 10.5 kB
JavaScript
exports.view=View={Game:{},Board:{},Move:{}},function(){var e,t,a,i,o,n=["","white","black","red"],s=[0,.1,.9,.7];View.Game.xdInitExtra=function(e){},View.Game.xdPreInit=function(e){},View.Game.xdInit=function(n){function s(e){var t=this;return this.getResource("smoothedfilegeo|0|"+a+"/res/xd-view/meshes/flatscreen.js",function(a,i){for(var o=[],n=0;n<i.length;n++)if("screen"==i[n].name){var s=i[n].clone();s.map=e,s.overdraw=!0,o.push(s)}else if("boomer"==i[n].name){var s=i[n].clone();s.shading=THREE.FlatShading,o.push(s)}else if("tv"==i[n].name){var s=i[n].clone();s.shading=THREE.FlatShading,o.push(s)}else o.push(i[n]);var r=new THREE.Mesh(a,new THREE.MultiMaterial(o));r.visible=!1,t.objectReady(r)}),null}if(a=this.mViewOptions.fullPath,"undefined"!=typeof THREE){i=new THREE.SphereGeometry(1,32,16);var r=a+"/res/xd-view/meshes/skybox/",d=[r+"px.jpg",r+"nx.jpg",r+"py.jpg",r+"ny.jpg",r+"pz.jpg",r+"nz.jpg"];o=(new THREE.CubeTextureLoader).load(d)}this.xdPreInit(),e=this.mOptions.size,t=Math.round(5800/e),n.createGadget("lightside",{"3d":{type:"custom3d",create:function(){return new THREE.PointLight(11184895,1,30)},z:1e4,x:1e4,castShadow:!1}}),n.createGadget("lightback",{"3d":{type:"custom3d",create:function(){return new THREE.PointLight(11193599,1,30)},z:-1e4,castShadow:!1}}),n.createGadget("skyball",{classic3d:{harbor:!1,type:"custommesh3d",rotate:135,rotateX:-60,create:function(){var e=new THREE.SphereGeometry(40,50,50);new THREE.MeshBasicMaterial({color:65280,wireframe:!1,side:THREE.DoubleSide});e.computeBoundingBox(),zMin=e.boundingBox.min.z,zMax=e.boundingBox.max.z,zRange=zMax-zMin;for(var t,i,o,n,s,r=["a","b","c","d"],d=0;d<e.vertices.length;d++){i=e.vertices[d],t=new THREE.Color(0);var l=(zMax-i.z)/zRange;t.b=1+l,t.g=.5+.4*l,t.r=.3*l,e.colors[d]=t}for(var d=0;d<e.faces.length;d++){o=e.faces[d],n=o instanceof THREE.Face3?3:4;for(var c=0;c<n;c++)s=o[r[c]],o.vertexColors[c]=e.colors[s]}var h=this,g=new THREE.TextureLoader;return g.setCrossOrigin("anonymous"),g.load(a+"/res/xd-view/meshes/square.png",function(t){t.wrapS=t.wrapT=THREE.RepeatWrapping,t.repeat.set(40,40);var a=new THREE.MeshBasicMaterial({map:t,vertexColors:THREE.VertexColors,side:THREE.DoubleSide});a.map.repeat.set(20,60);var i=new THREE.Mesh(e,a);i.doubleSided=!0,h.objectReady(i)},function(e){},function(e){console.log("error loading texture")}),null},opacity:1}}),n.createGadget("videoa",{"3d":{type:"video3d",makeMesh:function(e){s.call(this,e)}}}),n.createGadget("videob",{"3d":{type:"video3d",makeMesh:function(e){s.call(this,e)}}}),n.createGadget("board",{wood:{type:"image",file:a+"/res/images/wood.png",z:0}}),this.xdInitExtra(n)};var r={};View.Game.isGadgetId=function(e,t){return void 0!==r[t+"#"+e]},View.Game.spGadgetId=function(e,n,s){var d=s+"#"+n;if(void 0===r[d]){r[d]=!0;var l=.001*t*1.01,c=!1,h=1,g=!0,p="red",u=1,v=null,m=null;switch(s){case"piece":c=!0,u=4;break;case"cell":l=.001*t*.5,c=!0,h=.5,p="green",u=2;break;case"plot":l=.001*t*.2,c=!0,h=.7,p="blue",v="plot",m=.001*t*.6;break;case"text":g=!1,e.createGadget("text#"+n,{"2d":{type:"element",width:.2*t,height:.2*t,initialClasses:"notation",css:{color:"#808080","text-align":"center","font-weight":"bold"},z:2,display:function(e,t,a){e.css({"font-size":1*a+"pt","line-height":1*a+"px"}).text(n)}},"3d":{type:"custommesh3d",z:0,create:function(){var e=this;return this.getResource("font|"+a+"/res/xd-view/fonts/helvetiker_regular.typeface.json",function(a){var i=new THREE.TextGeometry(""+(n+0),{size:t*e.SCALE3D*.3,height:.05,curveSegments:6,font:a}),o=new THREE.MeshPhongMaterial({}),s=new THREE.Mesh(i,o);e.objectReady(s)}),null}}})}g&&e.createGadget(s+"#"+n,{"2d":{type:"image",file:a+"/res/images/"+(v||"ball_"+p)+".png",initialClasses:"xd-choice",width:2*t*l,height:2*t*l,z:u,opacity:h,classes:"sp-disk"},wood:{width:2*t*(m||l),height:2*t*(m||l)},"3d":{type:"custommesh3d",scale:[l,l,l],create:function(){for(var e=THREE.SmoothShading,t=new THREE.MeshPhongMaterial({name:"ball",specular:16777215,shininess:500,shading:e,opacity:h,transparent:c,envMap:o,reflectivity:.2,combine:THREE.MixOperation}),a=i.clone(),n=0;n<a.faces.length;n++)a.faces[n].materialIndex=0;return new THREE.Mesh(a,new THREE.MultiMaterial([t]))}}})}return d},View.Game.xdBuildScene=function(i){i.updateGadget("board",{wood:{visible:!0,width:2*t*(e+.3),height:2*t*(e+.3)}}),i.updateGadget("lightside",{"3d":{visible:!0}}),i.updateGadget("lightback",{"3d":{visible:!0}}),i.updateGadget("skyball",{"3d":{visible:!0}});i.updateGadget("videoa",{"3d":{visible:!0,scale:[2,2,2],rotate:1==this.mViewAs?180:0,rotateX:1==this.mViewAs?30:-30,z:3e3,y:1==this.mViewAs?12e3:-12e3,playerSide:1}}),i.updateGadget("videob",{"3d":{visible:!0,scale:[2,2,2],rotate:1==this.mViewAs?0:180,rotateX:1==this.mViewAs?-30:30,z:3e3,y:1==this.mViewAs?-12e3:12e3,playerSide:-1}});for(var o=0;o<e*e;o++){var n=this.getVCoord(o);i.updateGadget(this.spGadgetId(i,o,"plot"),{base:{visible:!0,x:n[0],y:n[1]},"3d":{materials:{ball:{map:a+"/res/xd-view/meshes/grey.png"}}}})}for(var o=0;o<this.g.Graph.length;o++){var n=this.getVCoord(o);i.updateGadget(this.spGadgetId(i,o,"text"),{base:{visible:this.mNotation,x:n[0],y:n[1]},"3d":{x:n[0]-.25*t,z:n[2]-.15*t}})}},View.Game.getVCoord=function(){var a,i,o;if(1==arguments.length){var n=arguments[0],s=this.g.Coord[n];a=s[0],i=s[1],o=s[2]}else a=arguments[0],i=arguments[1],o=arguments[1];var r=i-(e-1)/2+o/2,d=a-(e-1)/2+o/2,l=o*Math.sqrt(2)/2;return[2*r*t,2*d*t,2*l*t]},View.Game.getCCoord=function(e){var t=this.g.Coord[e],a=t[0],i=t[1],o=t[2];return this.getVCoord(a,i,o)},View.Board.xdDisplay=function(e,i){for(var o=.001*t*1.01,r=0;r<i.g.Coord.length;r++){if(0==this.board[r]&&i.isGadgetId(r,"piece"))e.updateGadget(i.spGadgetId(e,r,"piece"),{base:{visible:!1}});else if(this.board[r]){var d=i.getVCoord(r),l=i.g.Coord[r];e.updateGadget(i.spGadgetId(e,r,"piece"),{base:{visible:!0,x:d[0],y:d[1]},"2d":{file:a+"/res/images/ball_"+n[this.board[r]]+".png",width:2*t,height:2*t,z:2*l[2]+4},"3d":{z:d[2],scale:[o,o,o],materials:{ball:{map:a+"/res/xd-view/meshes/"+n[this.board[r]]+".png",reflectivity:s[this.board[r]],opacity:1}}}})}e.updateGadget(i.spGadgetId(e,r,"text"),{"2d":{visible:i.mNotation&&0==this.board[r]&&null==i.g.Beneath[r]}})}e.updateGadget("videoa",{"3d":{materials:{tv:{color:12298905}}}}),e.updateGadget("videob",{"3d":{materials:{tv:{color:2236962}}}})},View.Board.xdBuildHTStateMachine=function(e,t,i){function o(o,n){function r(){t.smQueueEvent("E_CLICK",{pos:o,type:n})}var d=i.getVCoord(o),l=i.g.Coord[o],c="green",h="piece";"normal"==n?h=0==f.board[o]?"cell":null:"moveto"==n?h="cell":"cancel"==n&&(c="red"),h&&e.updateGadget(i.spGadgetId(e,o,h),{base:{visible:!0,x:d[0],y:d[1],click:r,z:2*l[2]+5},"2d":{file:a+"/res/images/ball_"+c+".png"},"3d":{z:d[2],materials:{ball:{map:a+"/res/xd-view/meshes/"+c+".png",reflectivity:"red"==c||"green"==c?.1:s[E]}}}})}function r(e){}function d(t){for(var o=0;o<i.g.Graph.length;o++)i.isGadgetId(o,"cell")&&e.updateGadget(i.spGadgetId(e,o,"cell"),{base:{visible:!1,click:null}}),i.isGadgetId(o,"piece")&&f.board[o]&&e.updateGadget(i.spGadgetId(e,o,"piece"),{base:{click:null},"2d":{file:a+"/res/images/ball_"+n[f.board[o]]+".png"},"3d":{materials:{ball:{map:a+"/res/xd-view/meshes/"+n[f.board[o]]+".png",reflexivity:s[f.board[o]]}}}})}function l(e){for(var t={},a={},i=0;i<b.length;i++){var n=b[i];"+"==n.act?t[n.pos]=!0:">"==n.act&&(a[n.from]=!0)}for(var s in t)t.hasOwnProperty(s)&&o(s,"normal");for(var s in a)a.hasOwnProperty(s)&&o(s,"movefrom")}function c(e){o(w,"cancel");for(var t=0;t<b.length;t++){var a=b[t];w==a.from&&o(a.to,"moveto")}}function h(e){m=e.pos;for(var t=0;t<b.length;t++){var a=b[t];if(null===w){if(a.pos==m)return void(move0=a)}else if(a.from==w&&a.to==G)return void(move0=a)}console.error("move not found",m,b)}function g(a){f.spAnimateMove(e,i,move0,function(){t.smQueueEvent("E_ANIM_DONE",{})})}function p(e){i.MakeMove(move0)}function u(e){switch(e.type){case"normal":t.smQueueEvent("E_CLICK_ADD",e);break;case"movefrom":w=e.pos,t.smQueueEvent("E_CLICK_FROM",e);break;case"moveto":G=e.pos,t.smQueueEvent("E_CLICK_TO",e);break;case"cancel":t.smQueueEvent("E_CLICK_CANCEL",e)}}function v(e){w=null}var m,f=this,E=(1-this.mWho)/2+1,b=this.GenerateAllMoves(i),w=null,G=null;t.smTransition("S_INIT","E_INIT","S_SELECT",[r]),t.smEntering("S_SELECT",[l]),t.smTransition("S_SELECT","E_CLICK",null,[d,u]),t.smTransition("S_SELECT","E_CLICK_FROM",null,[c]),t.smTransition("S_SELECT","E_CLICK_TO","S_ANIMATING",[h,g]),t.smTransition("S_SELECT","E_CLICK_ADD","S_ANIMATING",[h,g]),t.smTransition("S_SELECT","E_CLICK_CANCEL",null,[d,v,l]),t.smLeaving("S_SELECT",[d]),t.smTransition("S_ANIMATING","E_ANIM_DONE","S_DONE",[p]),t.smTransition(["S_SELECT","S_DONE"],"E_END","S_DONE",[]),t.smEntering("S_DONE",[d])},View.Board.xdPlayedMove=function(e,t,a){return t.mOldBoard.spAnimateMove(e,t,a,function(){t.MoveShown()}),!1},View.Board.spAnimateMove=function(e,t,a,i){"+"==a.act?this.spAnimateMoveAdd.apply(this,arguments):">"==a.act&&this.spAnimateMoveShift.apply(this,arguments)},View.Board.spAnimateMoveAdd=function(e,i,o,r){function d(){0==--v&&r()}function l(a){v=a.length;for(var o=0;o<a.length;o++)e.updateGadget(i.spGadgetId(e,a[o],"piece"),{base:{},"2d":{width:2*t*p,height:2*t*p},"3d":{scale:[p,p,p]}},800,function(){d()})}var c=(1-this.mWho)/2+1,h=i.getVCoord(o.pos),g=i.g.Coord[o.pos],p=.001*t*.001,u=.001*t*1.01;e.updateGadget(i.spGadgetId(e,o.pos,"piece"),{base:{visible:!0,x:h[0],y:h[1]},"2d":{width:2*t*p,height:2*t*p,z:2*g[2]+4,file:a+"/res/images/ball_"+n[c]+".png"},"3d":{z:h[2],scale:[p,p,p],materials:{ball:{map:a+"/res/xd-view/meshes/"+n[c]+".png",reflectivity:s[c],opacity:1},transparent:!0}}}),e.updateGadget(i.spGadgetId(e,o.pos,"piece"),{base:{},"2d":{width:2*t,height:2*t},"3d":{scale:[u,u,u]}},800,function(){void 0!==o.remove&&o.remove.length>0?l(o.remove):r()});var v},View.Board.spAnimateMoveShift=function(e,a,i,o){function n(){0==--s&&o()}for(var s=0,r=[],d=i.down.length-1;d>=0;d--)r.push(i.down[d]);r.push(i.from),r.push(i.to);try{for(var d=1;d<r.length;d++){var l=r[d-1],c=a.getVCoord(l),h=a.getVCoord(r[d]),g=a.g.Coord[r[d]],p=Math.sqrt((c[0]-h[0])*(c[0]-h[0])+(c[1]-h[1])*(c[1]-h[1])),u=d==r.length-1&&p>2*t*1.1;s++,function(i,o,s){e.updateGadget(a.spGadgetId(e,l,"piece"),{base:{x:o[0],y:o[1]},"2d":{z:2*g[2]+4},"3d":{z:o[2],positionEasingUpdate:function(e){if(s){var a=(4*(.25-(e-.5)*(e-.5))*t*2+i[2])*this.SCALE3D;this.object3d.position.y=a}}}},800,function(){n()})}(c,h,u)}}catch(e){}}}();