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