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 = 300;
  16.     this.mesh_ = new THREE.Mesh(
  17.         new THREE.Cube(
  18.             this.size_,
  19.             this.size_,
  20.             this.size_,
  21.             1,
  22.             1,
  23.             1
  24.         ),
  25.         new THREE.MeshBasicMaterial({color: 0x5555FF, opacity: .9})
  26.     );
  27.     this.mesh_.position.y = this.halfSize_;
  28.     this.setTo(this.x, this.y);
  29. };
  30. Cube.prototype.positionCheck_ = function(x, y){
  31.     if(0 <= x && x < this.cols_
  32.             && 0 <= y && y < this.rows_
  33.             && (this.x != x || this.y != y)
  34.             && this.mtx_[y][x] == 0){
  35.         return true;
  36.     }
  37.     return false;
  38. };
  39. Cube.prototype.getMesh = function(){
  40.     return this.mesh_;
  41. };
  42. Cube.prototype.setTo = function(x, y){
  43.     if(!this.isMove && this.positionCheck_(x, y)){
  44.         this.x = x;
  45.         this.y = y;
  46.         this.mesh_.position.x = this.size_ * x - this.halfWidth_ + this.halfSize_;
  47.         this.mesh_.position.z = this.size_ * y - this.halfHeight_ + this.halfSize_;
  48.     }
  49. };
  50. Cube.prototype.moveTo = function(angle){
  51.     var pos = {x: this.x, y:this.y};
  52.     var tgKey, addAngle;
  53.     switch(angle){
  54.         case 0: //left
  55.             pos.x--;
  56.             tgKey = "z";
  57.             addAngle = 90 * Math.PI / 180;
  58.             break;
  59.         case 1: //up
  60.             pos.y--;
  61.             tgKey = "x";
  62.             addAngle = -90 * Math.PI / 180;
  63.             break;
  64.         case 2: //right
  65.             pos.x++;
  66.             tgKey = "z";
  67.             addAngle = -90 * Math.PI / 180;
  68.             break;
  69.         case 3: //down
  70.             pos.y++;
  71.             tgKey = "x";
  72.             addAngle = 90 * Math.PI / 180;
  73.             break;
  74.         default:
  75.             return false;
  76.     }
  77.     if(!this.isMove && this.positionCheck_(pos.x, pos.y)){
  78.         this.isMove = true;
  79.         this.x = pos.x;
  80.         this.y = pos.y;
  81.         (new TWEEN.Tween(this.mesh_.position))
  82.             .to({
  83.                 x: this.size_ * pos.x - this.halfWidth_ + this.halfSize_,
  84.                 z: this.size_ * pos.y - this.halfHeight_ + this.halfSize_
  85.             }, this.easeTime)
  86.             .easing(TWEEN.Easing.Quadratic.EaseInOut)
  87.             .onUpdate((function(tg, k, from, to){
  88.                 return function(e){
  89.                     tg[k] = to + (to - from) * e;
  90.                 };
  91.             })(this.mesh_.rotation, tgKey, this.mesh_.rotation[tgKey], this.mesh_.rotation[tgKey] + addAngle))
  92.             .onComplete((function(self){
  93.                 return function(){
  94.                     self.isMove = false;
  95.                 };
  96.             })(this))
  97.             .start();
  98.     }
  99. };
  100.  
  101.  
  102. /**
  103.  * main
  104.  */
  105. (function($){
  106.     var maze, stage, cube, stats, timer;
  107.     //フレームレート
  108.     var frameRate = 60;
  109.     //セルサイズ
  110.     var cubeSize = 40;
  111.     //Cubeの各面のマテリアル
  112.     var m = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/texture001.png\')});
  113.     var cubeMaterials = [
  114.         m, // right
  115.         m, // left
  116.         m, // top
  117.         m, // bottom
  118.         m, // back
  119.         m // front
  120.     ];
  121.  
  122.     //cycleEngine
  123.     function update(){
  124.         TWEEN.update();
  125.         stage.update();
  126.         stage.getRenderer().render(stage.getScene(), stage.getCamera());
  127.  
  128.         stats.update();
  129.     };
  130.  
  131.     //key down event
  132.     function keyDownHandler(e){
  133.         e.preventDefault();
  134.         switch(e.keyCode){
  135.             case 37: //left
  136.                 cube.moveTo(0);
  137.                 break;
  138.             case 38: //up
  139.                 cube.moveTo(1);
  140.                 break;
  141.             case 39: //right
  142.                 cube.moveTo(2);
  143.                 break;
  144.             case 40: //down
  145.                 cube.moveTo(3);
  146.                 break;
  147.             default:
  148.                 break;
  149.         }
  150.     };
  151.  
  152.     //rady event
  153.     $(function(){
  154.         //maze
  155.         maze = new Maze(10, 10);
  156.         var mtx = maze.getMatrix();
  157.         var st = maze.getStartPoint();
  158.  
  159.         //stage
  160.         stage = new Stage("three-stage", 520, 390, cubeSize);
  161.         stage.distance = 2400;
  162.         stage.makeWalls(mtx, cubeMaterials);
  163.  
  164.         //cube
  165.         cube = new Cube(cubeSize, mtx);
  166.         cube.setTo(st.x, st.y);
  167.         stage.getScene().addObject(cube.getMesh());
  168.  
  169.         //Stats SetUp
  170.         stats = new Stats();
  171.         stats.domElement.style.position = \'absolute\';
  172.         stats.domElement.style.top = \'0px\';
  173.         stats.domElement.style.zIndex = 100;
  174.         $(\'#three-stage\').append( stats.domElement );
  175.  
  176.         //cycleEngine
  177.         timer = setInterval(update, 1000 / frameRate);
  178.  
  179.         //Event
  180.         $(document).bind(\'keydown\', keyDownHandler);
  181.     });
  182. })(jQuery);
');