//
// Load renderer
//
var renderer = new THREE.WebGLRenderer({
antialias: true
});
//
// Load options
//
createOption({
name: "Field of View",
id: "graphics_fov",
category: "Graphics",
type: "range",
default: 60,
min: 10,
max: 60,
step: 10,
callback: function(val) {
camera.fov = val;
camera.updateProjectionMatrix();
}
});
createOption({
name: "Mipmaps",
id: "graphics_anisotropy",
category: "Graphics",
type: "range",
default: 4,
min: 1,
max: renderer.getMaxAnisotropy(),
step: 1,
callback: function(val) {
$.each(Object.keys(meshList), function(i, uuid) {
if (meshList[uuid].material.map != null) {
meshList[uuid].material.map.anisotropy = val;
meshList[uuid].material.map.needsUpdate = true;
}
});
grassPlane.material.map.anisotropy = val;
grassPlane.material.map.needsUpdate = true;
}
});
createOption({
name: "Terrain Detail",
id: "graphics_terrain_detail",
category: "Graphics",
type: "range",
default: 32,
min: 4,
max: 128,
step: 1,
callback: function(val) {
scene.remove(grassPlane);
grassPlane = new WAFFLE.Mesh(new THREE.PlaneBufferGeometry(4096, 4096, val, val), grassPlane.material);
grassPlane.applyMatrix(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
grassPlane.position.z = player.position.z;
grassPlane.position.x = player.position.x;
grassPlane.overdraw = true;
grassPlane.castShadow = false;
grassPlane.receiveShadow = true;
grassPlane.type = 'MeshGroundPlane';
scene.add(grassPlane);
}
});
createOption({
name: "Mirror Detail",
id: "graphics_mirror_detail",
category: "Graphics",
type: "range",
default: 8,
min: 7,
max: 11,
step: 1,
callback: function(val) {
var detail = Math.pow(2, val);
$.each(Object.keys(mirrorList), function(i, uuid) {
mirrorList[uuid].renderTarget.setSize(detail, detail);
});
}
});
createOption({
name: "Shadow Detail",
id: "graphics_shadow_detail",
category: "Graphics",
type: "range",
default: 11,
min: 11,
max: 13,
step: 1,
callback: function(val) {
// Sunlight
var detail = Math.pow(2, val);
sunLight.shadow.mapSize.width = detail;
sunLight.shadow.mapSize.height = detail;
sunLight.shadow.map.dispose();
sunLight.shadow.map = null;
// PointLights
var pointLightDetail = Math.pow(2, val - 2);
$.each(Object.keys(lightList), function(i, uuid) {
lightList[uuid].shadow.mapSize.width = pointLightDetail;
lightList[uuid].shadow.mapSize.height = pointLightDetail;
lightList[uuid].shadow.map.dispose();
lightList[uuid].shadow.map = null;
});
}
});
createOption({
name: "Point Light Shadows",
id: "graphics_shadow_pointlight",
category: "Graphics",
type: "checkbox",
default: false,
callback: function(val) {
$.each(Object.keys(lightList), function(i, uuid) {
lightList[uuid].castShadow = val;
});
}
});
createOption({
name: "Wireframe",
id: "graphics_wireframe",
category: "Graphics",
type: "checkbox",
default: false,
callback: function(val) {
$.each(Object.keys(meshList), function(i, uuid) {
meshList[uuid].material.wireframe = val;
});
}
});
createOption({
name: "HD Textures",
id: "graphics_hdtextures",
category: "Graphics",
type: "checkbox",
default: false,
callback: function(val) {
texturePath = 'Resources/Texture/' + (val ? 'High Quality' : 'Low Quality') + '/';
$.each(textureList, function(index, value) {
if (/\$textures\//.test(value.sourceFile)) {
WAFFLE.AssetLoader.queueAsync(value.sourceFile);
}
});
WAFFLE.AssetLoader.loadAsync();
}
});
createOption({
name: "Normal Mapping",
id: "graphics_normal_mapping",
category: "Graphics",
type: "checkbox",
default: false,
callback: function(val) {
if (val) {
grassPlane.material.normalMap = getTexture(localMap.ground.normalMap);
grassPlane.material.needsUpdate = true;
$.each(Object.keys(meshList), function(index, uuid) {
meshList[uuid].material.normalMap = getTexture(meshList[uuid].properties.normalMap);
meshList[uuid].material.needsUpdate = true;
});
} else {
grassPlane.material.normalMap = null;
grassPlane.material.needsUpdate = true;
$.each(Object.keys(meshList), function(index, uuid) {
meshList[uuid].material.normalMap = null;
meshList[uuid].material.needsUpdate = true;
});
}
}
});
createOption({
name: "Ambient Occlusion Mapping",
id: "graphics_occlusion_mapping",
category: "Graphics",
type: "checkbox",
default: false,
callback: function(val) {
if (val) {
grassPlane.material.aoMap = getTexture(localMap.ground.aoMap);
grassPlane.material.needsUpdate = true;
$.each(Object.keys(meshList), function(index, uuid) {
meshList[uuid].material.aoMap = getTexture(meshList[uuid].properties.aoMap);
meshList[uuid].material.needsUpdate = true;
});
} else {
grassPlane.material.aoMap = null;
grassPlane.material.needsUpdate = true;
$.each(Object.keys(meshList), function(index, uuid) {
meshList[uuid].material.aoMap = null;
meshList[uuid].material.needsUpdate = true;
});
}
}
});
createOption({
name: "Geometry Normal Computation",
id: "graphics_normal_computation",
category: "Graphics",
type: "checkbox",
default: false
});
createOption({
name: "Distant Mountains",
id: "graphics_distant_mountains",
category: "Graphics",
type: "checkbox",
default: true,
callback: function(val) {
if (val) {
grassPlane.material.displacementScale = 40;
grassPlane.needsUpdate = true;
} else {
grassPlane.material.displacementScale = 0;
grassPlane.needsUpdate = true;
}
}
});
createOption({
name: "Master Volume",
id: "volume_master",
category: "Sound",
type: "range",
default: 1,
min: 0,
max: 1,
step: 0.01,
callback: function(val) {
audioListener.setMasterVolume(val);
}
});
createOption({
name: "Ambient Sound Volume",
id: "volume_ambient",
category: "Sound",
type: "range",
default: 0.6,
min: 0,
max: 1,
step: 0.01,
callback: function(val) {
ambientSound.setVolume(val);
}
});
createOption({
name: "Spawn menu blur",
id: "interface_spawn_blur",
category: "Interface",
type: "checkbox",
default: false
});
createOption({
name: "Display chat",
id: "interface_display_chat",
category: "Interface",
type: "checkbox",
default: true
});
createOption({
name: "Prevent objects falling under map",
id: "gameplay_prevent_under_map",
category: "Gameplay",
type: "checkbox",
default: true
});
//
// Global variables
//
var isDebug = false;
var texturePath = 'Resources/Texture/Very Low Quality/';
var audioPath = 'resources/audio/';
//
// Load assets
//
WAFFLE.AssetLoader.queue(['lib/detector.js', 'lib/orbitControls.js', 'shader/CopyShader.js', 'shader/HorizontalBlurShader.js', 'shader/VerticalBlurShader.js', 'lib/three.effectComposer.js', 'lib/cannon.js'], 0);
if (isDebug) {
WAFFLE.AssetLoader.queue('js/core_debug.js', 0);
}
WAFFLE.AssetLoader.queue(['lib/three.renderPass.js', 'lib/three.maskPass.js', 'lib/three.shaderPass.js', 'js/core_post_processing.js', 'js/control_transform.js', 'js/core_mesh.js', 'js/core_vehicle.js', '$textures/Grass.jpg', '$textures/Grass_Displacement.jpg', 'Resources/Shader/default_vertex.shader', 'Resources/Shader/gradient_fragment.shader'], 1, null, function() {
texturePath = 'Resources/Texture/' + (getOption('graphics_hdtextures') ? 'High Quality' : 'Low Quality') + '/';
});
WAFFLE.AssetLoader.queue('lib/stats.min.js', 1, null, function() {
if (isDebug) {
loadStats();
}
});
WAFFLE.AssetLoader.queue('$textures/Default.png', 2, null, function() {
textureList[ "$textures/Default.png" ].wrapS = THREE.RepeatWrapping;
textureList[ "$textures/Default.png" ].wrapT = THREE.RepeatWrapping;
textureList[ "$textures/Default.png" ].repeat.set(64, 64);
//
// Diffuse Maps
//
WAFFLE.AssetLoader.queueAsync('$textures/Grass.jpg', {useLoadingPlaceholder: false});
//
// Displacement Maps
//
WAFFLE.AssetLoader.queueAsync('$textures/Grass_Displacement.jpg', {useLoadingPlaceholder: false});
});
WAFFLE.AssetLoader.queue('lib/ace/ace.js', 2, null, function() {
ace.config.set("basePath", "lib/ace");
});
WAFFLE.AssetLoader.queue('js/menu_spawn.js', 2, null, function() {
populateSpawnMenu();
});
WAFFLE.AssetLoader.queue(['js/core_main.js', 'lib/blockly/blockly_compressed.js', 'lib/farbtastic.js', 'lib/randomColor.js', 'lib/ace/mode_javascript.js', 'lib/ace/ext_searchbox.js', 'lib/TransformControls.js'], 3);
WAFFLE.AssetLoader.queue(['lib/blockly/javascript_compressed.js', 'lib/blockly/en.js'], 4);
WAFFLE.AssetLoader.queue(['lib/blockly/blocks_compressed.js', 'lib/jquery-ui-1.12.1.min.js'], 5);
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
WAFFLE.AssetLoader.queue('js/controls_touch.js', 6);
}
WAFFLE.AssetLoader.queue(['js/ui_control_imagelist.js', 'js/core_gui.js', 'js/core_tool.js', 'js/core_connection_authentication.js', 'js/core_connection_world.js', 'js/core_player.js', 'js/menu_spawn_lighting.js', 'js/menu_maps.js', 'js/core_pointlight.js', 'js/loader_world_object.js', 'js/blockly_control.js', 'js/blockly_event.js', 'js/blockly_interface.js', 'js/blockly_movement.js', 'js/blockly_variable.js', 'js/blockly_visual.js', 'js/blockly_lighting.js', 'js/blockly_colour.js', 'js/blockly_styles.js', 'js/blockly_elements.js', 'js/blockly_procedures.js', 'js/ui_scripting.js', 'js/loader_scene.js', 'js/ui_editor.js', 'js/core_raycasting.js', 'lib/SHA256.js'], 6);
// Preload icons
WAFFLE.AssetLoader.queue(['resources/icon/backArrow.svg', 'resources/icon/map_flatgrass.png', 'resources/icon/map_flatsand.png', 'resources/icon/cogs.png', 'resources/icon/script.svg', 'resources/icon/steeringWheel.svg', 'resources/icon/arrows.svg', 'resources/icon/bin.svg', 'resources/icon/rotate.svg', 'resources/icon/scale.svg', 'resources/icon/block.svg', 'resources/icon/grid.svg', 'resources/icon/object.svg', 'resources/icon/bulb.svg', 'resources/icon/tool_chrome.png', 'resources/icon/tool_default.png', 'resources/icon/tool_delete.png', 'resources/icon/tool_emissive.png', 'resources/icon/tool_gloss.png', 'resources/icon/tool_metalness.png', 'resources/icon/tool_mirror.png', 'resources/icon/tool_reflective.png', 'resources/icon/tool_texture.png', 'resources/icon/tool_vehicle.png'], 6, {
preloadIcon: true
});
//
// Load audio
//
WAFFLE.AssetLoader.queueAsync(audioPath + 'ambience_forest.mp3', null, function() {
ambientSound.setBuffer(audioBufferList['ambience_forest']);
ambientSound.setLoop(true);
ambientSound.setVolume(getOption("volume_ambient"));
scene.add(ambientSound);
ambientSound.play();
});
WAFFLE.AssetLoader.queueAsync(audioPath + 'steps_stone.mp3', null, function() {
vehicle.vehicleSound = new THREE.Audio(audioListener);
vehicle.vehicleSound.setBuffer(audioBufferList['steps_stone']);
vehicle.vehicleSound.setLoop(true);
scene.add(vehicle.vehicleSound);
});
WAFFLE.AssetLoader.queueAsync(audioPath + 'car_acceleration.mp3');
WAFFLE.AssetLoader.queueAsync(audioPath + 'car_horn.mp3', null, function() {
vehicle.vehicleHornSound = new THREE.Audio(audioListener);
vehicle.vehicleHornSound.setBuffer(audioBufferList['car_horn']);
scene.add(vehicle.vehicleHornSound);
});
//
// Load models
//
WAFFLE.AssetLoader.queue('Resources/Model/HouseA.json', 4, {
name: "House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/HouseA.png",
normalMap: "$textures/HouseA_Normal.jpg",
aoMap: "$textures/HouseA_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/HouseB.json', 4, {
name: "House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/HouseB.png",
normalMap: "$textures/HouseB_Normal.jpg",
aoMap: "$textures/HouseB_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/HouseC.json', 4, {
name: "House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/HouseC.png",
normalMap: "$textures/HouseC_Normal.jpg",
aoMap: "$textures/HouseC_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/HouseD.json', 4, {
name: "House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/HouseD.png",
normalMap: "$textures/HouseD_Normal.jpg",
aoMap: "$textures/HouseD_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/HouseE.json', 4, {
name: "House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/HouseE.png",
normalMap: "$textures/HouseE_Normal.jpg",
aoMap: "$textures/HouseE_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/FarmHouse.json', 4, {
name: "Farm House",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/FarmHouse.png",
normalMap: "$textures/FarmHouse_Normal.jpg",
aoMap: "$textures/FarmHouse_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/Church.json', 4, {
name: "Church",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/Church.png",
normalMap: "$textures/Church_Normal.jpg",
aoMap: "$textures/Church_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/Tower.json', 4, {
name: "Tower",
scale: new THREE.Vector3(1.6, 1.6, 1.6),
map: "$textures/Tower.png",
normalMap: "$textures/Tower_Normal.jpg",
aoMap: "$textures/Tower_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/Well.json', 4, {
name: "Well",
scale: new THREE.Vector3(1.5, 1.5, 1.5),
map: "$textures/Well.png",
normalMap: "$textures/Well_Normal.jpg",
aoMap: "$textures/Well_Occlusion.jpg",
mass: 100
});
WAFFLE.AssetLoader.queue('Resources/Model/Hut.json', 4, {
name: "Hut",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/Hut.png",
normalMap: "$textures/Hut_Normal.jpg",
aoMap: "$textures/Hut_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/WitchHut.json', 4, {
name: "Witch Hut",
scale: new THREE.Vector3(2.8, 2.8, 2.8),
map: "$textures/WitchHut.png",
normalMap: "$textures/WitchHut_Normal.jpg",
aoMap: "$textures/WitchHut_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/Pavillon.json', 4, {
name: "Pavillon",
scale: new THREE.Vector3(0.75, 0.75, 0.75),
map: "$textures/Pavillon.png",
normalMap: "$textures/Pavillon_Normal.jpg",
aoMap: "$textures/Pavillon_Occlusion.jpg",
mass: 1000
});
WAFFLE.AssetLoader.queue('Resources/Model/Waypoint.json', 4, {
name: "Waypoint",
scale: new THREE.Vector3(2, 2, 2),
map: "$textures/Waypoint.jpg",
normalMap: "$textures/Waypoint_Normal.jpg",
aoMap: "$textures/Waypoint_Occlusion.jpg",
mass: 10
});
WAFFLE.AssetLoader.queue(['Resources/Model/RockA.json', 'Resources/Model/RockB.json', 'Resources/Model/RockC.json'], 4, {
name: "Rock",
scale: new THREE.Vector3(2, 2, 2),
map: "$textures/Rock.png",
normalMap: "$textures/Rock_Normal.jpg",
aoMap: "$textures/Rock_Occlusion.jpg",
mass: 10000
});
WAFFLE.AssetLoader.queue('Resources/Model/FlammableBox.json', 4, {
name: "Flame Box",
initRotation: new THREE.Euler(-Math.PI / 2, 0, -Math.PI / 2, 'XYZ'),
scale: new THREE.Vector3(0.2, 0.2, 0.2),
map: "$textures/FlammableBox.png",
normalMap: "$textures/FlammableBox_Normal.jpg",
aoMap: "$textures/FlammableBox_Occlusion.jpg",
mass: 15
});
WAFFLE.AssetLoader.queue('Resources/Model/Crate.json', 4, {
name: "Crate",
scale: new THREE.Vector3(2, 2, 2),
map: "$textures/Crate.jpg",
normalMap: "$textures/Crate_Normal.jpg",
aoMap: "$textures/Crate_Occlusion.jpg",
mass: 15,
});
WAFFLE.AssetLoader.queue('Resources/Model/EiffelTower.json', 4, {
name: "Eiffel Tower",
initRotation: new THREE.Euler(-Math.PI, 0, 0, 'XYZ'),
scale: new THREE.Vector3(0.4, 0.4, 0.4),
map: "$textures/EiffelTower.jpg",
mass: 100000
});
WAFFLE.AssetLoader.queue('Resources/Model/Fence.json', 4, {
name: "Fence",
scale: new THREE.Vector3(0.5, 0.5, 0.5),
computeNormals: false,
map: "$textures/Fence.jpg",
normalMap: "$textures/Fence_Normal.jpg",
aoMap: "$textures/Fence_Occlusion.jpg",
mass: 6
});
WAFFLE.AssetLoader.queue('Resources/Model/PalletBoard.json', 4, {
name: "Pallet Board",
scale: new THREE.Vector3(0.05, 0.05, 0.05),
computeNormals: false,
map: "$textures/Fence.jpg",
normalMap: "$textures/Fence_Normal.jpg",
aoMap: "$textures/Fence_Occlusion.jpg",
mass: 15
});
WAFFLE.AssetLoader.queue('Resources/Model/Dresser.json', 4, {
name: "Dresser",
scale: new THREE.Vector3(0.02, 0.02, 0.02),
computeNormals: false,
map: "$textures/Dresser.jpg",
mass: 5
});
WAFFLE.AssetLoader.queue('Resources/Model/Camero.json', 4, {
name: "Camero",
category: "Vehicles",
scale: new THREE.Vector3(2, 2, 2),
initRotation: new THREE.Euler(-Math.PI / 2, 0, -Math.PI, 'XYZ'),
map: "$textures/Camero.png",
isVehicle: true,
hasHorn: true,
maximumVelocity: 6,
acceleration: 0.01,
mass: 15
});
//
// Trigger load
//
$("#loadingText").html("Loading " + assetsRemaining + " assets...");
WAFFLE.AssetLoader.load(function(remaining) {
if (remaining == 0) {
$("#loadingText").html("Loading login screen...");
if (isDebug) {
if (debugMode == 0) {
var mapUuid = Object.keys(mapList)[0];
currentMap = mapUuid;
loadSingleplayer(JSON.parse(localStorage.getItem('map' + mapUuid)));
} else {
loadMultiplayer();
}
} else {
$("#loadingScreen").fadeOut(1500, function() {
$("#loginScreen").fadeIn(1500);
});
}
} else if (remaining == 1) {
$("#loadingText").html("Loading 1 asset...");
} else {
$("#loadingText").html("Loading " + remaining + " assets...");
}
});
function populateSpawnMenu() {
// Load spawn tabs
addSpawnMenuTab("Models", "resources/icon/box.svg", "spawnItems");
addSpawnMenuTab("Lighting", "resources/icon/package.svg", "lightingItems");
addSpawnMenuTab("Tools", "resources/icon/toolbox.svg", "toolItems");
addSpawnMenuTab("Scenes", "resources/icon/policebox.svg", "sceneItems");
//addSpawnMenuTab("Shaders", "resources/icon/bricks.svg", "shaderItems");
// Load debug tab
if (isDebug) {
addSpawnMenuTab("Debug", "resources/icon/waffle.svg", "debugUI");
}
// Add vehicles category to prevent it being shown after geometries
addSpawnCategory("Vehicles");
// Load default geometries
addSpawnTile({
name: "Cube",
category: "Geometries",
type: "geometry",
geometryType: "box",
width: 4,
height: 4,
depth: 4
});
addSpawnTile({
name: "Cone",
category: "Geometries",
type: "geometry",
geometryType: "cone",
radius: 5,
height: 20,
radialSegments: 32,
heightSegments: 1
});
addSpawnTile({
name: "Cylinder",
category: "Geometries",
type: "geometry",
geometryType: "cylinder",
radiusTop: 5,
radiusBottom: 5,
height: 20,
radialSegments: 32,
heightSegments: 1
});
addSpawnTile({
name: "Dodecahedron",
category: "Geometries",
type: "geometry",
geometryType: "dodecahedron",
radius: 5,
detail: 0
});
addSpawnTile({
name: "Icosahedron",
category: "Geometries",
type: "geometry",
geometryType: "icosahedron",
radius: 5,
detail: 0
});
addSpawnTile({
name: "Octahedron",
category: "Geometries",
type: "geometry",
geometryType: "octahedron",
radius: 5,
detail: 0
});
addSpawnTile({
name: "Sphere",
category: "Geometries",
type: "geometry",
geometryType: "sphere",
radius: 5,
widthSegments: 32,
heightSegments: 32
});
addSpawnTile({
name: "Tetrahedron",
category: "Geometries",
type: "geometry",
geometryType: "tetrahedron",
radius: 5,
detail: 0
});
addSpawnTile({
name: "Torus",
category: "Geometries",
type: "geometry",
geometryType: "torus",
radius: 5,
tube: 2,
radialSegments: 32,
tublarSegments: 32
});
addSpawnTile({
name: "Torus Knot",
category: "Geometries",
type: "geometry",
geometryType: "torusKnot",
radius: 5,
tube: 2,
radialSegments: 32,
tublarSegments: 32
});
}
// Load single player
var localMap;
function loadSingleplayer(mapData) {
isSinglePlayer = true;
// Load ground
grassPlane.material.map = getTexture(mapData.ground.map);
grassPlane.material.normalMap = getOption("graphics_normal_mapping") ? getTexture(mapData.ground.normalMap) : null;
grassPlane.material.aoMap = getOption("graphics_occlusion_mapping") ? getTexture(mapData.ground.aoMap) : null;
// Load sky
skybox.material.uniforms.topColor.value = new THREE.Color(mapData.sky.top);
skybox.material.uniforms.bottomColor.value = new THREE.Color(mapData.sky.bottom);
// Load saved singleplayer map
localMap = mapData;
for (var key in localMap) {
new WorldObjectLoader(localMap[key]);
}
// Hide login screen items
$("#loginScreen").hide();
// Load the map scene
if ("sceneURL" in localMap) {
new SceneLoader(localMap["sceneURL"]);
}
// Load the player position
if ("playerPosition" in localMap) {
// Set player position
player.position.set(localMap[ "playerPosition" ].x, localMap[ "playerPosition" ].y, localMap[ "playerPosition" ].z);
// Move skybox
skybox.position.z = player.position.z;
skybox.position.x = player.position.x;
// Move and offset grass
grassPlane.position.z = player.position.z;
grassPlane.position.x = player.position.x;
grassPlane.material.map.offset.set(player.position.x / 32, -(player.position.z / 32));
}
// Show the player
player.visible = true;
// Disable orbit control's auto-rotate
controls.autoRotate = false;
// Enable orbit controls
controls.enabled = true;
// Show login tips
showTip("Hold TAB to view the spawn menu");
setTimeout(function () {showTip("Press ESC to open the pause menu")}, 5000);
// The player is in game
isPlaying = true;
// Animate into game
animateIntoGame();
// Fire events
broadcastEvent('onPlayerConnect');
}
// Load single player
function loadMultiplayer() {
if (!window.WebSocket) return;
isSinglePlayer = false;
// Loading text
$("#loadingText").html("Connecting to server...");
// Show loading screen
$("#loginScreen").fadeOut(1500, function() {
$("#loadingScreen").fadeIn(1500, function() {
connectWorldServer(
// Called when the connection is opened
function() {
$("#loadingText").html("Loading map...");
// Give the server the player name and begin proper communication with server
connection.send(JSON.stringify({type: '0', name: player.name}));
// Request the map from the server
connection.send(JSON.stringify({type:'11'}));
},
// Called when an error occurs whilst opening the connection
function () {
$("#loadingText").html("Error connecting to server");
setTimeout(function() {
$("#loadingScreen").fadeOut(1500, function() {
$("#loginScreen").fadeIn(1500);
});
}, 1000);
});
});
});
}
// Called when the map has been recieved from the server
function loadedMap() {
// Show the player
player.visible = true;
// Disable orbit control's auto-rotate
controls.autoRotate = false;
// Enable orbit controls
controls.enabled = true;
// Display and enable the chat
if (!isSinglePlayer) {
if (getOption("interface_display_chat")) chatBox.css("visibility", "visible");
isChatAvailable = true;
}
// Show login tips
showTip("Hold TAB to view the spawn menu");
setTimeout(function () {showTip("Press ESC to open the pause menu")}, 5000);
setTimeout(function () {showTip("Press ENTER to use the chat")}, 20000);
// The player is in game
isPlaying = true;
// Animate into game
animateIntoGame();
}
function animateIntoGame() {
// Fade out blur effect
for (var i = 0; i < 1000; i++) {
setTimeout(function() {
postProcessor.setUniform("horizontalBlur", "h", postProcessor.getUniform("horizontalBlur", "h") - blurIntensity / 1000);
postProcessor.setUniform("verticalBlur", "v", postProcessor.getUniform("verticalBlur", "v") - blurIntensity / 1000);
if (i == 1000) {
postProcessor.disable("verticalBlur");
}
}, i);
}
// Fade out loading screen
$("#loadingScreen").fadeOut(1000, function () {
// Display mobile controls
$("#button_open_spawn_menu").css("visibility", "visible");
$("#button_open_pause_menu").css("visibility", "visible");
});
// Display tip bar
$("#tipBar").fadeIn(1000);
}
function exitGame() {
// Remove scene objects
var uuids = [];
for (var sceneObject in scene.children) {
if (scene.children[sceneObject].type == 'Mesh' || scene.children[sceneObject].type == 'PointLight' || scene.children[sceneObject].type == 'LightHelper') {
uuids.push(scene.children[sceneObject].uuid);
}
}
for (var id in uuids) {
scene.remove(getSceneObjectByUUID(uuids[id]));
}
// Empty map object arrays
meshList = [];
lightList = [];
mirrorList = [];
// Hide pause menu
togglePauseMenu();
// Enable blur effect
postProcessor.setUniform("horizontalBlur", "h", blurIntensity);
postProcessor.setUniform("verticalBlur", "v", blurIntensity);
postProcessor.enable("verticalBlur");
// Hide the player
player.visible = false;
// Enable orbit control's auto-rotate
controls.autoRotate = true;
// Disable orbit controls
controls.enabled = false;
// Hide and disable the chat
if (!isSinglePlayer) {
chatBox.css("visibility", "hidden");
isChatAvailable = false;
}
// The player is not in game
isPlaying = false;
if (isSinglePlayer) {
// Save player location in map
localMap[ "playerPosition" ] = player.position;
// Save map
saveMap();
}
// Reset camera and player
player.position.x = 0;
player.position.y = 1;
player.position.z = 0;
player.rotation.x = 0;
player.rotation.y = 0;
player.rotation.z = 0;
controls.reset();
camera.position.y += 15;
camera.position.z += 35;
// Hide tips
$("#tipBar").hide();
for (var i in deferreds) {
deferreds[i].resolve();
}
// Disconnect from server
isConnectionReady = false;
if (connection != undefined && connection.readyState == 1) connection.close();
// Remove server players
for (var playerName in players) {
scene.remove(players[playerName]);
}
players = [];
// Clear chat
chatBox.empty();
// Change to default day scene
new SceneLoader("resources/scenes/day.json", true);
// Show login screen
$("#loginScreen").show();
}