2018-03-09 00:36:32 +01:00
|
|
|
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');
|
2018-03-09 14:49:40 +01:00
|
|
|
this.mouseDown = GameLib.EntityManager.Instance.findComponentById('8dntnb01wu');
|
2018-03-09 14:55:00 +01:00
|
|
|
this.mouseUp = GameLib.EntityManager.Instance.findComponentById('kx7drv1vqw');
|
|
|
|
this.keyDown = GameLib.EntityManager.Instance.findComponentById('ramgcjepp5');
|
|
|
|
this.keyUp = GameLib.EntityManager.Instance.findComponentById('4uie4sjqxd');
|
2018-03-09 00:36:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Meshes
|
|
|
|
*/
|
|
|
|
this.snail = GameLib.EntityManager.Instance.findComponentById('z2izbq1hcz');
|
|
|
|
this.cloud = GameLib.EntityManager.Instance.findComponentById('jbvm17iwfx');
|
|
|
|
this.solar = GameLib.EntityManager.Instance.findComponentById('lq5xyiu8n4');
|
2018-03-09 01:45:57 +01:00
|
|
|
this.treesLeft = GameLib.EntityManager.Instance.findComponentById('lq0rxnjq3n');
|
|
|
|
this.treesRight = GameLib.EntityManager.Instance.findComponentById('oag0yr1rav');
|
|
|
|
this.road = GameLib.EntityManager.Instance.findComponentById('4furha3wst');
|
2018-03-09 16:17:19 +01:00
|
|
|
this.sun = GameLib.EntityManager.Instance.findComponentById('nkdyvga5nl');
|
2018-03-09 01:45:57 +01:00
|
|
|
|
2018-03-09 19:08:22 +01:00
|
|
|
/**
|
|
|
|
* Lights
|
|
|
|
*/
|
|
|
|
this.spotLight = GameLib.EntityManager.Instance.findComponentById('abkdj2g1f1');
|
|
|
|
|
2018-03-09 19:20:37 +01:00
|
|
|
/**
|
|
|
|
* Images
|
|
|
|
*/
|
|
|
|
this.level1 = GameLib.EntityManager.Instance.findComponentById('z0kofhvw8k');
|
|
|
|
|
2018-03-09 01:45:57 +01:00
|
|
|
/**
|
2018-03-09 15:14:42 +01:00
|
|
|
* Road Sections and Clouds
|
2018-03-09 01:45:57 +01:00
|
|
|
*/
|
2018-03-09 15:14:42 +01:00
|
|
|
this.sections = [];
|
|
|
|
this.clouds = [];
|
2018-03-09 01:45:57 +01:00
|
|
|
|
2018-03-09 17:07:58 +01:00
|
|
|
this.maxSpeed = 50;
|
|
|
|
this.speedPercentage = 0;
|
|
|
|
|
2018-03-09 18:39:00 +01:00
|
|
|
this.cloudTime = 0;
|
2018-03-09 18:52:08 +01:00
|
|
|
this.time = 0;
|
2018-03-09 18:39:00 +01:00
|
|
|
|
2018-03-09 17:18:10 +01:00
|
|
|
/**
|
|
|
|
* Camera settings y = height, z = FOV
|
|
|
|
*/
|
2018-03-09 17:44:40 +01:00
|
|
|
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);
|
2018-03-09 17:07:58 +01:00
|
|
|
|
2018-03-09 17:44:40 +01:00
|
|
|
this.initialSnailSettings = new THREE.Vector3(0, 0, 0);
|
2018-03-09 19:04:44 +01:00
|
|
|
this.maximumSnailSettings = new THREE.Vector3(0.2, 0, 0);
|
2018-03-09 17:29:48 +01:00
|
|
|
this.currentSnailSettings = new THREE.Vector3(0, 0, 0);
|
|
|
|
|
|
|
|
this.currentSnailPosition = new THREE.Vector3(0, 0, 0);
|
|
|
|
this.targetSnailPosition = new THREE.Vector3(0, 0, 0);
|
|
|
|
|
2018-03-09 02:47:05 +01:00
|
|
|
/**
|
2018-03-09 16:12:19 +01:00
|
|
|
* Camera, Raycaster, Mouse
|
2018-03-09 02:47:05 +01:00
|
|
|
*/
|
2018-03-09 16:12:19 +01:00
|
|
|
this.camera = GameLib.EntityManager.Instance.findComponentById('hd8dsn7o4c');
|
|
|
|
this.raycaster = GameLib.EntityManager.Instance.findComponentById('h3yxd73sz7');
|
|
|
|
this.mouse = GameLib.EntityManager.Instance.findComponentById('eriv6mcw8k');
|
2018-03-09 02:47:05 +01:00
|
|
|
|
2018-03-09 19:25:10 +01:00
|
|
|
GameLib.CustomCode.prototype.loadLevel = function(index) {
|
2018-03-09 19:22:13 +01:00
|
|
|
var image = this['level' + index];
|
2018-03-09 19:47:35 +01:00
|
|
|
var heightData = image.getHeightData();
|
|
|
|
|
2018-03-09 19:48:46 +01:00
|
|
|
for (var z = 0; z < 10; z++) {
|
|
|
|
for (var x = 0; x < 1024; x++) {
|
2018-03-09 19:47:35 +01:00
|
|
|
var color = heightData[x,z];
|
|
|
|
|
|
|
|
if (color !== 0) {
|
|
|
|
console.log(color + ': ' + x + ', ' + z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:25:10 +01:00
|
|
|
}.bind(this);
|
2018-03-09 19:22:13 +01:00
|
|
|
|
2018-03-09 15:14:42 +01:00
|
|
|
GameLib.CustomCode.prototype.spawnRoadSections = function() {
|
2018-03-09 15:03:11 +01:00
|
|
|
|
2018-03-09 15:14:42 +01:00
|
|
|
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();
|
|
|
|
}
|
2018-03-09 01:45:57 +01:00
|
|
|
|
|
|
|
this.sections.push({
|
|
|
|
left : cloneLeft,
|
|
|
|
road : cloneRoad,
|
|
|
|
right : cloneRight
|
|
|
|
})
|
2018-03-09 00:36:32 +01:00
|
|
|
|
2018-03-09 15:14:42 +01:00
|
|
|
}.bind(this)
|
2018-03-09 03:19:54 +01:00
|
|
|
|
2018-03-09 18:52:08 +01:00
|
|
|
GameLib.CustomCode.prototype.spawnCloud = function() {
|
|
|
|
var cloud = this.cloud.clone();
|
|
|
|
|
|
|
|
this.cloud.position.x = -155;
|
2018-03-09 19:00:49 +01:00
|
|
|
//this.cloud.position.z = Math.sin(this.time) * 2;
|
|
|
|
this.cloud.position.z = (Math.random() * 30) - 15;
|
|
|
|
|
2018-03-09 18:52:08 +01:00
|
|
|
this.cloud.updateInstance('position');
|
|
|
|
|
|
|
|
this.clouds.push(cloud);
|
|
|
|
}.bind(this)
|
|
|
|
|
2018-03-09 18:39:00 +01:00
|
|
|
GameLib.CustomCode.prototype.advanceClouds = function(delta) {
|
|
|
|
|
|
|
|
var diff = delta * this.speed;
|
2018-03-09 15:20:15 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
)
|
2018-03-09 18:39:00 +01:00
|
|
|
|
|
|
|
this.cloudTime += diff;
|
|
|
|
|
2018-03-09 19:01:52 +01:00
|
|
|
if (this.cloudTime > 3) {
|
2018-03-09 18:39:00 +01:00
|
|
|
this.spawnCloud();
|
|
|
|
this.cloudTime = 0;
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:20:15 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-09 18:39:00 +01:00
|
|
|
GameLib.CustomCode.prototype.advanceRoadSections = function(delta) {
|
|
|
|
|
|
|
|
var diff = delta * this.speed;
|
|
|
|
|
2018-03-09 15:20:15 +01:00
|
|
|
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);
|
|
|
|
|
2018-03-09 18:39:00 +01:00
|
|
|
GameLib.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
|
|
|
|
*/
|
2018-03-09 15:20:15 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-09 15:25:55 +01:00
|
|
|
GameLib.CustomCode.prototype.adjustSpeed = function(delta) {
|
|
|
|
|
|
|
|
if (this.speedUp) {
|
|
|
|
this.speed += delta;
|
2018-03-09 15:28:41 +01:00
|
|
|
} else {
|
|
|
|
this.speed -= delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.speed < 0) {
|
|
|
|
this.speed = 0;
|
|
|
|
}
|
|
|
|
|
2018-03-09 17:07:58 +01:00
|
|
|
if (this.speed > this.maxSpeed) {
|
|
|
|
this.speed = this.maxSpeed;
|
2018-03-09 15:25:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 17:07:58 +01:00
|
|
|
this.speedPercentage = this.speed / this.maxSpeed;
|
|
|
|
|
2018-03-09 17:18:10 +01:00
|
|
|
this.currentCameraSettings.lerpVectors(
|
|
|
|
this.initialCameraSettings,
|
|
|
|
this.maximumCameraSettings,
|
|
|
|
this.speedPercentage
|
|
|
|
);
|
2018-03-09 17:12:35 +01:00
|
|
|
|
2018-03-09 17:18:10 +01:00
|
|
|
this.camera.position.y = this.currentCameraSettings.y;
|
2018-03-09 17:07:58 +01:00
|
|
|
this.camera.updateInstance('position');
|
|
|
|
|
2018-03-09 17:18:10 +01:00
|
|
|
this.camera.fov = this.currentCameraSettings.z;
|
2018-03-09 17:15:23 +01:00
|
|
|
this.camera.updateInstance('fov');
|
|
|
|
|
2018-03-09 15:25:55 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-09 15:48:20 +01:00
|
|
|
GameLib.CustomCode.prototype.adjustSnailPosition = function(delta) {
|
2018-03-09 17:36:34 +01:00
|
|
|
|
2018-03-09 17:40:36 +01:00
|
|
|
this.currentSnailSettings.lerpVectors(
|
|
|
|
this.initialSnailSettings,
|
|
|
|
this.maximumSnailSettings,
|
|
|
|
this.speedPercentage
|
|
|
|
);
|
2018-03-09 15:48:20 +01:00
|
|
|
|
2018-03-09 17:40:36 +01:00
|
|
|
this.currentSnailPosition.z = this.snail.position.z;
|
2018-03-09 15:31:33 +01:00
|
|
|
|
2018-03-09 17:42:43 +01:00
|
|
|
this.currentSnailPosition.lerp(this.targetSnailPosition, this.currentSnailSettings.x);
|
2018-03-09 15:31:33 +01:00
|
|
|
|
2018-03-09 17:40:36 +01:00
|
|
|
this.snail.position.z = this.currentSnailPosition.z;
|
2018-03-09 15:48:20 +01:00
|
|
|
|
2018-03-09 17:40:36 +01:00
|
|
|
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');
|
2018-03-09 15:31:33 +01:00
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-09 16:16:19 +01:00
|
|
|
GameLib.CustomCode.prototype.update3dCursor = function() {
|
|
|
|
|
|
|
|
this.raycaster.setFromCamera(
|
|
|
|
this.mouse,
|
|
|
|
this.camera
|
|
|
|
);
|
|
|
|
|
2018-03-09 16:32:05 +01:00
|
|
|
var distance = - this.camera.position.x / this.raycaster.direction.x;
|
2018-03-09 16:16:19 +01:00
|
|
|
var cursorPosition = this.camera.position.clone().add(
|
|
|
|
this.raycaster.direction.clone().multiply(
|
|
|
|
distance,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-03-09 17:29:48 +01:00
|
|
|
this.targetSnailPosition.z = cursorPosition.z;
|
2018-03-09 18:39:00 +01:00
|
|
|
|
2018-03-09 16:16:19 +01:00
|
|
|
}.bind(this);
|
2018-03-09 15:31:33 +01:00
|
|
|
|
2018-03-09 15:10:16 +01:00
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.GAME_START,
|
|
|
|
function() {
|
|
|
|
/**
|
|
|
|
* Game Variables
|
|
|
|
*/
|
|
|
|
this.snail.position.z = 0;
|
|
|
|
this.speed = 0;
|
|
|
|
this.speedUp = false;
|
2018-03-09 18:52:08 +01:00
|
|
|
this.time = 0;
|
|
|
|
|
2018-03-09 15:48:20 +01:00
|
|
|
this.movement = {
|
|
|
|
left : false,
|
|
|
|
right : false,
|
|
|
|
leftAmount : 0,
|
|
|
|
rightAmount : 0
|
|
|
|
};
|
|
|
|
|
2018-03-09 15:14:42 +01:00
|
|
|
this.clouds.map(
|
|
|
|
function(cloud) {
|
|
|
|
cloud.geometry = null;
|
|
|
|
cloud.materials = null;
|
|
|
|
cloud.remove();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.clouds = [];
|
|
|
|
|
2018-03-09 18:39:00 +01:00
|
|
|
this.cloudTime = 0;
|
|
|
|
|
2018-03-09 17:29:48 +01:00
|
|
|
this.currentSnailPosition.z = this.snail.position.z;
|
|
|
|
|
2018-03-09 15:10:16 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
);
|
|
|
|
|
2018-03-09 15:14:42 +01:00
|
|
|
this.spawnRoadSections();
|
2018-03-09 00:36:32 +01:00
|
|
|
|
2018-03-09 19:22:13 +01:00
|
|
|
this.loadLevel(1);
|
|
|
|
|
2018-03-09 15:25:01 +01:00
|
|
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
|
|
|
|
2018-03-09 00:36:32 +01:00
|
|
|
//@ sourceURL=entityLoaded.js
|