// Cloned by Mikey Dowling on 19 Feb 2019 from World "New World" by tuitef2
// Please leave this clone trail here.
var cam;
var meshFloor;
var food_list = [];
var keyboard = {};
var distance;
var player = { height:2.1, speed:0.5, turnSpeed:0.2}; //to get player position get cam position
var scene = new THREE.Scene(); //CREATE SCENE
var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/skyyy.png' );
scene.background = texture;
var renderer = new THREE.WebGLRenderer();
var borderWall = [
"####################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"####################"
];
var createMap = function (scene, levelDefinition) {
var map = {};
map.bottom = -(levelDefinition.length - 1);
map.top = 0;
map.left = 0;
map.right = 0;
map.numDots = 0;
map.pacmanSpawn = null;
map.ghostSpawn = null;
var x, y;
for (var row = 0; row < levelDefinition.length; row++) {
// Set the coordinates of the map so that they match the
// coordinate system for objects.
y = -row;
map[y] = {};
// Get the length of the longest row in the level definition.
var length = Math.floor(levelDefinition[row].length / 2);
//map.right = Math.max(map.right, length - 1);
map.right = Math.max(map.right, length);
// Skip every second element, which is just a space for readability.
for (var column = 0; column < levelDefinition[row].length; column += 2) {
x = Math.floor(column / 2);
var cell = levelDefinition[row][column];
var object = null;
if (cell === '#') {
object = createWall();
} else if (cell === '.') {
object = createDot();
map.numDots += 1;
} else if (cell === 'o') {
object = createPowerPellet();
} else if (cell === 'P') {
map.pacmanSpawn = new THREE.Vector3(x, y, 0);
} else if (cell === 'G') {
map.ghostSpawn = new THREE.Vector3(x, y, 0);
}
if (object !== null) {
object.position.set(x, y, 0);
map[y][x] = object;
scene.add(object);
}
}
}
map.centerX = (map.left + map.right) / 2;
map.centerY = (map.bottom + map.top) / 2;
return map;
};
var createWall = function () {
var wallGeometry = new THREE.BoxGeometry(1, 1, 1);
var wallMaterial = new THREE.MeshLambertMaterial({ color: 'red' });
return function () {
var wall = new THREE.Mesh(wallGeometry, wallMaterial);
wall.isWall = true;
return wall;
};
}();
var scenes = createScene();
var map = createMap(scenes, LEVEL);
function render(){
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
var createScene = function () {
var scene = new THREE.Scene();
// Add lighting
scene.add(new THREE.AmbientLight(0x888888));
var light = new THREE.SpotLight('white', 0.5);
light.position.set(0, 0, 50);
scene.add(light);}
function camera(){
cam = new THREE.PerspectiveCamera(90, window.innerWidth/window.innerHeight, 0.1, 1000);
cam.position.set(0, player.height, -5); //position.x,position.y,position.z
cam.lookAt(new THREE.Vector3(0,player.height,0));
}
function ground(){
var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/ground_texture1.png' );
// immediately use the texture for material creation
meshFloor = new THREE.Mesh(
new THREE.PlaneGeometry(400,400),
new THREE.MeshBasicMaterial({map:texture}));
meshFloor.rotation.x -= Math.PI / 2; // Rotate the plane 90 degrees as if you don't do this it'll be like a tv screen.
scene.add(meshFloor);
}
function movement(){
requestAnimationFrame(movement);
// Keyboard movement inputs
if(keyboard[87]){ // W key
if (cam.position.x<= 200 && cam.position.x>= -200){
cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
}
else{
cam.position.x += Math.sin(cam.rotation.y) * player.speed;
}
if(cam.position.z<= 100 && cam.position.z>= -100){
cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
}
else{
cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
}
}
if(keyboard[83]){ // S key
if (cam.position.x<= 200 && cam.position.x>= -200){
cam.position.x += Math.sin(cam.rotation.y) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
}
else{
cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
}
if(cam.position.z<= 100 && cam.position.z>= -100){
cam.position.x += Math.sin(cam.rotation.y) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
}
else{
cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
}
}
if(keyboard[65]){ // A key
// Redirect motion by 90 degrees
if (cam.position.x <= 200 && cam.position.x >= -200){
cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
}
else{
cam.position.x = Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
}
if(cam.position.z <= 100 && cam.position.z >= -100){
cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
}
else{
cam.position.z = -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
}
}
if(keyboard[68]){ // D key
if (cam.position.x<= 200 && cam.position.x>= -200){
cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
}
else{
cam.position.x = Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
}
if(cam.position.z<= 100 && cam.position.z>= -100){
cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
}
else{
cam.position.z = -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
}
}
// Keyboard turn inputs
if(keyboard[37]){ // left arrow key
cam.rotation.y -= player.turnSpeed;
}
if(keyboard[39]){ // right arrow key
cam.rotation.y += player.turnSpeed;
}
renderer.render(scene, cam);
}
function keyDown(event){
keyboard[event.keyCode] = true;
}
function keyUp(event){
keyboard[event.keyCode] = false;
}
function main(){
var scenes = createScene();
var map = createMap(scenes, LEVEL);
render();
camera();
ground();
movement();
}
window.addEventListener('keydown', keyDown);
window.addEventListener('keyup', keyUp);
window.onload = main;