r3-custom-code/78gnds8jrj.js

317 lines
7.4 KiB
JavaScript
Raw Normal View History

if (data.entity === this.parentEntity) {
console.log('snail runner loaded');
} else {
return;
}
/**
* Custom Code Components
*/
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('94xi7aitax');
this.mouseMove = GameLib.EntityManager.Instance.findComponentById('jz3qg0174l');
this.mouseDown = GameLib.EntityManager.Instance.findComponentById('8dntnb01wu');
this.mouseUp = GameLib.EntityManager.Instance.findComponentById('kx7drv1vqw');
this.keyDown = GameLib.EntityManager.Instance.findComponentById('ramgcjepp5');
this.keyUp = GameLib.EntityManager.Instance.findComponentById('4uie4sjqxd');
/**
* Meshes
*/
this.snail = GameLib.EntityManager.Instance.findComponentById('z2izbq1hcz');
this.cloud = GameLib.EntityManager.Instance.findComponentById('jbvm17iwfx');
this.solar = GameLib.EntityManager.Instance.findComponentById('lq5xyiu8n4');
this.treesLeft = GameLib.EntityManager.Instance.findComponentById('lq0rxnjq3n');
this.treesRight = GameLib.EntityManager.Instance.findComponentById('oag0yr1rav');
this.road = GameLib.EntityManager.Instance.findComponentById('4furha3wst');
this.sun = GameLib.EntityManager.Instance.findComponentById('nkdyvga5nl');
/**
* Road Sections and Clouds
*/
this.sections = [];
this.clouds = [];
this.maxSpeed = 50;
this.speedPercentage = 0;
/**
* Camera settings y = height, z = FOV
*/
this.initialCameraSettings = new THREE.Vector3(0, 1.1, 40);
this.maximumCameraSettings = new THREE.Vector3(0, 2.6, 90);
this.currentCameraSettings = new THREE.Vector3(0, 1.1, 40);
this.intitialSnailSettings = new THREE.Vector3(0, 0, 0);
this.maximumSnailSettings = new THREE.Vector3(0, 0, 0);
this.currentSnailSettings = new THREE.Vector3(0, 0, 0);
this.currentSnailPosition = new THREE.Vector3(0, 0, 0);
this.targetSnailPosition = new THREE.Vector3(0, 0, 0);
/**
* Camera, Raycaster, Mouse
*/
this.camera = GameLib.EntityManager.Instance.findComponentById('hd8dsn7o4c');
this.raycaster = GameLib.EntityManager.Instance.findComponentById('h3yxd73sz7');
this.mouse = GameLib.EntityManager.Instance.findComponentById('eriv6mcw8k');
GameLib.CustomCode.prototype.spawnCloud = function() {
var cloud = this.cloud.clone();
this.cloud.position.x = -155;
this.cloud.position.z = (Math.random() * 25) - 12.5;
this.cloud.updateInstance('position');
this.clouds.push(cloud);
}.bind(this)
GameLib.CustomCode.prototype.spawnRoadSections = function() {
var cloneLeft = this.treesLeft.clone();
var cloneRoad = this.road.clone();
var cloneRight = this.treesRight.clone();
for (var i = 0; i < 17; i++) {
this.sections.push({
left : cloneLeft,
road : cloneRoad,
right : cloneRight
})
cloneLeft = cloneLeft.clone();
cloneRoad = cloneRoad.clone();
cloneRight = cloneRight.clone();
}
this.sections.push({
left : cloneLeft,
road : cloneRoad,
right : cloneRight
})
}.bind(this)
GameLib.CustomCode.prototype.advanceClouds = function(diff) {
var cloudInfo = this.clouds.reduce(
function(result, cloud) {
cloud.position.x += diff;
cloud.updateInstance('position');
if (cloud.position.x > 48) {
result.dead.push(cloud);
} else {
result.alive.push(cloud);
}
return result;
},
{
alive : [],
dead : []
}
);
this.clouds = cloudInfo.alive;
cloudInfo.dead.map(
function(deadCloud) {
deadCloud.geometry = null;
deadCloud.materials = null;
deadCloud.remove();
}
)
}.bind(this);
GameLib.CustomCode.prototype.advanceRoadSections = function(diff) {
this.sections.map(
function(section) {
section.left.position.x += diff;
section.road.position.x += diff;
section.right.position.x += diff;
if (section.left.position.x > 48) {
section.left.position.x -= 204;
section.road.position.x -= 204;
section.right.position.x -= 204;
}
section.left.updateInstance('position');
section.road.updateInstance('position');
section.right.updateInstance('position');
}
);
}.bind(this);
GameLib.CustomCode.prototype.advanceCamera = function(delta) {
//this.camera.position.z = this.snail.position.z;
//this.camera.updateInstance('position');
// var modified = false;
// if ((this.camera.position.z - this.snail.position.z) > (0.05)) {
// this.camera.position.z -= this.speed * delta;
// modified = true;
// }
// if ((this.snail.position.z - this.camera.position.z) > (0.05)) {
// this.camera.position.z += this.speed * delta;
// modified = true;
// }
// if (modified) {
// this.camera.lookAt.x = this.snail.position.x;
// this.camera.lookAt.y = this.snail.position.y;
// this.camera.lookAt.z = this.snail.position.z;
//this.camera.updateInstance('position');
//this.camera.updateInstance('lookAt');
// }
}.bind(this);
GameLib.CustomCode.prototype.adjustSpeed = function(delta) {
if (this.speedUp) {
this.speed += delta;
} else {
this.speed -= delta;
}
if (this.speed < 0) {
this.speed = 0;
}
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
this.speedPercentage = this.speed / this.maxSpeed;
this.currentCameraSettings.lerpVectors(
this.initialCameraSettings,
this.maximumCameraSettings,
this.speedPercentage
);
this.camera.position.y = this.currentCameraSettings.y;
this.camera.updateInstance('position');
this.camera.fov = this.currentCameraSettings.z;
this.camera.updateInstance('fov');
}.bind(this);
GameLib.CustomCode.prototype.adjustSnailPosition = function(delta) {
// var diff = delta * 5;
// var modified = false;
// if (this.movement.left) {
// this.snail.position.z += diff;
// this.movement.left = false;
// modified = true;
// }
// if (this.movement.right) {
// this.snail.position.z -= diff;
// this.movement.right = false;
// modified = true;
// }
// if (modified) {
this.currentSnailPosition.z = this.snail.position.z;
this.snail.position.z = this.targetSnailPosition.z;
if (this.snail.position.z < -4.8) {
this.snail.position.z = -4.8;
}
if (this.snail.position.z > 4.8) {
this.snail.position.z = 4.8;
}
this.solar.position.z = this.snail.position.z;
this.snail.updateInstance('position');
this.solar.updateInstance('position');
// }
}.bind(this);
GameLib.CustomCode.prototype.update3dCursor = function() {
this.raycaster.setFromCamera(
this.mouse,
this.camera
);
var distance = - this.camera.position.x / this.raycaster.direction.x;
var cursorPosition = this.camera.position.clone().add(
this.raycaster.direction.clone().multiply(
distance,
true
)
);
this.targetSnailPosition.z = cursorPosition.z;
// this.sun.position.setFrom(cursorPosition);
// this.sun.updateInstance('position');
//cursorPosition.z = 3;
//this.lookAtTarget = cursorPosition;
}.bind(this);
GameLib.Event.Subscribe(
GameLib.Event.GAME_START,
function() {
/**
* Game Variables
*/
this.snail.position.z = 0;
this.speed = 0;
this.speedUp = false;
this.movement = {
left : false,
right : false,
leftAmount : 0,
rightAmount : 0
};
this.clouds.map(
function(cloud) {
cloud.geometry = null;
cloud.materials = null;
cloud.remove();
}
);
this.clouds = [];
this.currentSnailPosition.z = this.snail.position.z;
/**
* Activate our custom code components
*/
this.beforeRender.entityLoaded = this;
this.mouseMove.entityLoaded = this;
this.mouseDown.entityLoaded = this;
this.mouseUp.entityLoaded = this;
this.keyDown.entityLoaded = this;
this.keyUp.entityLoaded = this;
}.bind(this)
);
this.spawnRoadSections();
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
//@ sourceURL=entityLoaded.js