document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Mr.Cube
  3.  */
  4. var Cube = function(size, mtx){
  5.     this.size_ = size;
  6.     this.halfSize_ = this.size_ / 2;
  7.     this.mtx_ = mtx;
  8.     this.cols_ = (this.mtx_.length > 0) ? this.mtx_[0].length || 0 : 0 ;
  9.     this.rows_ = this.mtx_.length || 0;
  10.     this.halfWidth_ = this.cols_ * this.size_ / 2;
  11.     this.halfHeight_ = this.rows_ * this.size_ / 2;
  12.     this.x = 0;
  13.     this.y = 0;
  14.     this.isMove = false;
  15.     this.easeTime = 200;
  16.     this.isGoal = false;
  17.     this.goalPoint = null;
  18.  
  19.     var m = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/cube-texture.png\')});
  20.     var s = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/cube-texture-side.png\')});
  21.     var cubeMaterials = [
  22.         s, // right
  23.         s, // left
  24.         m, // top
  25.         m, // bottom
  26.         m, // back
  27.         m // front
  28.     ];
  29.  
  30.     this.mesh_ = new THREE.Mesh(
  31.         new THREE.Cube(
  32.             this.size_,
  33.             this.size_,
  34.             this.size_,
  35.             1,
  36.             1,
  37.             1,
  38.             cubeMaterials
  39.         ),
  40.         new THREE.MeshFaceMaterial()
  41.     );
  42.     this.mesh_.position.y = this.halfSize_;
  43.     this.setTo(this.x, this.y);
  44. };
  45. Cube.prototype.positionCheck_ = function(x, y){
  46.     if(0 <= x && x < this.cols_
  47.             && 0 <= y && y < this.rows_
  48.             && (this.x != x || this.y != y)
  49.             && this.mtx_[y][x] == 0){
  50.         return true;
  51.     }
  52.     return false;
  53. };
  54. Cube.prototype.goalCheck_ = function(){
  55.     if(!this.isGoal && this.goalPoint
  56.             && this.goalPoint.x == this.x && this.goalPoint.y == this.y){
  57.         this.isGoal = true;
  58.         alert("Congratulations!");
  59.     }
  60. };
  61. Cube.prototype.getMesh = function(){
  62.     return this.mesh_;
  63. };
  64. Cube.prototype.setTo = function(x, y){
  65.     if(!this.isMove && this.positionCheck_(x, y)){
  66.         this.x = x;
  67.         this.y = y;
  68.         this.mesh_.position.x = this.size_ * x - this.halfWidth_ + this.halfSize_;
  69.         this.mesh_.position.z = this.size_ * y - this.halfHeight_ + this.halfSize_;
  70.     }
  71. };
  72. Cube.prototype.moveTo = function(angle){
  73.     var pos = {x: this.x, y:this.y};
  74.     var tgKey, addAngle;
  75.     switch(angle){
  76.         case 0: //left
  77.             pos.x--;
  78.             tgKey = "z";
  79.             addAngle = 90 * Math.PI / 180;
  80.             break;
  81.         case 1: //up
  82.             pos.y--;
  83.             tgKey = "x";
  84.             addAngle = -90 * Math.PI / 180;
  85.             break;
  86.         case 2: //right
  87.             pos.x++;
  88.             tgKey = "z";
  89.             addAngle = -90 * Math.PI / 180;
  90.             break;
  91.         case 3: //down
  92.             pos.y++;
  93.             tgKey = "x";
  94.             addAngle = 90 * Math.PI / 180;
  95.             break;
  96.         default:
  97.             return false;
  98.     }
  99.     if(!this.isMove && this.positionCheck_(pos.x, pos.y)){
  100.         this.isMove = true;
  101.         this.x = pos.x;
  102.         this.y = pos.y;
  103.         (new TWEEN.Tween(this.mesh_.position))
  104.             .to({
  105.                 x: this.size_ * pos.x - this.halfWidth_ + this.halfSize_,
  106.                 z: this.size_ * pos.y - this.halfHeight_ + this.halfSize_
  107.             }, this.easeTime)
  108.             .easing(TWEEN.Easing.Quadratic.EaseInOut)
  109.             .onUpdate((function(tg, k, from, to){
  110.                 return function(e){
  111.                     tg[k] = to + (to - from) * e;
  112.                 };
  113.             })(this.mesh_.rotation, tgKey, this.mesh_.rotation[tgKey], this.mesh_.rotation[tgKey] + addAngle))
  114.             .onComplete((function(self){
  115.                 return function(){
  116.                     self.goalCheck_();
  117.                     self.isMove = false;
  118.                 };
  119.             })(this))
  120.             .start();
  121.     }
  122. };
  123.  
  124. /**
  125.  * main
  126.  */
  127. (function($){
  128.     var maze, stage, cube, light, stats, timer;
  129.     //フレームレート
  130.     var frameRate = 60;
  131.     //セルサイズ
  132.     var cubeSize = 40;
  133.     //カメラ追従対象ポイント
  134.     var cameraTarget = new THREE.Vector3;
  135.     //zoom tween
  136.     var tween, isZoomOut = false, minDistance = 500, maxDistance = 1800;
  137.     //Cubeの各面のマテリアル
  138.     var m = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/stage-texture.png\')});
  139.     var p = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/stage-plane-texture.png\')});
  140.     var cubeMaterials = [
  141.         m, // right
  142.         m, // left
  143.         m, // top
  144.         m, // bottom
  145.         m, // back
  146.         m // front
  147.     ];
  148.  
  149.     //zoom out
  150.     function zoomOut(bool){
  151.         if(isZoomOut != bool){
  152.             isZoomOut = bool;
  153.             if(tween) tween.stop();
  154.             tween = new TWEEN.Tween(stage);
  155.             tween.to({
  156.                     distance: (isZoomOut ? maxDistance : minDistance)
  157.                 }, 400)
  158.                 .easing(TWEEN.Easing.Quintic.EaseInOut)
  159.                 .start();
  160.         }
  161.     }
  162.  
  163.     //cycleEngine
  164.     function update(){
  165.         TWEEN.update();
  166.         stats.update();
  167.  
  168.         //カメラターゲットセット
  169.         cameraTarget.x = cube.getMesh().position.x;
  170.         cameraTarget.y = cube.getMesh().position.y;
  171.         cameraTarget.z = cube.getMesh().position.z;
  172.         stage.update(cameraTarget);
  173.  
  174.         stage.getRenderer().render(stage.getScene(), stage.getCamera());
  175.     };
  176.  
  177.     //key down event
  178.     function keyDownHandler(e){
  179.         e.preventDefault();
  180.         switch(e.keyCode){
  181.             case 37: //left
  182.                 cube.moveTo(0);
  183.                 break;
  184.             case 38: //up
  185.                 cube.moveTo(1);
  186.                 break;
  187.             case 39: //right
  188.                 cube.moveTo(2);
  189.                 break;
  190.             case 40: //down
  191.                 cube.moveTo(3);
  192.                 break;
  193.             case 16: //shift
  194.                 zoomOut(true)
  195.                 break;
  196.             default:
  197.                 break;
  198.         }
  199.     };
  200.  
  201.     //key up event
  202.     function keyUpHandler(e){
  203.         e.preventDefault();
  204.         switch(e.keyCode){
  205.             case 16: //shift
  206.                 zoomOut(false)
  207.                 break;
  208.             default:
  209.                 break;
  210.         }
  211.     };
  212.  
  213.     //rady event
  214.     $(function(){
  215.         //maze
  216.         maze = new Maze(15, 15);
  217.         var mtx = maze.getMatrix();
  218.         var st = maze.getStartPoint();
  219.  
  220.         //stage
  221.         stage = new Stage("three-stage", 520, 390, cubeSize);
  222.         stage.distance = minDistance;
  223.         stage.makeWalls(mtx, cubeMaterials, p);
  224.  
  225.         //cube
  226.         cube = new Cube(cubeSize, mtx);
  227.         cube.setTo(st.x, st.y);
  228.         cube.goalPoint = maze.getGoalPoint();
  229.         stage.getScene().addObject(cube.getMesh());
  230.  
  231.         //Stats SetUp
  232.         stats = new Stats();
  233.         stats.domElement.style.position = \'absolute\';
  234.         stats.domElement.style.top = \'0px\';
  235.         stats.domElement.style.zIndex = 100;
  236.         $(\'#three-stage\').append( stats.domElement );
  237.  
  238.         //cycleEngine
  239.         timer = setInterval(update, 1000 / frameRate);
  240.  
  241.         //Event
  242.         $(document).bind(\'keydown\', keyDownHandler);
  243.         $(document).bind(\'keyup\', keyUpHandler);
  244.     });
  245. })(jQuery);
');