(function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()})(function(){
var MODE_DEFAULT = 0;
var MODE_SELECT = 1;
var MOVE_LIMIT = 6;
var i, imax, j, jmax, mtx, mode = MODE_DEFAULT;
var stage, stats, timer, sprite, light;
var frameRate = 60;
var m,t,s,cubeMaterial,cubeMaterials = [];
var el = document.getElementById("three-stage");
var isDown, isDrag, dragPoint, startAngle, tween;
var sizeW = 21, sizeH = 7, rows = 11, cols = 11;
var panels = [];
var panelMatNormal = new THREE.MeshBasicMaterial({color: 0x5555FF, opacity: 0.6});
var panelMatActive = new THREE.MeshBasicMaterial({color: 0xFF5555, opacity: 0.6});
var cube, angle = 0;
//ステージ
var stage = new FieldStage(el, 520, 390, {x:sizeW, y:sizeH, z:sizeW}, false);
stage.distance = 1000;
//get material
function getMaterial(texture, texture2, isBasic){
if(stage.useWebgl() && !isBasic){
return new THREE.MeshLambertMaterial({map: texture});
}
return new THREE.MeshBasicMaterial({map: (stage.useWebgl() ? texture : texture2 || texture)});
};
// reset all panels
function clearSelectPanel(){
var i,j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
switch(mode){
case MODE_SELECT:
panels[i][j].materials[0] = panelMatNormal;
break;
default:
panels[i][j].materials[0] = panelMatNormal;
panels[i][j].visible = false;
break;
}
}
}
};
//cycle engine
function update(){
TWEEN.update();
light.position.x = cube.getMesh().position.x;
light.position.y = cube.getMesh().position.y + sizeW;
light.position.z = cube.getMesh().position.z;
stage.update(cube.getMesh().position);
stats.update();
stage.getRenderer().render(stage.getScene(), stage.getCamera());
};
//Cubeの各面のマテリアル
//No.1
m = getMaterial(THREE.ImageUtils.loadTexture(\'images/texture001.png\'));
t = getMaterial(THREE.ImageUtils.loadTexture(\'images/texture001_top.png\'));
cubeMaterials.push([
m, // right
m, // left
t, // top
m, // bottom
m, // back
m // front
]);
//No.2
t = getMaterial(THREE.ImageUtils.loadTexture(\'images/texture002_top.png\'));
cubeMaterials.push([
m, // right
m, // left
t, // top
m, // bottom
m, // back
m // front
]);
//No.3
t = getMaterial(THREE.ImageUtils.loadTexture(\'images/texture003_top.png\'));
cubeMaterials.push([
m, // right
m, // left
t, // top
m, // bottom
m, // back
m // front
]);
//No.4
t = getMaterial(THREE.ImageUtils.loadTexture(\'images/texture004_top.png\'));
cubeMaterials.push([
m, // right
m, // left
t, // top
m, // bottom
m, // back
m // front
]);
//Cube
m = getMaterial(
THREE.ImageUtils.loadTexture(\'images/cube001.png\'),
THREE.ImageUtils.loadTexture(\'images/cube002.png\'),
true
);
cubeMaterial = [
m, // right
m, // left
m, // top
m, // bottom
m, // back
m // front
];
//フィールドデータ生成
mtx = new FieldGenerator(cols, rows)
.add("h", [0.3, 0.4, 0.3])
.add("t", [0.3, 0.35, 0.3, 0.05])
.get();
var r2 = Math.floor(rows/2);
var c2 = Math.floor(cols/2);
mtx[r2][c2].h = 1;
mtx[r2][c2].t = 1;
stage.makeWalls(mtx, cubeMaterials);
//mr.cube
cube = new MrCube(sizeW, cubeMaterial, mtx);
cube.cellY = sizeH;
cube.setTo(c2, r2);
stage.getScene().addObject(cube.getMesh());
//ライティング (webgl only)
var defLight = new THREE.AmbientLight(0x333333);
stage.getScene().addLight(defLight);
light = new THREE.PointLight(0xFFFFFF, 3, 250);
light.position.set(0, 150, 0);
stage.getScene().addLight(light);
//インターフェイスパネル
var pm;
var pl = - cols * sizeW / 2 + sizeW / 2;
var pt = - rows * sizeW / 2 + sizeW / 2;
for (i = 0; i < rows; i++) {
panels[i] = [];
for (j = 0; j < cols; j++) {
pm = new THREE.Mesh(new THREE.Plane(sizeW, sizeW, 1, 1), panelMatNormal);
pm.rotation.x = -90 * Math.PI / 180;
pm.position.set(j * sizeW + pl, (mtx[i][j].h+1) * sizeH + 0.5, i * sizeW + pt);
pm.visible = false;
pm.isPanel = true;
pm.x = j;
pm.y = i;
panels[i][j] = pm;
stage.getScene().addObject(pm);
}
}
//mouse events
$(el).mousedown(function(e){
e.preventDefault();
dragPoint = new THREE.Vector2(e.pageX, e.pageY);
startAngle = stage.cameraAngle3.clone();
if(tween) tween.stop();
isDown = true;
});
$(el).mousewheel(function(e, delta){
e.preventDefault();
stage.distance += delta * 100;
stage.distance = Math.max(Math.min(stage.distance, 1800), 400);
});
$(document).mouseup(function(e){
clearSelectPanel();
if(!isDrag && !cube.isQueing){
var i, s, list;
list = stage.getRay().intersectScene(stage.getScene());
for(i in list){
if(list[i].object.isPanel){
s = list[i].object;
break;
}
}
if(s){
switch(mode){
case MODE_SELECT:
mode = MODE_DEFAULT;
if(s.visible){
cube.isQueing = true;
list = RouteSearch.search(
new RouteSearch.Point(cube.x, cube.y),
new RouteSearch.Point(s.x, s.y),
mtx,
1
);
list.shift();
cube.setQueAndPlay(list);
}
clearSelectPanel();
break;
default:
if(s.x == cube.x && s.y == cube.y){
mode = MODE_SELECT;
list = RouteSearch.searchByLimit(
new RouteSearch.Point(cube.x, cube.y),
MOVE_LIMIT,
mtx,
1
);
for(i in list){
panels[list[i].y][list[i].x].visible = true;
}
}
break;
}
}
}
var xx = stage.cameraAngle3.x + 45;
xx = Math.round(xx/90) * 90 - 45;
angle = ((360 - (xx - 45)) / 90) % 4;
if(angle < 0) angle += 4;
var yy = Math.floor(stage.cameraAngle3.y/10) * 10;
yy = Math.min(Math.max(yy, 15), 50);
tween = new TWEEN.Tween(stage.cameraAngle3)
.to({
x: xx,
y: yy,
z: xx
}, 1000)
.easing(TWEEN.Easing.Elastic.EaseOut)
.start();
isDown = false;
isDrag = false;
});
$(document).mousemove(function(e){
if(!isDrag){
clearSelectPanel();
var i, list, xx, yy, mouse3D;
xx = (e.offsetX || e.layerX) / stage.stageWidth;
yy = -(e.offsetY || e.layerY) / stage.stageHeight;
mouse3D = stage.getProjector().unprojectVector(
new THREE.Vector3(
xx * 2 - 1,
yy * 2 + 1,
0.5
),
stage.getCamera()
);
stage.getRay().direction = mouse3D.subSelf(stage.getCamera().position).normalize();
list = stage.getRay().intersectScene(stage.getScene());
for(i in list){
if(list[i].object.isPanel){
switch(mode){
case MODE_SELECT:
list[i].object.materials[0] = panelMatActive;
break;
default:
list[i].object.visible = true;
break;
}
break;
}
}
}
if(isDown){
stage.cameraAngle3.x = (dragPoint.x - e.pageX) / 5 + startAngle.x;
stage.cameraAngle3.y = (e.pageY - dragPoint.y) / 10 + startAngle.y;
stage.cameraAngle3.y = Math.min(Math.max(stage.cameraAngle3.y, 10), 55);
stage.cameraAngle3.z = (dragPoint.x - e.pageX) / 5 + startAngle.z;
if(!isDrag && Math.abs(dragPoint.x - e.pageX) + Math.abs(e.pageY - dragPoint.y) > 5){
isDrag = true;
}
}
});
//Stats SetUp
stats = new Stats();
stats.domElement.style.position = \'absolute\';
stats.domElement.style.top = \'0px\';
stats.domElement.style.zIndex = 100;
el.appendChild(stats.domElement);
document.getElementById("renderer").innerHTML = stage.useWebgl() ? "WebGLRenderer" : "CanvasRenderer" ;
//cycle engine start
timer = setInterval(update, 1000 / frameRate);
});