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.mesh_ = new THREE.Mesh(
  15.         new THREE.Cube(
  16.             this.size_,
  17.             this.size_,
  18.             this.size_,
  19.             1,
  20.             1,
  21.             1
  22.         ),
  23.         new THREE.MeshBasicMaterial({color: 0x6666FF, opacity: .9})
  24.     );
  25.     this.mesh_.position.y = this.halfSize_;
  26.     this.setTo(this.x, this.y);
  27. };
  28. Cube.prototype.positionCheck_ = function(x, y){
  29.     if(0 <= x && x < this.cols_
  30.             && 0 <= y && y < this.rows_
  31.             && this.mtx_[y][x] == 0){
  32.         return true;
  33.     }
  34.     return false;
  35.  
  36. };
  37. Cube.prototype.getMesh = function(){
  38.     return this.mesh_;
  39. };
  40. Cube.prototype.setTo = function(x, y){
  41.     if(this.positionCheck_(x, y)){
  42.         this.x = x;
  43.         this.y = y;
  44.         this.mesh_.position.x = this.size_ * x - this.halfWidth_ + this.halfSize_;
  45.         this.mesh_.position.z = this.size_ * y - this.halfHeight_ + this.halfSize_;
  46.     }
  47. };
  48.  
  49.  
  50. /**
  51.  * main
  52.  */
  53. (function($){
  54.     var maze, stage, cube, stats, timer;
  55.     //フレームレート
  56.     var frameRate = 60;
  57.     //セルサイズ
  58.     var cubeSize = 40;
  59.     //Cubeの各面のマテリアル
  60.     var m = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/texture001.png\')});
  61.     var cubeMaterials = [
  62.         m, // right
  63.         m, // left
  64.         m, // top
  65.         m, // bottom
  66.         m, // back
  67.         m // front
  68.     ];
  69.  
  70.     //cycleEngine
  71.     function update(){
  72.         stage.update();
  73.         stage.getRenderer().render(stage.getScene(), stage.getCamera());
  74.  
  75.         stats.update();
  76.     };
  77.  
  78.     //key down event
  79.     function keyDownHandler(e){
  80.         e.preventDefault();
  81.         switch(e.keyCode){
  82.             case 37: //left
  83.                 cube.setTo(cube.x - 1, cube.y);
  84.                 break;
  85.             case 38: //up
  86.                 cube.setTo(cube.x, cube.y - 1);
  87.                 break;
  88.             case 39: //right
  89.                 cube.setTo(cube.x + 1, cube.y);
  90.                 break;
  91.             case 40: //down
  92.                 cube.setTo(cube.x, cube.y + 1);
  93.                 break;
  94.             default:
  95.                 break;
  96.         }
  97.     };
  98.  
  99.     //rady event
  100.     $(function(){
  101.         //maze
  102.         maze = new Maze(10, 10);
  103.         var mtx = maze.getMatrix();
  104.         var st = maze.getStartPoint();
  105.  
  106.         //stage
  107.         stage = new Stage("three-stage", 520, 390, cubeSize);
  108.         stage.distance = 2400;
  109.         stage.makeWalls(mtx, cubeMaterials);
  110.  
  111.         //cube
  112.         cube = new Cube(cubeSize, mtx);
  113.         cube.setTo(st.x, st.y);
  114.         stage.getScene().addObject(cube.getMesh());
  115.  
  116.         //Stats SetUp
  117.         stats = new Stats();
  118.         stats.domElement.style.position = \'absolute\';
  119.         stats.domElement.style.top = \'0px\';
  120.         stats.domElement.style.zIndex = 100;
  121.         $(\'#three-stage\').append( stats.domElement );
  122.  
  123.         //cycleEngine
  124.         timer = setInterval(update, 1000 / frameRate);
  125.  
  126.         //Event
  127.         $(document).bind(\'keydown\', keyDownHandler);
  128.     });
  129. })(jQuery);
');