From 5069ec4ee2c75dd725a3af4f3074e8138872c6f6 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Tue, 29 Nov 2016 12:52:48 +0100 Subject: [PATCH] new build step in gulp + some components --- gulpfile.js | 49 ++++++++++-- package.json | 6 +- src/game-lib-a.js | 5 +- src/game-lib-component-entity-parent.js | 71 +++++++++++++++++ src/game-lib-component-entity-permutation.js | 81 ++++++++++++++++++++ src/game-lib-component-path-controls.js | 14 +++- src/game-lib-component-path-following.js | 78 +++++++++++++------ 7 files changed, 268 insertions(+), 36 deletions(-) create mode 100644 src/game-lib-component-entity-parent.js create mode 100644 src/game-lib-component-entity-permutation.js diff --git a/gulpfile.js b/gulpfile.js index 1b589cb..1597985 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,19 +1,52 @@ -var gulp = require('gulp'); -var concat = require('gulp-concat'); -var sort = require('gulp-sort'); -var minify = require('gulp-minify'); -var plumber = require('gulp-plumber'); -var istanbul = require('gulp-istanbul'); -var mocha = require('gulp-mocha'); -var watch = require('gulp-watch'); +var gulp = require('gulp'); +var concat = require('gulp-concat'); +var sort = require('gulp-sort'); +var minify = require('gulp-minify'); +var plumber = require('gulp-plumber'); +var istanbul = require('gulp-istanbul'); +var mocha = require('gulp-mocha'); +var watch = require('gulp-watch'); var preprocessor = require('gulp-c-preprocessor'); +var inject = require('gulp-inject-string'); +var replace = require('gulp-string-replace'); gulp.task( 'merge', function() { + + var d = new Date(); + var __DATE__ = JSON.stringify(d.toString()); + console.log("Compiling start at", __DATE__); + + var __EXTENDS__ = ""; return gulp.src('./src/game-lib-*.js') .pipe(sort()) .pipe(concat('game-lib.js')) + .pipe( + inject.prepend( + "// COMPILE TIME DEFINITIONS (Generated via gulp) \n" + + "var __DATE__ = " + __DATE__ + "; \n" + + "// END COMPILE TIME DEFINITIONS \n \n" + ) + ) + .pipe( + replace(new RegExp('GameLib.D3.Utils.Extend\\(.*?\\);', 'g'), function (replacement) { + __EXTENDS__ += replacement + "\n"; + return "//" + replacement; + }) + ) + .pipe( + inject.append( + "\n//EXTENDS\n" + ) + ) + .pipe( + replace( + "//EXTENDS", function() { + return "//EXTENDING INTERFACES (Generated via gulp) \n" + __EXTENDS__ + "//END EXTENDING"; + } + ) + ) .pipe(minify({ ext:{ src:'.js', diff --git a/package.json b/package.json index e1347fd..95ead59 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,14 @@ "cannon": "^0.6.2", "gulp": "^3.9.1", "gulp-concat": "^2.6.0", + "gulp-inject-string": "^1.1.0", "gulp-minify": "0.0.14", "gulp-sort": "^2.0.0", + "gulp-string-replace": "^0.2.0", "gulp-util": "^3.0.7", + "i": "^0.3.5", "lodash": "^4.17.2", + "npm": "^4.0.2", "q": "^1.4.1", "three": "^0.81.2", "through2": "^2.0.1" @@ -24,6 +28,6 @@ "gulp-watch": "^4.3.10", "sinon": "^1.17.6", "sinon-chai": "^2.8.0", - "gulp-c-preprocessor": "0.0.4", + "gulp-c-preprocessor": "0.0.4" } } diff --git a/src/game-lib-a.js b/src/game-lib-a.js index 686e6e0..522dc3e 100644 --- a/src/game-lib-a.js +++ b/src/game-lib-a.js @@ -33,4 +33,7 @@ console.log("Loading runtime library..."); //#ifdef EDITOR__ console.log("Loading editor library..."); -//#endif \ No newline at end of file +//#endif + +// This gets injected by gulp +console.log("Compiled at", __DATE__); \ No newline at end of file diff --git a/src/game-lib-component-entity-parent.js b/src/game-lib-component-entity-parent.js new file mode 100644 index 0000000..4b7684f --- /dev/null +++ b/src/game-lib-component-entity-parent.js @@ -0,0 +1,71 @@ +/** + * + * @param id + * @param name + * @param parent + * @constructor + */ +GameLib.D3.ComponentEntityParent = function ComponentEntityParent( + id, + name, + parent +) { + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + this.parent = parent; + + this.parentEntity = null; + this.originPosition = new GameLib.D3.Vector3(0, 0, 0); + + // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. + GameLib.D3.Utils.Extend(GameLib.D3.ComponentEntityParent, GameLib.D3.ComponentInterface); +}; + +//#ifdef RUNTIME__ +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentEntityParent.prototype.onLateUpdate = function( + deltaTime, + parentEntity +) { + + if(parentEntity && this.parent) { + + // parentEntity = this component's parent + parentEntity.position.x = this.parent.position.x /*+ this.parentOrigin.x + this.originPosition.x*/; + parentEntity.position.y = this.parent.position.y /*+ this.parentOrigin.y + this.originPosition.y*/; + parentEntity.position.z = this.parent.position.z /*+ this.parentOrigin.z + this.originPosition.z*/; + + parentEntity.quaternion.x = this.parent.quaternion.x; + parentEntity.quaternion.y = this.parent.quaternion.y; + parentEntity.quaternion.z = this.parent.quaternion.z; + parentEntity.quaternion.w = this.parent.quaternion.w; + + } +}; + +GameLib.D3.ComponentEntityParent.prototype.onSetParentEntity = function( + parentScene, + parentEntity +) { + if(parentEntity && parentEntity.mesh) { + + /* var scale = parentEntity.mesh.scale.clone(); + /!* parentEntity.mesh.updateMatrix(); + parentEntity.mesh.geometry.applyMatrix( parentEntity.mesh.matrix ); + parentEntity.mesh.updateMatrix();*!/ + */ + + /*this.parentOrigin = this.parent.mesh.geometry.center(); + this.originPosition = parentEntity.mesh.geometry.center();*/ + + + // don't forget to set the scale on the entity itself!!! + } + +}; + +//#endif \ No newline at end of file diff --git a/src/game-lib-component-entity-permutation.js b/src/game-lib-component-entity-permutation.js new file mode 100644 index 0000000..7e3149e --- /dev/null +++ b/src/game-lib-component-entity-permutation.js @@ -0,0 +1,81 @@ +/** + * + * @param id + * @param name + * @param positionOffset + * @param quaternionOffset + * @param scaleOffset + * @constructor + */ +GameLib.D3.ComponentEntityPermutation = function ComponentEntityPermutation( + id, + name, + positionOffset, + quaternionOffset, + scaleOffset +) { + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + this.name = name; + + this.parentEntity = null; + + this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0); + this.quaternionOffset = quaternionOffset || new GameLib.D3.Vector4(0, 0, 0, 1); + this.scaleOffset = scaleOffset || new GameLib.D3.Vector3(0, 0, 0); + + // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. + GameLib.D3.Utils.Extend(GameLib.D3.ComponentEntityPermutation, GameLib.D3.ComponentInterface); +}; + +//#ifdef RUNTIME__ +if(typeof THREE != "undefined") { + ComponentEntityPermutation_quaternion = new THREE.Quaternion(); + ComponentEntityPermutation_quaternionCopy = new THREE.Quaternion(); + ComponentEntityPermutation_position = new THREE.Vector3(); + ComponentEntityPermutation_scale = new THREE.Vector3(); + ComponentEntityPermutation_offsetQuaternion = new THREE.Quaternion(); + ComponentEntityPermutation_offsetPosition = new THREE.Vector3(); + ComponentEntityPermutation_offsetScale = new THREE.Vector3(); +} + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentEntityPermutation.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + + if(parentEntity && parentEntity.mesh) { + ComponentEntityPermutation_quaternion.copy(parentEntity.quaternion); + ComponentEntityPermutation_quaternionCopy.copy(ComponentEntityPermutation_quaternion); + ComponentEntityPermutation_position.copy(parentEntity.position); + + ComponentEntityPermutation_offsetQuaternion.copy(this.quaternionOffset); + ComponentEntityPermutation_quaternion = ComponentEntityPermutation_quaternion.multiply(ComponentEntityPermutation_offsetQuaternion).normalize(); + + ComponentEntityPermutation_offsetPosition.copy(this.positionOffset); + ComponentEntityPermutation_position = ComponentEntityPermutation_position.add(ComponentEntityPermutation_offsetPosition.applyQuaternion(ComponentEntityPermutation_quaternionCopy)); + + ComponentEntityPermutation_scale.copy(parentEntity.scale); + + ComponentEntityPermutation_offsetScale.copy(this.scaleOffset); + ComponentEntityPermutation_scale = ComponentEntityPermutation_scale.add(ComponentEntityPermutation_offsetScale); + + parentEntity.position.x = ComponentEntityPermutation_position.x; + parentEntity.position.y = ComponentEntityPermutation_position.y; + parentEntity.position.z = ComponentEntityPermutation_position.z; + + parentEntity.quaternion.x = ComponentEntityPermutation_quaternion.x; + parentEntity.quaternion.y = ComponentEntityPermutation_quaternion.y; + parentEntity.quaternion.z = ComponentEntityPermutation_quaternion.z; + parentEntity.quaternion.w = ComponentEntityPermutation_quaternion.w; + + parentEntity.scale.x = ComponentEntityPermutation_scale.x; + parentEntity.scale.y = ComponentEntityPermutation_scale.y; + parentEntity.scale.z = ComponentEntityPermutation_scale.z; + } +}; +//#endif \ No newline at end of file diff --git a/src/game-lib-component-path-controls.js b/src/game-lib-component-path-controls.js index 23c5391..54ce11c 100644 --- a/src/game-lib-component-path-controls.js +++ b/src/game-lib-component-path-controls.js @@ -46,17 +46,23 @@ GameLib.D3.ComponentPathControls.prototype.onUpdate = function( // left right if (this.keyLeftPressed) { // Left [j] - this.pathFollowingComponent.offset.x = 0; + + this.pathFollowingComponent.offset.x = -1; this.pathFollowingComponent.offset.y = 0; - this.pathFollowingComponent.offset.z = 1; + this.pathFollowingComponent.offset.z = 0; + } else if (this.keyRightPressed) { // Right [l] - this.pathFollowingComponent.offset.x = 0; + + this.pathFollowingComponent.offset.x = 1; this.pathFollowingComponent.offset.y = 0; - this.pathFollowingComponent.offset.z = -1; + this.pathFollowingComponent.offset.z = 0; + } else if(!this.keyRightPressed && !this.keyLeftPressed){ + this.pathFollowingComponent.offset.x = 0; this.pathFollowingComponent.offset.y = 0; this.pathFollowingComponent.offset.z = 0; + } }; diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index dfaaeb1..de3852a 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -3,6 +3,7 @@ * @param id * @param name * @param splineCurve3 + * @param trackThreeMeshArray * @param accel * @param maxSpeed * @param baseOffset @@ -14,7 +15,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( id, name, splineCurve3, - normalSplineCurve3, + trackThreeMeshArray, accel, maxSpeed, baseOffset, @@ -31,7 +32,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.parentEntity = null; this.splineCurve3 = splineCurve3; - this.normalSplineCurve3 = normalSplineCurve3; + this.trackThreeMeshArray = trackThreeMeshArray; this.maxSpeed = maxSpeed || 10.0; this.accel = accel || 2.0; @@ -53,13 +54,16 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( }; //#ifdef RUNTIME__ + +ComponentPathFollowing_Three_Raycaster = new THREE.Raycaster(); + ///////////////////////// Methods to override ////////////////////////// GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( deltaTime, parentEntity ) { - if(this.splineCurve3 && this.normalSplineCurve3) { + if(this.splineCurve3 && this.trackThreeMeshArray) { if(this.currentPathValue >= 1 || this.currentPathValue < 0) { this.currentPathValue = 0; @@ -69,26 +73,63 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( //http://stackoverflow.com/questions/18400667/three-js-object-following-a-spline-path-rotation-tanget-issues-constant-sp var position = this.splineCurve3.getPointAt(this.currentPathValue); - var nextIndex = this.currentPathValue + 0.005; + + // Update the current thing + var t = deltaTime * this.accel; + t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); + this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t; + + // update current path value & check bounds + this.currentPathValue = this.currentPathValue + ( (this.currentPathValue + this.currentSpeed) - this.currentPathValue ) * t; + if(this.currentSpeed >= this.maxSpeed) { + this.currentSpeed = this.maxSpeed; + } else if (this.currentSpeed <= 0) { + this.currentSpeed = 0.0; + } + + + var nextIndex = this.currentPathValue; if(nextIndex > 1.0) { nextIndex = 0; } var nextPoint = this.splineCurve3.getPointAt(nextIndex); - var normal = this.normalSplineCurve3.getPointAt(this.currentPathValue).normalize(); + + + // - - - - - - - - - - - - - + // Raytrace from thhe current position. + ComponentPathFollowing_Three_Raycaster.set( + position, + new THREE.Vector3( + 0, + -1, + 0 + ) + ); + + + var normal = new THREE.Vector3(0, 1, 0); + + for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { + var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( + this.trackThreeMeshArray[m] + ); + + if(intersect) { + + normal = intersect[0].face.normal; + + break; + } + } + var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal); var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); - { // update current speed - var t = deltaTime * this.accel; - t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); - this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t; - } - { // update position - var t = deltaTime * ((this.currentSpeed / this.maxSpeed) * this.steeringSpeed); // put this into another variable + t = deltaTime * ((this.currentSpeed / this.maxSpeed) * this.steeringSpeed); // put this into another variable t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); var targetVector = new THREE.Vector3( @@ -113,27 +154,20 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.baseOffset.z + lerpedOffset.z ).applyQuaternion(quaternion); + // apply to parent rigidbody instead of direclty to the mesh. parentEntity.position.x = position.x + transformedOffset.x; parentEntity.position.y = position.y + transformedOffset.y; parentEntity.position.z = position.z + transformedOffset.z; } - { // update rotation + { + // update rotation parentEntity.quaternion.x = quaternion.x; parentEntity.quaternion.y = quaternion.y; parentEntity.quaternion.z = quaternion.z; parentEntity.quaternion.w = quaternion.w; } - - // update current path value & check bounds - this.currentPathValue += (this.currentSpeed); - if(this.currentSpeed >= this.maxSpeed) { - this.currentSpeed = this.maxSpeed; - } else if (this.currentSpeed <= 0) { - this.currentSpeed = 0.0; - } - } };