r3-custom-code/78gnds8jrj.js

330 lines
7.5 KiB
JavaScript

if (data.entity === this.parentEntity) {
console.log('snail runner loaded');
} else {
return;
}
/**
* Custom Code Components
*/
this.beforeRender = R3.EntityManager.Instance.findComponentById('94xi7aitax');
this.mouseMove = R3.EntityManager.Instance.findComponentById('jz3qg0174l');
this.mouseDown = R3.EntityManager.Instance.findComponentById('8dntnb01wu');
this.mouseUp = R3.EntityManager.Instance.findComponentById('kx7drv1vqw');
this.keyDown = R3.EntityManager.Instance.findComponentById('ramgcjepp5');
this.keyUp = R3.EntityManager.Instance.findComponentById('4uie4sjqxd');
/**
* Meshes
*/
this.snail = R3.EntityManager.Instance.findComponentById('z2izbq1hcz');
this.cloud = R3.EntityManager.Instance.findComponentById('jbvm17iwfx');
this.solar = R3.EntityManager.Instance.findComponentById('lq5xyiu8n4');
this.treesLeft = R3.EntityManager.Instance.findComponentById('lq0rxnjq3n');
this.treesRight = R3.EntityManager.Instance.findComponentById('oag0yr1rav');
this.road = R3.EntityManager.Instance.findComponentById('4furha3wst');
this.sun = R3.EntityManager.Instance.findComponentById('nkdyvga5nl');
/**
* Lights
*/
this.spotLight = R3.EntityManager.Instance.findComponentById('abkdj2g1f1');
/**
* Images
*/
this.level1 = R3.EntityManager.Instance.findComponentById('z0kofhvw8k');
/**
* Road Sections and Clouds
*/
this.sections = [];
this.clouds = [];
this.maxSpeed = 50;
this.speedPercentage = 0;
this.cloudTime = 0;
this.time = 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.initialSnailSettings = new THREE.Vector3(0, 0, 0);
this.maximumSnailSettings = new THREE.Vector3(0.2, 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 = R3.EntityManager.Instance.findComponentById('hd8dsn7o4c');
this.raycaster = R3.EntityManager.Instance.findComponentById('h3yxd73sz7');
this.mouse = R3.EntityManager.Instance.findComponentById('eriv6mcw8k');
R3.CustomCode.prototype.loadLevel = function(index) {
var image = this['level' + index];
var heightData = image.getHeightData();
for (var z = 0; z < 10; z++) {
for (var x = 0; x < 1024; x++) {
var color = heightData[x,z];
if (color !== 0) {
console.log(color + ': ' + x + ', ' + z);
}
}
}
}.bind(this);
R3.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)
R3.CustomCode.prototype.spawnCloud = function() {
var cloud = this.cloud.clone();
this.cloud.position.x = -155;
//this.cloud.position.z = Math.sin(this.time) * 2;
this.cloud.position.z = (Math.random() * 30) - 15;
this.cloud.updateInstance('position');
this.clouds.push(cloud);
}.bind(this)
R3.CustomCode.prototype.advanceClouds = function(delta) {
var diff = delta * this.speed;
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();
}
)
this.cloudTime += diff;
if (this.cloudTime > 3) {
this.spawnCloud();
this.cloudTime = 0;
}
}.bind(this);
R3.CustomCode.prototype.advanceRoadSections = function(delta) {
var diff = delta * this.speed;
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);
R3.CustomCode.prototype.adjustCamera = function(delta) {
/**
* We don't adjust the camera anymore since it affects the ray-tracing in mouse-move
* An update here causes a slightly different 3d mouse location on mouse move which
* immediately causes a new snail position: resulting in jitter
*/
}.bind(this);
R3.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);
R3.CustomCode.prototype.adjustSnailPosition = function(delta) {
this.currentSnailSettings.lerpVectors(
this.initialSnailSettings,
this.maximumSnailSettings,
this.speedPercentage
);
this.currentSnailPosition.z = this.snail.position.z;
this.currentSnailPosition.lerp(this.targetSnailPosition, this.currentSnailSettings.x);
this.snail.position.z = this.currentSnailPosition.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);
R3.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;
}.bind(this);
R3.Event.Subscribe(
R3.Event.GAME_START,
function() {
/**
* Game Variables
*/
this.snail.position.z = 0;
this.speed = 0;
this.speedUp = false;
this.time = 0;
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.cloudTime = 0;
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();
this.loadLevel(1);
R3.Event.Emit(R3.Event.GAME_LOADED);
//@ sourceURL=entityLoaded.js