const gridsize = 8;
const squaresize = 50;
const skycolor = 'lightblue';
const boxcolor = 'blue';
const objectsize = 300; // size of object
const startRadius = 500; // distance from centre we start the camera at
const maxRadius = startRadius * 10; // maximum distance from camera we render things
var shape = new THREE.BoxGeometry ( squaresize, squaresize, squaresize );
var selectorSide = new THREE.BoxGeometry (10, 1, 50);
var selectorRoof = new THREE.BoxGeometry (50, 1, 10);
var nextMove = new THREE.BoxGeometry(30, 1, 30);
var cylinder = new THREE.CylinderGeometry ( 20, 20, 20 )
var material = new THREE.MeshBasicMaterial ( { color: boxcolor.toLowerCase() } );
var theobject = new THREE.Mesh ( shape, material );
var theCylinderW1, theCylinderW2, theCylinderW3, theCylinderW4, theCylinderW5, theCylinderW6, theCylinderW7, theCylinderW8, theCylinderW9, theCylinderW10, theCylinderW11, theCylinderW12;
var theCylinderR1, theCylinderR2, theCylinderR3, theCylinderR4, theCylinderR5, theCylinderR6, theCylinderR7, theCylinderR8, theCylinderR9, theCylinderR10, theCylinderR11, theCylinderR12;
var whereTiles = [[" ",["W", theCylinderW1]," ",["W", theCylinderW2]," ",["W", theCylinderW3]," ",["W", theCylinderW4]],
[["W", theCylinderW5]," ",["W", theCylinderW6]," ",["W", theCylinderW7]," ",["W", theCylinderW8]," "],
[" ",["W", theCylinderW9]," ",["W", theCylinderW10]," ",["W", theCylinderW11]," ",["W", theCylinderW12]],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[["R", theCylinderR1]," ",["R", theCylinderR2]," ",["R", theCylinderR3]," ",["R", theCylinderR4]," "],
[" ",["R", theCylinderR5]," ",["R", theCylinderR6]," ",["R", theCylinderR7]," ",["R", theCylinderR8]],
[["R", theCylinderR9]," ",["R", theCylinderR10]," ",["R", theCylinderR11]," ",["R", theCylinderR12]," "]]
var whereSelect = [[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," ","S"]]
var whereLocation = [0,0];
var beforeMove = [];
var possibleLocations = [];
var enterCheck = false;
var theLeft, theRight, theBottom, theTop;
var leftCorner, rightCorner;
var tmp;
function displayBoard()
{
var evenOdd = 1;
for (var i = 0; i < gridsize; i += 1)
{
for (var j = 0; j < gridsize; j += 1)
{
if (evenOdd % 2 === 0)
{
colour = new THREE.MeshBasicMaterial( { color: 'black' } );
}
else
{
colour = new THREE.MeshBasicMaterial( { color: 'grey' } );
}
theCube = new THREE.Mesh(shape, colour);
theCube.position.x = -175 + (50 * j);
theCube.position.z = -175 +(50 * i);
ABWorld.scene.add(theCube);
evenOdd += 1;
console.log(i, evenOdd);
}
evenOdd += 1;
}
}
function displayTiles()
{
for (var i = 0; i < gridsize; i += 1)
{
for (var j = 0; j < gridsize; j += 1)
{
if (whereTiles[i][j] != ' ')
{
ABWorld.scene.remove(whereTiles[i][j][1]);
if (whereTiles[i][j][0] == 'W')
{
colour = new THREE.MeshBasicMaterial( { color: 'white' } );
}
else
{
colour = new THREE.MeshBasicMaterial( { color: 'red' } );
}
whereTiles[i][j][1] = new THREE.Mesh(cylinder, colour);
whereTiles[i][j][1].position.x = -175 + (50 * j);
whereTiles[i][j][1].position.z = -175 +(50 * i);
whereTiles[i][j][1].position.y = 25;
ABWorld.scene.add(whereTiles[i][j][1]);
}
//console.log((whereTiles[i][j]));
}
}
}
function displaySelector(){
ABWorld.scene.remove(theLeft);
colour = new THREE.MeshBasicMaterial( { color: 'blue' } );
theLeft = new THREE.Mesh(selectorSide, colour);
theLeft.position.x = -195 + (50 * whereLocation[0]);
theLeft.position.y = 25.5;
theLeft.position.z = -175 + (50 * whereLocation[1]);
ABWorld.scene.add(theLeft);
ABWorld.scene.remove(theRight);
colour = new THREE.MeshBasicMaterial( { color: 'blue' } );
theRight = new THREE.Mesh(selectorSide, colour);
theRight.position.x = -155 + (50 * whereLocation[0]);
theRight.position.y = 25.5;
theRight.position.z = -175 + (50 * whereLocation[1]);
ABWorld.scene.add(theRight);
ABWorld.scene.remove(theTop);
colour = new THREE.MeshBasicMaterial( { color: 'blue' } );
theTop = new THREE.Mesh(selectorRoof, colour);
theTop.position.x = -175 + (50 * whereLocation[0]);
theTop.position.y = 25.5;
theTop.position.z = -195 + (50 * whereLocation[1]);
ABWorld.scene.add(theTop);
ABWorld.scene.remove(theBottom);
colour = new THREE.MeshBasicMaterial( { color: 'blue' } );
theBottom = new THREE.Mesh(selectorRoof, colour);
theBottom.position.x = -175 + (50 * whereLocation[0]);
theBottom.position.y = 25.5;
theBottom.position.z = -155 + (50 * whereLocation[1]);
ABWorld.scene.add(theBottom);
}
function displayNextMove(direction)
{
possibleLocations = [];
colour = new THREE.MeshBasicMaterial( { color: 'yellow' } );
if (direction == "W")
{
console.log(whereTiles[whereLocation[1] + 1][whereLocation[0] + 1]);
if (whereLocation[0] != 7 && whereTiles[whereLocation[1] + 1][whereLocation[0] + 1] == " ")
{
leftCorner = new THREE.Mesh(nextMove, colour);
leftCorner.position.x = -175 + (50 * (whereLocation[0] + 1));
leftCorner.position.y = 25.5;
leftCorner.position.z = -175 + (50 * (whereLocation[1] + 1));
ABWorld.scene.add(leftCorner);
possibleLocations.push([whereLocation[0] + 1, whereLocation[1] + 1]);
} else if (whereLocation[0] != 7 && whereTiles[whereLocation[1] + 1][whereLocation[0] + 1][0] == "R" && whereTiles[whereLocation[1] + 2][whereLocation[0] + 2] == " ")
{
leftCorner = new THREE.Mesh(nextMove, colour);
leftCorner.position.x = -175 + (50 * (whereLocation[0] + 2));
leftCorner.position.y = 25.5;
leftCorner.position.z = -175 + (50 * (whereLocation[1] + 2));
ABWorld.scene.add(leftCorner);
possibleLocations.push([whereLocation[0] + 2, whereLocation[1] + 2]);
}
if (whereLocation[0] != 0 && whereTiles[whereLocation[1] + 1][whereLocation[0] - 1] == " ")
{
rightCorner = new THREE.Mesh(nextMove, colour);
rightCorner.position.x = -175 + (50 * (whereLocation[0] - 1));
rightCorner.position.y = 25.5;
rightCorner.position.z = -175 + (50 * (whereLocation[1] + 1));
ABWorld.scene.add(rightCorner);
possibleLocations.push([whereLocation[0] - 1, whereLocation[1] + 1]);
} else if (whereLocation[0] > 1 && whereTiles[whereLocation[1] + 1][whereLocation[0] - 1][0] == "R" && whereTiles[whereLocation[1] + 2][whereLocation[0] - 2] == " ")
{
rightCorner = new THREE.Mesh(nextMove, colour);
rightCorner.position.x = -175 + (50 * (whereLocation[0] - 2));
rightCorner.position.y = 25.5;
rightCorner.position.z = -175 + (50 * (whereLocation[1] + 2));
ABWorld.scene.add(rightCorner);
possibleLocations.push([whereLocation[0] - 2, whereLocation[1] + 2]);
}
}
else if (direction == "R")
{
if (whereLocation[0] != 7 && whereTiles[whereLocation[1] - 1][whereLocation[0] + 1] == " ")
{
leftCorner = new THREE.Mesh(nextMove, colour);
leftCorner.position.x = -175 + (50 * (whereLocation[0] + 1));
leftCorner.position.y = 25.5;
leftCorner.position.z = -175 + (50 * (whereLocation[1] - 1));
ABWorld.scene.add(leftCorner);
possibleLocations.push([whereLocation[0] + 1, whereLocation[1] - 1]);
} else if (whereLocation[0] != 7 && whereTiles[whereLocation[1] - 1][whereLocation[0] + 1][0] == "W" && whereTiles[whereLocation[1] - 2][whereLocation[0] + 2] == " ")
//else if (whereLocation[0] != 7 && whereTiles[whereLocation[1] + 1][whereLocation[0] + 1][0] == "R" && whereTiles[whereLocation[1] + 2][whereLocation[0] + 2] == " ")
{
leftCorner = new THREE.Mesh(nextMove, colour);
leftCorner.position.x = -175 + (50 * (whereLocation[0] + 2));
leftCorner.position.y = 25.5;
leftCorner.position.z = -175 + (50 * (whereLocation[1] - 2));
ABWorld.scene.add(leftCorner);
possibleLocations.push([whereLocation[0] + 2, whereLocation[1] - 2]);
}
if (whereLocation[0] != 0 && whereTiles[whereLocation[1] - 1][whereLocation[0] - 1] == " ")
{
rightCorner = new THREE.Mesh(nextMove, colour);
rightCorner.position.x = -175 + (50 * (whereLocation[0] - 1));
rightCorner.position.y = 25.5;
rightCorner.position.z = -175 + (50 * (whereLocation[1] - 1));
ABWorld.scene.add(rightCorner);
possibleLocations.push([whereLocation[0] - 1, whereLocation[1] - 1]);
} else if (whereLocation[0] != 0 && whereTiles[whereLocation[1] - 1][whereLocation[0] - 1][0] == "W" && whereTiles[whereLocation[1] - 2][whereLocation[0] - 2] == " ")
{
rightCorner = new THREE.Mesh(nextMove, colour);
rightCorner.position.x = -175 + (50 * (whereLocation[0] - 2));
rightCorner.position.y = 25.5;
rightCorner.position.z = -175 + (50 * (whereLocation[1] - 2));
ABWorld.scene.add(rightCorner);
possibleLocations.push([whereLocation[0] - 2, whereLocation[1] - 2]);
}
}
}
function keyHandler(event)
{
if (event.keyCode == 37)
{
moveSelector("left");
//console.log("left");
}
else if (event.keyCode == 38) {
moveSelector("up");
//console.log("down");
}
else if (event.keyCode == 39)
{
moveSelector("right");
//console.log("right");
}
else if (event.keyCode == 40)
{
moveSelector("down");
//console.log("up");
}
else if (event.keyCode == 13)
{
movePossible();
}
}
function moveSelector(input)
{
//console.log(input);
if (input == "left")
{
if (whereLocation[0] > 0 && whereLocation[0] <= 8 )
{
whereLocation[0] -= 1;
}
} else if (input == "right"){
if (whereLocation[0] >= 0 && whereLocation[0] < 7 )
{
whereLocation[0] += 1;
}
} else if (input == "down"){
if (whereLocation[1] >= 0 && whereLocation[1] < 7 )
{
whereLocation[1] += 1;
}
} else if (input == "up"){
if (whereLocation[1] > 0 && whereLocation[1] <= 8 )
{
whereLocation[1] -= 1;
}
}
console.log(whereLocation);
displaySelector();
}
function movePossible()
{
if (enterCheck === true)
{
enterCheck = false;
var length = possibleLocations.length;
for (var i = 0; i < length; i++)
{
console.log(beforeMove, possibleLocations[i], whereLocation);
if(possibleLocations[i][0] == whereLocation[0] && possibleLocations[i][1] == whereLocation[1])
{
//console.log(whereTiles);
//console.log(Math.abs(beforeMove[0] - whereLocation[0]));
if (Math.abs(beforeMove[0] - whereLocation[0]) == 2)
{
var first = (Math.abs(beforeMove[0]) + Math.abs(whereLocation[0])) / 2;
var second = (Math.abs(beforeMove[1]) + Math.abs(whereLocation[1])) / 2;
var tmpColour = whereTiles[second][first][0];
ABWorld.scene.remove(whereTiles[second][first][1]);
whereTiles[second][first] = " ";
//var firstDifference = beforeMove[0] - whereLocation[0]
//var secondDifference = beforeMove[1] - whereLocation[1];
}
tmp = whereTiles[beforeMove[1]][beforeMove[0]];
whereTiles[beforeMove[1]][beforeMove[0]] = " ";
whereTiles[whereLocation[1]][whereLocation[0]] = tmp;
console.log(whereTiles);
displayTiles()
ABWorld.scene.remove(leftCorner);
ABWorld.scene.remove(rightCorner);
return;
}
}
//console.log(possibleLocations, beforeMove);
}
beforeMove = [whereLocation[0],whereLocation[1]];
ABWorld.scene.remove(leftCorner);
ABWorld.scene.remove(rightCorner);
if (whereTiles[whereLocation[1]][whereLocation[0]] != " ")
{
if(whereTiles[whereLocation[1]][whereLocation[0]][0] == "W")
{
displayNextMove("W");
enterCheck = true;
}
else if (whereTiles[whereLocation[1]][whereLocation[0]][0] == "R")
{
displayNextMove("R");
enterCheck = true;
}
}
}
AB.world.newRun = function()
{
ABWorld.init3d ( startRadius, maxRadius, skycolor );
displayBoard();
displayTiles();
displaySelector();
// add the object to the scene:
//ABWorld.scene.add ( theobject );
// Code for Three.js initial drawing of objects.
// Should include one of:
// ABWorld.init2d ( arguments );
// ABWorld.init3d ( arguments );
document.onkeydown = keyHandler;
};
AB.world.nextStep = function()
{
// Code for Three.js re-drawing of objects.
};
AB.world.endRun = function()
{
};