jocly
Version:
Board games 3D/2D UI and AI web library
1 lines • 11.1 kB
JavaScript
exports.view=View={Game:{},Board:{},Move:{}},function(){function e(e,t,i){var o=0==t?0:Math.PI;return h&&(o+=-e*c),i&&(o=180*o/Math.PI),o}var t,i,o,a,r,n,s,d,h,c,l,f,p,u,v=2,m=v/5,g=.1;View.Game.fiarCoord2d=function(e,i){return[(e+.5)*t-o/2,((this.mOptions.height-1)/2-i)*t]},View.Game.fiarCoord3d=function(t,i,o){var a,s,f,p=0,u=o||0;if(f=(i+.5-n/2)*v*d,h){p=e(t,0,!0);var i=l*Math.cos(c/2)-m/2;i+=u,s=i*Math.cos(t*c)*d,a=i*Math.sin(t*c)*d}else a=(t+.5-r/2)*v*d,s=0;return[a,s,f,p]},View.Game.xdInit=function(w){function E(t,i,o,a){o.position.y=0,o.position.y=(i+.5-n/2)*v,o.position.x=0==a?-m/2:m/2,o.position.z=(t+.5-r/2)*v,o.rotation.y=e(t,a)}function y(t,i,o,a){o.position.y=(i+.5-n/2)*v;var i=l*Math.cos(c/2);i-=a*(m+g),o.position.x=i*Math.cos(t*c),o.position.z=i*Math.sin(t*c),o.rotation.y=e(t,a),a>0&&(o.scale.z=1-m*Math.tan(c/2))}function M(e){var t=this;return this.getResource("smoothedfilegeo|0|"+i+"/res/xd-view/meshes/stade-screen.js",function(i,o){for(var a=[],r=0;r<o.length;r++)if("mat.screen"==o[r].name){var n=o[r].clone();n.map=e,n.overdraw=!0,n.emissive={r:1,g:1,b:1},a.push(n)}else{var n=o[r].clone();n.shading=THREE.FlatShading,a.push(n)}var s=new THREE.Mesh(i,new THREE.MultiMaterial(a));s.visible=!1,t.objectReady(s)}),null}t=Math.ceil(12e3/Math.max(this.mOptions.width,this.mOptions.height)),o=t*this.mOptions.width,a=t*this.mOptions.height,i=this.mViewOptions.fullPath,h=this.mOptions.torus,d=1e3,r=this.mOptions.width,n=this.mOptions.height,c=2*Math.PI/r,l=v/2/Math.sin(c/2),f=h?3:2,p=h?4e4:2e4,u=1.5,s=26367;for(var T=0;T<f;T++)!function(e){w.createGadget("lightback"+e,{"3d":{type:"custom3d",create:function(){var t=new THREE.SpotLight(12303291,u);t.castShadow=!h,t.shadow.camera.near=h?30:15,t.shadow.camera.far=h?40:35,t.shadow.camera.fov=h?60:70,t.shadow.mapSize.width=4096,t.shadow.mapSize.height=4096;var i=new THREE.Object3D,o=new THREE.Object3D;return o.position.set(-p*Math.cos(e*(2*Math.PI)/f),0,-p*Math.sin(e*(2*Math.PI)/f)),i.add(o),t.target=o,i.add(t),i},x:p*Math.cos(e*(2*Math.PI)/f),y:p*Math.sin(e*(2*Math.PI)/f)}})}(T);var G=h?y:E;w.createGadget("board3d",{"3d":{type:"custommesh3d",create:function(e){function t(){if(0==--a){for(var t=0;t<d.length;t++)o.add(d[t]);e(o)}}var o=new THREE.Object3D,a=4,d=[],c=new THREE.MeshPhongMaterial({wireframe:!1,shading:THREE.FlatShading,color:s,specular:1118481,shininess:40}),l=0,f="smoothedfilegeo|"+l+"|"+i+"/res/xd-view/meshes/connect4cell.js";if(this.getResource(f,function(e,i){for(var o=0;o<2;o++)for(var a=0;a<n;a++)for(var s=0;s<r;s++){var h=new THREE.Mesh(e,c);h.receiveShadow=!1,h.castShadow=!0,G(s,a,h,o),d.push(h)}t()}),l=0,f="smoothedfilegeo|"+l+"|"+i+"/res/xd-view/meshes/connect4cell-ring-smoothed.js",this.getResource(f,function(e,i){for(var o=0;o<2;o++)for(var a=0;a<n;a++)for(var h=0;h<r;h++){var c=new THREE.Mesh(e,new THREE.MeshPhongMaterial({wireframe:!1,shading:THREE.SmoothShading,color:s,specular:3355443}));c.receiveShadow=!1,c.castShadow=!0,G(h,a,c,o),d.push(c)}t()}),l=0,f="smoothedfilegeo|"+l+"|"+i+"/res/xd-view/meshes/connect4cell-foot.js",this.getResource(f,function(e,i){if(!h)for(var o=0;o<2;o++){var a=new THREE.Mesh(e,c),s=0==o?r/2*v:-r/2*v;a.position.x=0,a.position.y=(-n/2+.5)*v,a.position.z=s,d.push(a);var l=new THREE.Mesh(new THREE.BoxGeometry(.6,v*n,.2),c);l.position.z=s,d.push(l)}t()}),h)for(var p=0;p<r;p++){var u=new THREE.Mesh(new THREE.BoxGeometry(1.4*m,m,1.2*v),c);G(p,-.5,u,.5),d.push(u)}else{var g=new THREE.Mesh(new THREE.BoxGeometry(.6,.2,v*r),c);g.position.y=-n/2*v,d.push(g)}return t(),null}}});for(var b=0;b<this.mOptions.height;b++)for(var x=0;x<this.mOptions.width;x++){var O=b*this.mOptions.width+x,R=this.fiarCoord2d(x,b);w.createGadget("cell#"+O,{"2d":{type:"sprite",x:R[0],y:R[1],z:2,file:i+"/res/sprites2d.png",clipx:0,clipy:0,clipwidth:200,clipheight:200,width:t,height:t}})}for(var x=0;x<this.mOptions.width;x++){var k=this;!function(e){var o=String.fromCharCode(65+e),a=k.fiarCoord2d(e,k.mOptions.height-1),s=k.fiarCoord3d(e,k.mOptions.height-.7,h?1:0),l=.2*d;if(h){s[3]+=90;var f=e*c;s[0]+=l*Math.cos(f),s[1]-=l*Math.sin(f)}else s[3]-=90,s[0]-=.2*d;w.createGadget("text#"+e,{"2d":{type:"element",x:a[0],y:a[1],z:3,initialClasses:"fiar-text",display:function(e,t,i){e.css({"font-size":.5*i+"pt","line-height":1*i+"px"}).text(o)}},"3d":{type:"custommesh3d",x:s[1],y:s[0],z:s[2]+v/2*d,rotate:s[3],create:function(){var e=this;return this.getResource("font|"+i+"/res/xd-view/fonts/helvetiker_regular.typeface.json",function(t){var i=new THREE.TextGeometry(""+o,{size:.6,height:.05,curveSegments:6,font:t}),a=new THREE.MeshPhongMaterial({color:16711680}),r=new THREE.Mesh(i,a);e.objectReady(r)}),null}}}),w.createGadget("clicker#"+e+"t",{"2d":{type:"element",x:(e+.5)*t-6e3,y:-t,z:4,width:t,height:k.mOptions.height*t},"3d":{type:"custommesh3d",create:function(t){var i=new THREE.Object3D,o=new THREE.BoxGeometry(.05*(2*g+m),v*n,v),a=new THREE.Mesh(o,new THREE.MeshPhongMaterial({wireframe:!0,color:16777215*Math.random(),shininess:10,transparent:!0,opacity:0}));a.position.z=(e+.5-r/2)*v,G(e,n/2-.5,a,0),i.add(a),t(i)}}}),w.createGadget("clicker#"+e+"b",{"2d":{type:"element",x:(e+.5)*t-6e3,y:k.mOptions.height/2*t,z:4,width:t,height:2*t},"3d":{type:"custommesh3d",create:function(t){var i=new THREE.Object3D,o=new THREE.BoxGeometry(1.1*(4*g+m),v,v),a=new THREE.Mesh(o,new THREE.MeshPhongMaterial({wireframe:!0,color:16777215*Math.random(),shininess:250,transparent:!0,opacity:0}));a.position.z=(e+.5-r/2)*v,G(e,0,a,0),i.add(a),t(i)}}})}(x)}var H=(r/2+2)*v*d;w.createGadget("videoa",{"3d":{type:"video3d",makeMesh:function(e){return M.call(this,e)},z:0,x:3e3,y:H,rotate:-155,scale:[5,5,5]}}),w.createGadget("videob",{"3d":{type:"video3d",makeMesh:function(e){return M.call(this,e)},z:0,x:3e3,y:-H,rotate:-25,scale:[5,5,5]}});for(var x=0;x<this.mOptions.width+1;x++)w.createGadget("filler#c"+x,{"2d":{type:"element",x:x*t-o/2,y:0,z:3,width:.05*t,height:(this.mOptions.height+.05)*t,css:{"background-color":"#0065d0"}}});for(var b=0;b<this.mOptions.height+1;b++)w.createGadget("filler#r"+b,{"2d":{type:"element",y:b*t-a/2,x:0,z:3,height:.05*t,width:(this.mOptions.width+.05)*t,css:{"background-color":"#0065d0"}}});if(h)for(var x=0;x<2;x++)w.createGadget("fillertorus#c"+x,{"2d":{type:"element",x:x*o-o/2,y:0,z:4,width:.08*t,height:(this.mOptions.height+.05)*t,initialClasses:0==x?"fiar-holes-left":"fiar-holes-right"}})};var w={},E=1;View.Game.fiarIsToken=function(e,t){return!!w[e+"/"+t]},View.Game.fiarRemoveToken=function(e,t){w[e+"/"+t]&&delete w[e+"/"+t]},View.Game.fiarMoveToken=function(e,t,i,o){var a=w[e+"/"+t];a&&(delete w[e+"/"+t],w[i+"/"+o]=a)},View.Game.fiarGetToken=function(e,o,a,r){var n=w[o+"/"+a];if(!n){n="token#"+E++;var s=this.fiarCoord2d(o,a),d=this.fiarCoord3d(o,a);e.createGadget(n,{"2d":{type:"sprite",x:s[0],y:s[1],z:1,file:i+"/res/sprites2d.png",clipx:0,clipy:0,clipwidth:200,clipheight:200,width:t,height:t},"3d":{type:"meshfile",file:i+"/res/xd-view/meshes/connect4-token.js",flatShading:!0,x:d[1],y:d[0],z:d[2],rotate:d[3],receiveShadow:!1}}),w[o+"/"+a]=n}if(arguments.length>3){var h=i+"/res/xd-view/meshes/connect4-yellow.png";1==r&&(h=i+"/res/xd-view/meshes/connect4-red.png"),e.updateGadget(n,{"2d":{clipx:1==r?200:400},"3d":{materials:{"token-mat":{map:h}}}})}return n},View.Game.xdBuildScene=function(e){for(var t=0;t<this.mOptions.width*this.mOptions.height;t++)e.updateGadget("cell#"+t,{base:{visible:!0}});e.updateGadget("board3d",{"3d":{visible:!0}}),e.updateGadget("lightside",{"3d":{visible:!0}});for(var i=0;i<f;i++)e.updateGadget("lightback"+i,{"3d":{visible:!0}});e.updateGadget("videoa",{"3d":{visible:!0,playerSide:1}}),e.updateGadget("videob",{"3d":{visible:!0,playerSide:-1}});for(var o=0;o<this.mOptions.width;o++)e.updateGadget("text#"+o,{base:{visible:this.mNotation}});for(var o=0;o<this.mOptions.width+1;o++)e.updateGadget("filler#c"+o,{"2d":{visible:!0}});for(var a=0;a<this.mOptions.height+1;a++)e.updateGadget("filler#r"+a,{"2d":{visible:!0}});if(h)for(var o=0;o<2;o++)e.updateGadget("fillertorus#c"+o,{"2d":{visible:!0}})},View.Board.xdDisplay=function(e,t){var o={};if(this.mFinished)for(var a=0;a<t.g.TuplesList.length;a++){for(var r=t.g.TuplesList[a],n={1:0,0:0,"-1":0},s=0;s<r.length;s++)n[this.board[r[s]]]++;if(n[1]==t.mOptions.lines||n[-1]==t.mOptions.lines)for(var s=0;s<r.length;s++)o[r[s]]=!0}for(var d=0;d<t.mOptions.height;d++)for(var h=0;h<t.mOptions.width;h++){var c=d*t.mOptions.width+h;if(0==this.board[c]){if(t.fiarIsToken(h,d)){var l=t.fiarGetToken(e,h,d);e.updateGadget(l,{base:{visible:!1}})}}else{var l=t.fiarGetToken(e,h,d,this.board[c]),f=t.fiarCoord2d(h,d),p=t.fiarCoord3d(h,d),u=i+"/res/xd-view/meshes/connect4";1==this.board[c]?u+="-red":u+="-yellow",c in o&&(u+="-star"),u+=".png",e.updateGadget(l,{base:{visible:!0},"2d":{x:f[0],y:f[1],clipy:c in o?200:0},"3d":{x:p[1],y:p[0],z:p[2],rotate:p[3],materials:{"token-mat":{map:u}}}})}}},View.Board.xdBuildHTStateMachine=function(e,t,i){function o(){for(var o=0;o<n.mMoves.length;o++){var a=n.mMoves[o];"+"==a.op?function(o){e.updateGadget("clicker#"+o+"t",{base:{visible:!0,click:function(){t.smQueueEvent("E_CLICK",{col:o,op:"+"})}}}),i.mOptions.remove||e.updateGadget("clicker#"+a.col+"b",{base:{visible:!0,click:function(){t.smQueueEvent("E_CLICK",{col:o,op:"+"})}}})}(a.col):"-"==a.op&&function(i){e.updateGadget("clicker#"+a.col+"b",{base:{visible:!0,click:function(){t.smQueueEvent("E_CLICK",{col:i,op:"-"})}}})}(a.col)}}function a(){for(var t=0;t<i.mOptions.width;t++)e.updateGadget("clicker#"+t+"t",{base:{visible:!1,click:null}}),e.updateGadget("clicker#"+t+"b",{base:{visible:!1,click:null}})}function r(t){"+"==t.op?n.fiarAnimateDrop(e,i,t.col,n.cols[t.col],function(){i.HumanMove({col:t.col,op:"+"})}):"-"==t.op&&n.fiarAnimateOut(e,i,t.col,n.cols[t.col]-1,function(){i.HumanMove({col:t.col,op:"-"})})}var n=this;t.smTransition("S_INIT","E_INIT","S_SELECT",[o]),t.smTransition("S_SELECT","E_CLICK","S_ANIMATE",[a,r]),t.smTransition(["S_SELECT","S_ANIMATE"],"E_END","S_DONE",[]),t.smEntering("S_DONE",[a])},View.Board.xdPlayedMove=function(e,t,i){"+"==i.op?this.fiarAnimateDrop(e,t,i.col,this.cols[i.col]-1,function(){t.MoveShown()}):"-"==i.op&&this.fiarAnimateOut(e,t,i.col,this.cols[i.col],function(){t.MoveShown()})},View.Board.fiarAnimateDrop=function(e,t,i,o,a){var r=t.fiarGetToken(e,i,o,this.mWho),s=t.fiarCoord2d(i,o),h=t.fiarCoord3d(i,o);e.updateGadget(r,{base:{visible:!0},"2d":{x:s[0],y:-13e3,clipy:0},"3d":{y:h[0],z:n/2*v*d}}),t.PlaySound("sound2"),e.updateGadget(r,{"2d":{y:s[1]},"3d":{z:h[2]}},400,function(){a()})},View.Board.fiarAnimateOut=function(e,t,i,o,a){function r(){if(0==--n){for(var e=0;e<s.length;e++){var i=s[e].from,o=s[e].to;t.fiarMoveToken(i.c,i.r,o.c,o.r)}a()}}var n=0,s=[],d=t.fiarGetToken(e,i,0,this.mWho),h=t.fiarCoord2d(i,0),c=t.fiarCoord3d(i,0);n++,e.updateGadget(d,{"2d":{x:h[0],y:13e3},"3d":{y:c[0],z:-13e3}},600,function(){e.removeGadget(d),t.fiarRemoveToken(i,0),r()});for(var l=1;l<=o;l++)!function(o){var a=t.fiarGetToken(e,i,o);if(a){s.push({from:{c:i,r:o},to:{c:i,r:o-1}});var d=t.fiarCoord2d(i,o-1),h=t.fiarCoord3d(i,o-1);n++,e.updateGadget(a,{"2d":{y:d[1]},"3d":{z:h[2]}},600,function(){r()})}}(l)}}();