more components

beta.r3js.org
Theunis J. Botha 2016-12-19 17:44:15 +01:00
parent 15d3c5b446
commit 846bdba6c4
21 changed files with 1142 additions and 386 deletions

View File

@ -1,10 +1,20 @@
GameLib.Component = function(
componentType
componentType,
linkedObjects
) {
this.componentType = componentType;
if (GameLib.Utils.UndefinedOrNull(linkedObjects)) {
linkedObjects = [];
}
this.linkedObjects = linkedObjects;
};
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING = 0x1;
GameLib.Component.COMPONENT_RENDERABLE = 0x2;
GameLib.Component.COMPONENT_RENDERER = 0x3;
GameLib.Component.COMPONENT_EDITOR_INPUT = 0x4;
GameLib.Component.COMPONENT_LOOK_AT = 0x5;
GameLib.Component.prototype.toApiComponent = function() {
return this.id;

View File

@ -0,0 +1,5 @@
/**
* GameLib.D3.API.Input namespace
* @constructor
*/
GameLib.D3.API.Input = function() {};

View File

@ -0,0 +1,46 @@
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
* @param id String
* @param name String
* @param domElementId
* @param camera GameLib.D3.Camera
* @constructor
*/
GameLib.D3.API.Input.Editor = function (
id,
name,
domElementId,
camera
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_EDITOR_INPUT,
[
'camera'
]
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = this.constructor.name;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(domElementId)) {
domElementId = "divCanvas";
}
this.domElementId = domElementId;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
};
GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Input.Editor.prototype.constructor = GameLib.D3.API.Input.Editor;

View File

@ -0,0 +1,46 @@
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
* @param id String
* @param name String
* @param domElementId
* @param camera GameLib.D3.Camera
* @constructor
*/
GameLib.D3.API.Input.Fly = function (
id,
name,
domElementId,
camera
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_FLY_INPUT,
[
'camera'
]
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = this.constructor.name;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(domElementId)) {
domElementId = "divCanvas";
}
this.domElementId = domElementId;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
};
GameLib.D3.API.Input.Fly.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Input.Fly.prototype.constructor = GameLib.D3.API.Input.Fly;

View File

@ -2,27 +2,30 @@
* Looks from currentPosition to targetPosition (default up is 0,1,0)
* @param id
* @param name
* @param currentPosition GameLib.API.Vector3
* @param targetPosition GameLib.API.Vector3
* @param currentComponent GameLib.Component
* @param targetComponent GameLib.Component
* @param targetPositionOffset GameLib.API.Vector3
* @param rotationSpeed Number
* @param lookAtMatrix GameLib.API.Matrix4
* @param up GameLib.API.Vector3
* @param currentRotation GameLib.API.Quaternion
* @constructor
*/
GameLib.D3.API.LookAt = function (
id,
name,
currentPosition,
targetPosition,
currentComponent,
targetComponent,
targetPositionOffset,
rotationSpeed,
lookAtMatrix,
up,
currentRotation
rotationSpeed
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_LOOK_AT,
[
'currentComponent',
'targetComponent'
]
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
@ -33,15 +36,15 @@ GameLib.D3.API.LookAt = function (
}
this.name = name;
if(GameLib.Utils.UndefinedOrNull(currentPosition)) {
currentPosition = new GameLib.API.Vector3(0, 10, 10);
if(GameLib.Utils.UndefinedOrNull(currentComponent)) {
currentComponent = null;
}
this.currentPosition = currentPosition;
this.currentComponent = currentComponent;
if(GameLib.Utils.UndefinedOrNull(targetPosition)) {
targetPosition = new GameLib.API.Vector3(0, 0, 0);
if(GameLib.Utils.UndefinedOrNull(targetComponent)) {
targetComponent = null;
}
this.targetPosition = targetPosition;
this.targetComponent = targetComponent;
if(GameLib.Utils.UndefinedOrNull(targetPositionOffset)) {
targetPositionOffset = new GameLib.API.Vector3(0, 0, 0);
@ -53,18 +56,12 @@ GameLib.D3.API.LookAt = function (
}
this.rotationSpeed = rotationSpeed;
if (GameLib.Utils.UndefinedOrNull(lookAtMatrix)) {
lookAtMatrix = new GameLib.API.Matrix4();
}
this.lookAtMatrix = lookAtMatrix;
this.lookAtMatrix = new GameLib.API.Matrix4();
if(GameLib.Utils.UndefinedOrNull(up)) {
up = new GameLib.API.Vector3(0, 1, 0);
}
this.up = up;
this.up = new GameLib.API.Vector3(0, 1, 0);
if(GameLib.Utils.UndefinedOrNull(currentRotation)) {
currentRotation = new GameLib.API.Quaternion();
}
this.currentRotation = currentRotation;
this.currentRotation = new GameLib.API.Quaternion();
};
GameLib.D3.API.LookAt.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.LookAt.prototype.constructor = GameLib.D3.API.LookAt;

View File

@ -3,6 +3,7 @@
* @param id String
* @param name String
* @param spline GameLib.D3.API.Spline
* @param mesh GameLib.D3.API.Mesh
* @param raytraceMesh GameLib.D3.API.Mesh
* @param accelleration Number
* @param maxSpeed Number
@ -14,9 +15,6 @@
* @param currentPathValue Number
* @param currentSpeed Number
* @param direction Number
* @param mx GameLib.Utils.MovingAverage
* @param my GameLib.Utils.MovingAverage
* @param mz GameLib.Utils.MovingAverage
* @param raycaster GameLib.D3.Raycaster
* @param currentPosition GameLib.API.Vector3
* @param futurePosition GameLib.API.Vector3
@ -29,6 +27,7 @@ GameLib.D3.API.PathFollowing = function (
id,
name,
spline,
mesh,
raytraceMesh,
accelleration,
maxSpeed,
@ -50,7 +49,12 @@ GameLib.D3.API.PathFollowing = function (
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING,
[
'spline',
'mesh',
'raytraceMesh'
]
);
if (GameLib.Utils.UndefinedOrNull(id)) {
@ -68,6 +72,11 @@ GameLib.D3.API.PathFollowing = function (
}
this.spline = spline;
if (GameLib.Utils.UndefinedOrNull(mesh)) {
mesh = null;
}
this.mesh = mesh;
if (GameLib.Utils.UndefinedOrNull(raytraceMesh)) {
raytraceMesh = null;
}

View File

@ -0,0 +1,36 @@
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
* @param id String
* @param name String
* @param visible
* @constructor
*/
GameLib.D3.API.Renderable = function (
id,
name,
visible
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_RENDERABLE
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = this.constructor.name;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(visible)) {
visible = true;
}
this.visible = visible;
};
GameLib.D3.API.Renderable.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Renderable.prototype.constructor = GameLib.D3.API.Renderable;

View File

@ -0,0 +1,76 @@
/**
* This component renders a scene
* @param id String
* @param name String
* @param scene GameLib.D3.Scene
* @param camera GameLib.D3.Camera
* @param autoClear bool
* @param localClipping
* @param width
* @param height
* @constructor
*/
GameLib.D3.API.Renderer = function (
id,
name,
scene,
camera,
autoClear,
localClipping,
width,
height
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_RENDERER,
[
'scene',
'camera'
]
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = this.constructor.name;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(scene)) {
scene = null;
}
this.scene = scene;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
if (GameLib.Utils.UndefinedOrNull(autoClear)) {
autoClear = true;
}
this.autoClear = autoClear;
if (GameLib.Utils.UndefinedOrNull(localClipping)) {
localClipping = false;
}
this.localClipping = localClipping;
if (GameLib.Utils.UndefinedOrNull(width)) {
width = 800;
}
this.width = width;
if (GameLib.Utils.UndefinedOrNull(height)) {
height = 600;
}
this.height = height;
};
GameLib.D3.API.Renderer.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Renderer.prototype.constructor = GameLib.D3.API.Renderer;

View File

@ -0,0 +1,5 @@
/**
* GameLib.D3.Input namespace
* @constructor
*/
GameLib.D3.Input = function () {};

View File

@ -0,0 +1,84 @@
/**
* Input parent class
* @param graphics GameLib.D3.Graphics
* @param parentObject
* @param apiInputEditor GameLib.D3.API.Input.Editor
* @constructor
*/
GameLib.D3.Input.Editor = function RuntimeEditorInput(
graphics,
parentObject,
apiInputEditor
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.D3.API.Input.Editor.call(
this,
apiInputEditor.id,
apiInputEditor.name,
apiInputEditor.domElementId,
apiInputEditor.camera
);
/**
* Don't create an instance here - since we don't have our camera loaded yet...
*/
};
GameLib.D3.Input.Editor.prototype = Object.create(GameLib.D3.API.Input.Editor.prototype);
GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor;
GameLib.D3.Input.Editor.prototype.createInstance = function(update) {
var instance = null;
if (this.camera && this.domElementId) {
instance = new THREE.EditorControls(
this.camera.instance,
document.getElementById(this.domElementId)
);
}
this.instance = instance;
return instance;
};
GameLib.D3.Input.Editor.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Input.Editor.prototype.toApiComponent = function() {
var apiInputEditor = new GameLib.D3.API.Input.Editor(
this.id,
this.name,
this.domElementId,
GameLib.Utils.IdOrNull(this.camera)
);
return apiInputEditor;
};
GameLib.D3.Input.Editor.FromObjectComponent = function(graphics, objectComponent) {
var apiInputEditor = new GameLib.D3.API.Input.Editor(
objectComponent.id,
objectComponent.name,
objectComponent.domElementId,
objectComponent.camera
);
return new GameLib.D3.Input.Editor(
graphics,
this,
apiInputEditor
);
};

View File

@ -0,0 +1,285 @@
/**
* Input parent class
* @param graphics GameLib.D3.Graphics
* @param parentObject
* @param apiInputFly GameLib.D3.API.Input.Fly
* @constructor
*/
GameLib.D3.Input.Fly = function RuntimeEditorInput(
graphics,
parentObject,
apiInputFly
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.D3.API.Input.Fly.call(
this,
apiInputFly.id,
apiInputFly.name,
apiInputFly.domElementId,
apiInputFly.camera
);
this.mouseUpCallback = this.onMouseUp.bind(this);
this.mouseDownCallback = this.onMouseDown.bind(this);
this.mouseMoveCallback = this.onMouseMove.bind(this);
this.mouseWheelCallback = this.onMouseWheel.bind(this);
this.keyDownCallback = this.onKeyDown.bind(this);
this.keyUpCallback = this.onKeyUp.bind(this);
this.camera = camera;
this.canvas.addEventListener('keydown', this.keyDownCallback, false);
this.canvas.addEventListener('keyup', this.keyUpCallback, false);
this.canvas.addEventListener('mousedown', this.mouseDownCallback, false);
this.canvas.addEventListener('mouseup', this.mouseUpCallback, false);
this.canvas.addEventListener('mousewheel', this.mouseWheelCallback, false);
this.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
this.element = document.body;
if (this.havePointerLock) {
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
/**
* Don't create an instance here - since we don't have our camera loaded yet...
*/
};
GameLib.D3.Input.Fly.prototype = Object.create(GameLib.D3.API.Input.Fly.prototype);
GameLib.D3.Input.Fly.prototype.constructor = GameLib.D3.Input.Fly;
GameLib.D3.Input.Fly.prototype.createInstance = function(update) {
//todo
};
GameLib.D3.Input.Fly.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Input.Fly.prototype.toApiComponent = function() {
var apiInputFly = new GameLib.D3.API.Input.Fly(
this.id,
this.name,
this.domElementId,
GameLib.Utils.IdOrNull(this.camera)
);
return apiInputFly;
};
GameLib.D3.Input.Fly.FromObjectComponent = function(graphics, objectComponent) {
var apiInputFly = new GameLib.D3.API.Input.Fly(
objectComponent.id,
objectComponent.name,
objectComponent.domElementId,
objectComponent.camera
);
return new GameLib.D3.Input.Fly(
graphics,
this,
apiInputFly
);
};
/**
* Go forward / backward on mouse wheel
* @param event
*/
GameLib.D3.Input.Fly.prototype.onMouseWheel = function(event) {
this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001);
event.preventDefault();
this.moveForward = false;
};
/**
* Start rotating the camera on mouse middle button down
* @param event
*/
GameLib.D3.Input.Fly.prototype.onMouseDown = function(event) {
if (event.button == 1) {
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
}
};
/**
* Stop rotating on middle mouse button down
* @param event
*/
GameLib.D3.Input.Fly.prototype.onMouseUp = function(event) {
if (event.button == 1) {
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
/**
* Apply current yaw and pitch to camera
*/
GameLib.D3.Input.Fly.prototype.applyRotation = function() {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
/**
* Apply current position to camera
* @param deltaTime
*/
GameLib.D3.Input.Fly.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
direction = direction.applyEuler(rotation);
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
if(this.moveForward) {
this.camera.position.x += forward.x * (deltaTime * this.flySpeed);
this.camera.position.y += forward.y * (deltaTime * this.flySpeed);
this.camera.position.z += forward.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
this.camera.position.x -= forward.x * (deltaTime * this.flySpeed);
this.camera.position.y -= forward.y * (deltaTime * this.flySpeed);
this.camera.position.z -= forward.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
this.camera.position.x -= right.x * (deltaTime * this.flySpeed);
this.camera.position.y -= right.y * (deltaTime * this.flySpeed);
this.camera.position.z -= right.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
this.camera.position.x += right.x * (deltaTime * this.flySpeed);
this.camera.position.y += right.y * (deltaTime * this.flySpeed);
this.camera.position.z += right.z * (deltaTime * this.flySpeed);
}
if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed);
} else if(this.moveDown) {
this.camera.position.y -= (deltaTime * this.flySpeed);
}
};
/**
* This update function should be called from the animation function in order to apply the 'frame rate independent'
* movement to the camera
* @param deltaTime
*/
GameLib.D3.Input.Fly.prototype.update = function(deltaTime) {
this.applyRotation();
this.applyTranslation(deltaTime);
};
/**
* Rotate on mouse move
* @param event
*/
GameLib.D3.Input.Fly.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.yaw -= movementX * 0.002;
this.pitch -= movementY * 0.002;
}
};
/**
* Keyboard controls
* @param event
*/
GameLib.D3.Input.Fly.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 87: // w
this.moveForward = true;
break;
case 65: // a
this.moveLeft = true;
break;
case 83: // s
this.moveBackward = true;
break;
case 68: // d
this.moveRight = true;
break;
case 104: // keypad up arrow
this.moveUp = true;
break;
case 98: // keypad down arrow
this.moveDown = true;
break;
}
};
/**
* Keyboard controls
* @param event
*/
GameLib.D3.Input.Fly.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
this.moveForward = false;
break;
case 37: // left
case 65: // a
this.moveLeft = false;
break;
case 40: // down
case 83: // s
this.moveBackward = false;
break;
case 39: // right
case 68: // d
this.moveRight = false;
break;
case 104: // keypad up arrow
this.moveUp = false;
break;
case 98: // keypad down arrow
this.moveDown = false;
break;
}
};

View File

@ -20,33 +20,18 @@ GameLib.D3.LookAt = function RuntimeLookAt(
GameLib.D3.API.LookAt.call(
this,
apiLookAt.id,
apiLookAt.name,
apiLookAt.currentPosition,
apiLookAt.targetPosition,
apiLookAt.targetPositionOffset,
apiLookAt.rotationSpeed,
apiLookAt.lookAtMatrix,
apiLookAt.up,
apiLookAt.currentRotation
apiLookAt.id,
apiLookAt.name,
apiLookAt.currentComponent,
apiLookAt.targetComponent,
apiLookAt.targetPositionOffset,
apiLookAt.rotationSpeed
);
this.currentPosition = new GameLib.Vector3(
this.targetPositionOffset = new GameLib.Vector3(
this.graphics,
this,
this.currentPosition
);
this.targetPosition = new GameLib.Vector3(
this.graphics,
this,
this.targetPosition
);
this.targetOffset = new GameLib.Vector3(
this.graphics,
this,
this.targetOffset
this.targetPositionOffset
);
this.lookAtMatrix = new GameLib.Matrix4(
@ -70,3 +55,43 @@ GameLib.D3.LookAt = function RuntimeLookAt(
GameLib.D3.LookAt.prototype = Object.create(GameLib.D3.API.LookAt.prototype);
GameLib.D3.LookAt.prototype.constructor = GameLib.D3.LookAt;
GameLib.D3.LookAt.prototype.toApiComponent = function() {
var apiLookAt = new GameLib.D3.API.LookAt(
this.id,
this.name,
GameLib.Utils.IdOrNull(this.currentComponent),
GameLib.Utils.IdOrNull(this.targetComponent),
this.targetPositionOffset.toApiVector(),
this.rotationSpeed
);
return apiLookAt;
};
GameLib.D3.LookAt.FromObjectComponent = function(graphics, objectComponent) {
var apiLookAt = new GameLib.D3.API.LookAt(
objectComponent.id,
objectComponent.name,
objectComponent.currentComponent,
objectComponent.targetComponent,
objectComponent.targetPositionOffset,
objectComponent.rotationSpeed
);
return new GameLib.D3.LookAt(
graphics,
this,
apiLookAt
);
};
/**
* Updates the component
* @param deltaTime
*/
GameLib.D3.LookAt.prototype.update = function(deltaTime) {
};

View File

@ -24,7 +24,8 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing(
apiPathFollowing.id,
apiPathFollowing.name,
apiPathFollowing.spline,
apiPathFollowing.raytraceMesh,
apiPathFollowing.mesh,
apiPathFollowing.raytraceMesh,
apiPathFollowing.accelleration,
apiPathFollowing.maxSpeed,
apiPathFollowing.baseOffset,
@ -113,8 +114,9 @@ GameLib.D3.PathFollowing.prototype.toApiComponent = function() {
var apiPathFollowing = new GameLib.D3.API.PathFollowing(
this.id,
this.name,
this.spline,
this.raytraceMesh,
GameLib.Utils.IdOrNull(this.spline),
GameLib.Utils.IdOrNull(this.mesh),
GameLib.Utils.IdOrNull(this.raytraceMesh),
this.accelleration,
this.maxSpeed,
this.baseOffset.toApiVector(),
@ -142,6 +144,7 @@ GameLib.D3.PathFollowing.FromObjectComponent = function(graphics, objectComponen
objectComponent.id,
objectComponent.name,
objectComponent.spline,
objectComponent.mesh,
objectComponent.raytraceMesh,
objectComponent.accelleration,
objectComponent.maxSpeed,
@ -216,4 +219,73 @@ GameLib.D3.PathFollowing.FromObjectComponent = function(graphics, objectComponen
this,
apiPathFollowing
);
};
/**
* Updates the component
* @param deltaTime
*/
GameLib.D3.PathFollowing.prototype.update = function(deltaTime) {
if (this.spline && this.mesh && this.raytraceMesh) {
this.currentSpeed += this.accelleration * deltaTime * this.direction;
if(this.currentSpeed > this.maxSpeed) {
this.currentSpeed = this.maxSpeed;
}
this.grain = (this.currentSpeed / 100.0);
this.currentPosition = this.spline.getPointAt(this.currentPathValue);
this.currentPathValue += this.grain;
if (this.currentPathValue >= 1) {
this.currentPathValue = this.currentPathValue - 1;
}
if (this.currentPathValue < 0) {
this.currentPathValue = 0.0;
}
this.futurePosition = this.spline.getPointAt(this.currentPathValue);
this.raycaster.setPosition(
this.futurePosition
);
var normal = this.raycaster.getFaceNormal(this.raytraceMesh);
if (normal) {
this.up.x = this.mx(normal.x);
this.up.y = this.my(normal.y);
this.up.z = this.mz(normal.z);
this.up.updateInstance();
}
this.rotationMatrix.lookAt(
this.currentPosition,
this.futurePosition,
this.up
);
this.rotationVector.setFromRotationMatrix(this.rotationMatrix);
/**
* Update Position
*/
this.mesh.position.x = this.futurePosition.x;// + transformedOffset.x;
this.mesh.position.y = this.futurePosition.y;// + transformedOffset.y;
this.mesh.position.z = this.futurePosition.z;// + transformedOffset.z;
/**
* Update Rotation
*/
this.mesh.quaternion.x = this.rotationVector.x;
this.mesh.quaternion.y = this.rotationVector.y;
this.mesh.quaternion.z = this.rotationVector.z;
this.mesh.quaternion.w = this.rotationVector.w;
this.mesh.updateInstance();
}
};

View File

@ -0,0 +1,57 @@
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
* @param graphics GameLib.D3.Graphics
* @param parentObject
* @param apiRenderable GameLib.D3.API.Renderable
* @constructor
*/
GameLib.D3.Renderable = function RuntimeRenderable(
graphics,
parentObject,
apiRenderable
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.D3.API.Renderable.call(
this,
apiRenderable.id,
apiRenderable.name,
apiRenderable.visible
);
};
GameLib.D3.Renderable.prototype = Object.create(GameLib.D3.API.Renderable.prototype);
GameLib.D3.Renderable.prototype.constructor = GameLib.D3.Renderable;
GameLib.D3.Renderable.prototype.toApiComponent = function() {
var apiRenderable = new GameLib.D3.API.Renderable(
this.id,
this.name,
this.visible
);
return apiRenderable;
};
GameLib.D3.Renderable.FromObjectComponent = function(graphics, objectComponent) {
var apiRenderable = new GameLib.D3.API.Renderable(
objectComponent.id,
objectComponent.name,
objectComponent.visible
);
return new GameLib.D3.Renderable(
graphics,
this,
apiRenderable
);
};

View File

@ -0,0 +1,95 @@
/**
* Renders a scene with a camera
* @param graphics GameLib.D3.Graphics
* @param parentObject
* @param apiRenderer GameLib.D3.API.Renderer
* @constructor
*/
GameLib.D3.Renderer = function RuntimeRenderer(
graphics,
parentObject,
apiRenderer
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.D3.API.Renderer.call(
this,
apiRenderer.id,
apiRenderer.name,
apiRenderer.scene,
apiRenderer.camera,
apiRenderer.autoClear,
apiRenderer.localClipping,
apiRenderer.width,
apiRenderer.height
);
this.instance = this.createInstance();
};
GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype);
GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer;
GameLib.D3.Renderer.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = new THREE.WebGLRenderer();
}
instance.localClippingEnabled = this.localClipping;
instance.setSize(this.width, this.height);
instance.autoClear = this.autoClear;
this.instance = instance;
return instance;
};
GameLib.D3.Renderer.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Renderer.prototype.toApiComponent = function() {
var apiRenderer = new GameLib.D3.API.Renderer(
this.id,
this.name,
GameLib.Utils.IdOrNull(this.scene),
GameLib.Utils.IdOrNull(this.camera),
this.autoClear
);
return apiRenderer;
};
GameLib.D3.Renderer.FromObjectComponent = function(graphics, objectComponent) {
var apiRenderer = new GameLib.D3.API.Renderer(
objectComponent.id,
objectComponent.name,
objectComponent.scene,
objectComponent.camera,
objectComponent.autoClear
);
return new GameLib.D3.Renderer(
graphics,
this,
apiRenderer
);
};
GameLib.D3.Renderer.prototype.render = function(deltaTime) {
this.instance.render(this.scene.instance, this.camera.instance);
};

View File

@ -75,29 +75,66 @@ GameLib.EntityManager.prototype.removeEntity = function(entity) {
*/
GameLib.EntityManager.prototype.query = function(components) {
return this.entities.map(
var result = this.entities.reduce(
function(__queryComponents) {
return function(entity) {
__queryComponents.map(
return function(result, entity) {
var results = __queryComponents.reduce(
function(__entity) {
return function(queryComponent) {
__entity.components.map(
return function(componentResult, queryComponent) {
var components = __entity.components.reduce(
function(__queryComponent) {
return function(entityComponent) {
if (__queryComponent == entityComponent.constructor.name) {
return function(__components, entityComponent) {
if (__queryComponent == entityComponent.constructor) {
// arrow should point towards a window or sergej --->
return entityComponent;
__components[entityComponent.id] = entityComponent;
}
return __components;
}
}(queryComponent)
}(queryComponent),
{}
);
for (var property in components) {
if (components.hasOwnProperty(property)) {
componentResult[property] = components[property];
}
}
return componentResult;
}
}(entity)
)
}(entity),
{}
);
for (var property in results) {
if (results.hasOwnProperty(property)) {
result[property] = results[property];
}
}
return result;
}
}(components)
}(components),
{}
);
var array = [];
for (var property in result) {
if (result.hasOwnProperty(property)) {
array.push(result[property]);
}
}
return array;
};
/**
@ -128,13 +165,22 @@ GameLib.EntityManager.prototype.toApiEntityManager = function() {
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) {
var apiEntities = objectEntities.entities.map(
function (objectEntity) {
objectEntity.components = objectEntity.components.map(
function (component) {
if (component instanceof Object) {
if (component.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) {
if (component.componentType === GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) {
return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component);
} else if (component.componentType === GameLib.Component.COMPONENT_RENDERABLE) {
return GameLib.D3.Renderable.FromObjectComponent(graphics, component);
} else if (component.componentType === GameLib.Component.COMPONENT_RENDERER) {
return GameLib.D3.Renderer.FromObjectComponent(graphics, component);
} else if (component.componentType === GameLib.Component.COMPONENT_EDITOR_INPUT) {
return GameLib.D3.Input.Editor.FromObjectComponent(graphics, component);
} else if (component.componentType === GameLib.Component.COMPONENT_LOOK_AT) {
return GameLib.D3.LookAt.FromObjectComponent(graphics, component);
} else {
console.warn('no component was associated with this object');
throw new Error('no component was associated with this object');
@ -190,25 +236,15 @@ GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
} else {
array[index] = idToObject[componentId];
}
array[index].linkedObjects.map(
function (propertyName) {
array[index][propertyName] = idToObject[array[index][propertyName]];
}
)
}
)
}
);
// TODO: fix
// this.components.forEach(
// function(currentValue, index, array) {
//
// if (!idToObject[currentValue]) {
// //throw new Error('Unable to locate component with ID: ' + currentValue);
// console.log('Unable to locate component with ID: ' + currentValue + ' - it must have been deleted before');
// array.splice(index, 1);
// } else {
// array[index] = idToObject[currentValue];
// }
//
//
// }.bind(this)
// );
};

View File

@ -13,8 +13,25 @@ GameLib.System = function(
};
GameLib.System.SYSTEM_TYPE_RENDER = 0x1;
GameLib.System.SYSTEM_TYPE_ANIMATION = 0x2;
GameLib.System.SYSTEM_TYPE_INPUT = 0x3;
GameLib.System.SYSTEM_TYPE_ALL = 0x4;
/**
* @callback
* @override
*/
GameLib.System.prototype.update = function() {};
GameLib.System.prototype.start = function() {};
/**
* @callback
* @override
*/
GameLib.System.prototype.update = function() {};
/**
* @callback
* @override
*/
GameLib.System.prototype.stop = function() {};

View File

@ -14,80 +14,25 @@ GameLib.System.Animation = function(
GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Animation.prototype.constructor = GameLib.System.Animation;
GameLib.System.Animation.prototype.start = function() {
};
/**
* @override
*/
GameLib.System.prototype.update = function() {
GameLib.System.Animation.prototype.update = function(deltaTime) {
var objects = this.entityManager.queryComponents([GameLib.D3.Mesh, GameLib.D3.Animation]);
var objects = this.entityManager.query([GameLib.D3.PathFollowing]);
objects.forEach(function(object) {
object.update(deltaTime);
});
/**
* TODO: PathFollowingComponent Stuff
*
*
* if (this.spline && this.raytraceMesh) {
this.currentSpeed += this.accelleration * deltaTime * this.direction;
if(this.currentSpeed > this.maxSpeed) {
this.currentSpeed = this.maxSpeed;
}
this.grain = (this.currentSpeed / 100.0);
this.currentPosition = this.spline.getPointAt(this.currentPathValue);
this.currentPathValue += this.grain;
if (this.currentPathValue >= 1) {
this.currentPathValue = this.currentPathValue - 1;
}
if (this.currentPathValue < 0) {
this.currentPathValue = 0.0;
}
this.futurePosition = this.spline.getPointAt(this.currentPathValue);
this.raycaster.setPosition(
this.futurePosition
);
var normal = this.raycaster.getFaceNormal(this.raytraceMesh);
if (normal) {
this.up.x = this.mx(normal.x);
this.up.y = this.my(normal.y);
this.up.z = this.mz(normal.z);
this.up.updateInstance();
}
this.rotationMatrix.lookAt(
this.currentPosition,
this.futurePosition,
this.up
);
this.rotationVector.setFromRotationMatrix(this.rotationMatrix);
/**
* Update Position
*
this.parentEntity.position.x = this.futurePosition.x;// + transformedOffset.x;
this.parentEntity.position.y = this.futurePosition.y;// + transformedOffset.y;
this.parentEntity.position.z = this.futurePosition.z;// + transformedOffset.z;
/**
* Update Rotation
*
this.parentEntity.quaternion.x = this.rotationVector.x;
this.parentEntity.quaternion.y = this.rotationVector.y;
this.parentEntity.quaternion.z = this.rotationVector.z;
this.parentEntity.quaternion.w = this.rotationVector.w;
}
*
TODO: lookat component code
@ -177,4 +122,7 @@ GameLib.System.prototype.update = function() {
}
*/
};
GameLib.System.Animation.prototype.stop = function() {
};

View File

@ -14,229 +14,51 @@ GameLib.System.Input = function(
GameLib.System.Input.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Input.prototype.constructor = GameLib.System.Input;
GameLib.System.Input.prototype.start = function() {
var objects = this.entityManager.query([GameLib.D3.Input.Editor]);
objects.forEach(
function(object) {
if (!object.instance) {
object.createInstance();
}
}
);
};
/**
* @override
*/
GameLib.System.prototype.update = function() {
GameLib.System.Input.prototype.update = function(deltaTime) {
var objects = this.entityManager.queryComponents([GameLib.D3.FlyControls, GameLib.D3.Input]);
objects.forEach(function(object) {
});
/**
* TODO : flycontrols stuff
* this.mouseUpCallback = this.onMouseUp.bind(this);
this.mouseDownCallback = this.onMouseDown.bind(this);
this.mouseMoveCallback = this.onMouseMove.bind(this);
this.mouseWheelCallback = this.onMouseWheel.bind(this);
this.keyDownCallback = this.onKeyDown.bind(this);
this.keyUpCallback = this.onKeyUp.bind(this);
this.camera = camera;
this.canvas.addEventListener('keydown', this.keyDownCallback, false);
this.canvas.addEventListener('keyup', this.keyUpCallback, false);
this.canvas.addEventListener('mousedown', this.mouseDownCallback, false);
this.canvas.addEventListener('mouseup', this.mouseUpCallback, false);
this.canvas.addEventListener('mousewheel', this.mouseWheelCallback, false);
this.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
this.element = document.body;
if (this.havePointerLock) {
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
//TODO: Fly controls (and some others) have an update function which need to get executed here
// var objects = this.entityManager.query([GameLib.D3.Input.Editor]);
//
// objects.forEach(
// function(object) {
// if (!object.instance) {
// object.instance = object.createInstance();
// }
// }
// );
/**
* Go forward / backward on mouse wheel
* @param event
*
GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001);
event.preventDefault();
this.moveForward = false;
};
/**
* Start rotating the camera on mouse middle button down
* @param event
*
GameLib.D3.FlyControls.prototype.onMouseDown = function(event) {
if (event.button == 1) {
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
};
GameLib.System.Input.prototype.stop = function() {
var objects = this.entityManager.query([GameLib.D3.Input.Editor]);
objects.forEach(
function (object) {
if (object.instance) {
object.instance.enabled = false;
delete object.instance;
object.instance = null;
}
}
};
/**
* Stop rotating on middle mouse button down
* @param event
*
GameLib.D3.FlyControls.prototype.onMouseUp = function(event) {
if (event.button == 1) {
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
/**
* Apply current yaw and pitch to camera
*
GameLib.D3.FlyControls.prototype.applyRotation = function() {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
/**
* Apply current position to camera
* @param deltaTime
*
GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
direction = direction.applyEuler(rotation);
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
if(this.moveForward) {
this.camera.position.x += forward.x * (deltaTime * this.flySpeed);
this.camera.position.y += forward.y * (deltaTime * this.flySpeed);
this.camera.position.z += forward.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
this.camera.position.x -= forward.x * (deltaTime * this.flySpeed);
this.camera.position.y -= forward.y * (deltaTime * this.flySpeed);
this.camera.position.z -= forward.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
this.camera.position.x -= right.x * (deltaTime * this.flySpeed);
this.camera.position.y -= right.y * (deltaTime * this.flySpeed);
this.camera.position.z -= right.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
this.camera.position.x += right.x * (deltaTime * this.flySpeed);
this.camera.position.y += right.y * (deltaTime * this.flySpeed);
this.camera.position.z += right.z * (deltaTime * this.flySpeed);
}
if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed);
} else if(this.moveDown) {
this.camera.position.y -= (deltaTime * this.flySpeed);
}
};
/**
* This update function should be called from the animation function in order to apply the 'frame rate independent'
* movement to the camera
* @param deltaTime
*
GameLib.D3.FlyControls.prototype.update = function(deltaTime) {
this.applyRotation();
this.applyTranslation(deltaTime);
};
/**
* Rotate on mouse move
* @param event
*
GameLib.D3.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.yaw -= movementX * 0.002;
this.pitch -= movementY * 0.002;
}
};
/**
* Keyboard controls
* @param event
*
GameLib.D3.FlyControls.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 87: // w
this.moveForward = true;
break;
case 65: // a
this.moveLeft = true;
break;
case 83: // s
this.moveBackward = true;
break;
case 68: // d
this.moveRight = true;
break;
case 104: // keypad up arrow
this.moveUp = true;
break;
case 98: // keypad down arrow
this.moveDown = true;
break;
}
};
/**
* Keyboard controls
* @param event
*
GameLib.D3.FlyControls.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
this.moveForward = false;
break;
case 37: // left
case 65: // a
this.moveLeft = false;
break;
case 40: // down
case 83: // s
this.moveBackward = false;
break;
case 39: // right
case 68: // d
this.moveRight = false;
break;
case 104: // keypad up arrow
this.moveUp = false;
break;
case 98: // keypad down arrow
this.moveDown = false;
break;
}
};
*/
};
);
}

View File

@ -3,40 +3,100 @@
* @constructor
*/
GameLib.System.Render = function(
entityManager
entityManager,
domElement,
stats
) {
GameLib.System.call(
this,
entityManager
);
if (GameLib.Utils.UndefinedOrNull(domElement)) {
domElement = null;
}
this.domElement = domElement;
if (GameLib.Utils.UndefinedOrNull(stats)) {
stats = null;
}
this.stats = stats;
};
GameLib.System.Render.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Render.prototype.constructor = GameLib.System.Render;
/**
*
*/
GameLib.System.Render.prototype.start = function() {
this.domElement.innerHTML = '';
this.domElement.appendChild(this.stats.dom);
var renderers = this.entityManager.query([GameLib.D3.Renderer]);
renderers.forEach(
function (renderer) {
if (!renderer.instance) {
renderer.createInstance();
} else {
renderer.updateInstance();
}
this.domElement.appendChild(renderer.instance.domElement);
}.bind(this)
);
};
/**
* @override
*/
GameLib.System.prototype.update = function() {
GameLib.System.Render.prototype.update = function(deltaTime) {
var objects = this.entityManager.queryComponents([GameLib.D3.Camera]);
var renderers = this.entityManager.query([GameLib.D3.Renderer]);
objects.forEach(function(object) {
renderers.forEach(
function (renderer) {
renderer.render(deltaTime);
}
);
//TODO camera component stuff
object.quaternion.x = this.parentEntity.quaternion.x;
object.quaternion.y = this.parentEntity.quaternion.y;
object.quaternion.z = this.parentEntity.quaternion.z;
object.quaternion.w = this.parentEntity.quaternion.w;
// renderObjects.forEach(function(object) {
//
// //TODO camera component stuff
// object.quaternion.x = this.parentEntity.quaternion.x;
// object.quaternion.y = this.parentEntity.quaternion.y;
// object.quaternion.z = this.parentEntity.quaternion.z;
// object.quaternion.w = this.parentEntity.quaternion.w;
//
// object.position.x = this.parentEntity.position.x;
// object.position.y = this.parentEntity.position.y;
// object.position.z = this.parentEntity.position.z;
//
// object.updateInstance();
//
// //TODO scene component stuff
// renderer.render(this.instance, this.cameras[this.activeCameraIndex].instance);
// });
object.position.x = this.parentEntity.position.x;
object.position.y = this.parentEntity.position.y;
object.position.z = this.parentEntity.position.z;
};
object.updateInstance();
GameLib.System.Render.prototype.stop = function() {
//TODO scene component stuff
renderer.render(this.instance, this.cameras[this.activeCameraIndex].instance);
});
this.domElement.innerHTML = '';
var renderers = this.entityManager.query([GameLib.D3.Renderer]);
renderers.forEach(
function (renderer) {
if (renderer.instance) {
delete renderer.instance;
renderer.instance = null;
}
}.bind(this)
);
};

View File

@ -10,6 +10,31 @@ GameLib.Utils.Extend = function(
}
};
// GameLib.Utils.ObjectFactory = function() {
//
// var promiseList = {};
//
// return function(objectId) {
//
// if (!objectId) {
// console.log('No Object ID specified ' + objectId);
// throw new Error('No Object ID specified ' + objectId);
// }
//
// if (promiseList[objectId]) {
// return promiseList[objectId];
// }
//
// var defer = Q.defer();
//
// promiseList[objectId] = defer.promise;
//
// GameLib.Utils.ObjectFactory.Link(idToObject, objectId, defer);
//
// return promiseList[objectId];
// }
// };
GameLib.Utils.UndefinedOrNull = function (
variable
) {