const skycolor = 'LightBlue'; // sky color
const objectsize = 300; // size of player
const startRadius = 1000; // distance from centre we start the camera at
const maxRadius = startRadius * 10; // maximum distance from camera we render things
const skycolorObject = new THREE.Color ( skycolor.toLowerCase() );
const speed = 50;
function World()
{
var player = new THREE.Mesh ( new THREE.BoxGeometry ( objectsize, objectsize, objectsize ) );
//--------------Controls start-----------------
var left = 37;
var forward = 38;
var right = 39;
var backward = 40;
var velocity = new THREE.Vector2(0,0);
function updateControls()
{
if((ABWorld.camera.rotation.y >= -Math.PI / 4 && ABWorld.camera.rotation.y <= Math.PI / 4) &&
(ABWorld.camera.rotation.x >= -Math.PI / 2 && ABWorld.camera.rotation.x <= 0) &&
(ABWorld.camera.rotation.z >= -Math.PI / 4 && ABWorld.camera.rotation.z <= Math.PI / 4))
{
left = 37;
right = 39;
forward = 38;
backward = 40;
}
else if((ABWorld.camera.rotation.y >= -Math.PI / 4 && ABWorld.camera.rotation.y <= Math.PI / 4) &&
(ABWorld.camera.rotation.x >= -Math.PI && ABWorld.camera.rotation.x <= -Math.PI / 2) &&
(ABWorld.camera.rotation.z >= 3 * Math.PI / 4 || ABWorld.camera.rotation.z <= -3 * Math.PI / 4))
{
left = 39;
right = 37;
forward = 40;
backward = 38;
}
else if((ABWorld.camera.rotation.y >= 0 && ABWorld.camera.rotation.y <= Math.PI / 2) &&
(ABWorld.camera.rotation.x >= -Math.PI && ABWorld.camera.rotation.x <= 0) &&
(ABWorld.camera.rotation.z >= 0 && ABWorld.camera.rotation.z <= Math.PI))
{
left = 38;
right = 40;
forward = 39;
backward = 37;
}
else if((ABWorld.camera.rotation.y >= -Math.PI / 2 && ABWorld.camera.rotation.y <= 0) &&
(ABWorld.camera.rotation.x >= -Math.PI && ABWorld.camera.rotation.x <= 0) &&
(ABWorld.camera.rotation.z >= - Math.PI && ABWorld.camera.rotation.z <= 0))
{
left = 40;
right = 38;
forward = 37;
backward = 39;
}
}
function handleKeyDown (event)
{
var code = event.keyCode;
if (code == left)
{
velocity.x = - speed;
}
if (code == forward)
{
velocity.y = - speed;
}
if (code == right)
{
velocity.x = speed;
}
if (code == backward)
{
velocity.y = speed;
}
}
function handleKeyUp (event)
{
var code = event.keyCode;
if (code == left && velocity.x != speed)
{
velocity.x = 0;
}
if (code == forward && velocity.y != speed)
{
velocity.y = 0;
}
if (code == right && velocity.x != - speed)
{
velocity.x = 0;
}
if (code == backward && velocity.y != - speed)
{
velocity.y = 0;
}
}
//--------------Controls End-----------------
this.newRun = function()
{
// start a 3D scene:
ABWorld.init3d ( startRadius, maxRadius, skycolorObject );
// add the player to the scene:
ABWorld.scene.add(player);
document.addEventListener('keydown', handleKeyDown);//add arrow key listener
document.addEventListener('keyup', handleKeyUp);//add arrow key listener
};
this.nextStep = function()
{
updateControls();
player.position.x += velocity.x;
player.position.z += velocity.y;
}
}