/**
* Mr.Cube
*/
var Cube = function(size, mtx){
this.size_ = size;
this.halfSize_ = this.size_ / 2;
this.mtx_ = mtx;
this.cols_ = (this.mtx_.length > 0) ? this.mtx_[0].length || 0 : 0 ;
this.rows_ = this.mtx_.length || 0;
this.halfWidth_ = this.cols_ * this.size_ / 2;
this.halfHeight_ = this.rows_ * this.size_ / 2;
this.x = 0;
this.y = 0;
this.isMove = false;
this.easeTime = 300;
this.mesh_ = new THREE.Mesh(
new THREE.Cube(
this.size_,
this.size_,
this.size_,
1,
1,
1
),
new THREE.MeshBasicMaterial({color: 0x5555FF, opacity: .9})
);
this.mesh_.position.y = this.halfSize_;
this.setTo(this.x, this.y);
};
Cube.prototype.positionCheck_ = function(x, y){
if(0 <= x && x < this.cols_
&& 0 <= y && y < this.rows_
&& (this.x != x || this.y != y)
&& this.mtx_[y][x] == 0){
return true;
}
return false;
};
Cube.prototype.getMesh = function(){
return this.mesh_;
};
Cube.prototype.setTo = function(x, y){
if(!this.isMove && this.positionCheck_(x, y)){
this.x = x;
this.y = y;
this.mesh_.position.x = this.size_ * x - this.halfWidth_ + this.halfSize_;
this.mesh_.position.z = this.size_ * y - this.halfHeight_ + this.halfSize_;
}
};
Cube.prototype.moveTo = function(angle){
var pos = {x: this.x, y:this.y};
var tgKey, addAngle;
switch(angle){
case 0: //left
pos.x--;
tgKey = "z";
addAngle = 90 * Math.PI / 180;
break;
case 1: //up
pos.y--;
tgKey = "x";
addAngle = -90 * Math.PI / 180;
break;
case 2: //right
pos.x++;
tgKey = "z";
addAngle = -90 * Math.PI / 180;
break;
case 3: //down
pos.y++;
tgKey = "x";
addAngle = 90 * Math.PI / 180;
break;
default:
return false;
}
if(!this.isMove && this.positionCheck_(pos.x, pos.y)){
this.isMove = true;
this.x = pos.x;
this.y = pos.y;
(new TWEEN.Tween(this.mesh_.position))
.to({
x: this.size_ * pos.x - this.halfWidth_ + this.halfSize_,
z: this.size_ * pos.y - this.halfHeight_ + this.halfSize_
}, this.easeTime)
.easing(TWEEN.Easing.Quadratic.EaseInOut)
.onUpdate((function(tg, k, from, to){
return function(e){
tg[k] = to + (to - from) * e;
};
})(this.mesh_.rotation, tgKey, this.mesh_.rotation[tgKey], this.mesh_.rotation[tgKey] + addAngle))
.onComplete((function(self){
return function(){
self.isMove = false;
};
})(this))
.start();
}
};
/**
* main
*/
(function($){
var maze, stage, cube, stats, timer;
//フレームレート
var frameRate = 60;
//セルサイズ
var cubeSize = 40;
//Cubeの各面のマテリアル
var m = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture(\'images/texture001.png\')});
var cubeMaterials = [
m, // right
m, // left
m, // top
m, // bottom
m, // back
m // front
];
//cycleEngine
function update(){
TWEEN.update();
stage.update();
stage.getRenderer().render(stage.getScene(), stage.getCamera());
stats.update();
};
//key down event
function keyDownHandler(e){
e.preventDefault();
switch(e.keyCode){
case 37: //left
cube.moveTo(0);
break;
case 38: //up
cube.moveTo(1);
break;
case 39: //right
cube.moveTo(2);
break;
case 40: //down
cube.moveTo(3);
break;
default:
break;
}
};
//rady event
$(function(){
//maze
maze = new Maze(10, 10);
var mtx = maze.getMatrix();
var st = maze.getStartPoint();
//stage
stage = new Stage("three-stage", 520, 390, cubeSize);
stage.distance = 2400;
stage.makeWalls(mtx, cubeMaterials);
//cube
cube = new Cube(cubeSize, mtx);
cube.setTo(st.x, st.y);
stage.getScene().addObject(cube.getMesh());
//Stats SetUp
stats = new Stats();
stats.domElement.style.position = \'absolute\';
stats.domElement.style.top = \'0px\';
stats.domElement.style.zIndex = 100;
$(\'#three-stage\').append( stats.domElement );
//cycleEngine
timer = setInterval(update, 1000 / frameRate);
//Event
$(document).bind(\'keydown\', keyDownHandler);
});
})(jQuery);