many nice refactorings, publisher and subscriber patterns, grids

beta.r3js.org
Theunis J. Botha 2017-06-01 16:58:22 +02:00
parent 63d175a8d7
commit 1eeeecdd66
14 changed files with 191 additions and 156 deletions

62
src/game-lib-a-1-event.js Normal file
View File

@ -0,0 +1,62 @@
/**
* Event Core
* @constructor
*/
GameLib.Event = function() {
};
/**
* Some nice Events handling
* @type {{}}
*/
GameLib.Event.Subscriptions = {};
/**
* Events we can subscribe to and publish
*/
GameLib.Event.WINDOW_RESIZE = 0x1;
/**
* Subscribe to some events
* @param eventName
* @param callback
*/
GameLib.Event.prototype.subscribe = function(
eventName,
callback
) {
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
GameLib.Event.Subscriptions[eventName].push(callback.bind(this));
} else {
GameLib.Event.Subscriptions[eventName] = [];
GameLib.Event.Subscriptions[eventName].push(callback.bind(this));
}
};
/**
* Publish some event happened with some data
* @param eventName
* @param data
*/
GameLib.Event.prototype.publish = function(
eventName,
data
) {
GameLib.Event.Emit(eventName, data);
};
/**
* Static method call
*/
GameLib.Event.Emit = function(eventName, data) {
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
GameLib.Event.Subscriptions[eventName].map(
function(callback) {
callback(data);
}
)
}
};

View File

@ -37,6 +37,9 @@ GameLib.API.Component = function(
this.selected = selected; this.selected = selected;
}; };
GameLib.API.Component.prototype = Object.create(GameLib.Event.prototype);
GameLib.API.Component.prototype.constructor = GameLib.API.Component;
/** /**
* Returns an API component from an object component * Returns an API component from an object component
* @param objectComponent (should be an ID string - components get loaded and linked) * @param objectComponent (should be an ID string - components get loaded and linked)

View File

@ -4,7 +4,6 @@
* @param name String * @param name String
* @param domElement * @param domElement
* @param camera * @param camera
* @param selectDelayMs
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -13,7 +12,6 @@ GameLib.D3.API.Input.Editor = function (
name, name,
domElement, domElement,
camera, camera,
selectDelayMs,
parentEntity parentEntity
) { ) {
GameLib.Component.call( GameLib.Component.call(
@ -63,7 +61,6 @@ GameLib.D3.API.Input.Editor.FromObjectComponent = function(objectComponent) {
objectComponent.name, objectComponent.name,
objectComponent.domElement, objectComponent.domElement,
objectComponent.camera, objectComponent.camera,
objectComponent.selectDelayMs,
objectComponent.parentEntity objectComponent.parentEntity
); );
}; };

View File

@ -11,6 +11,7 @@
* @param lights [GameLib.D3.API.Light] * @param lights [GameLib.D3.API.Light]
* @param textures [GameLib.D3.API.Texture] * @param textures [GameLib.D3.API.Texture]
* @param materials [GameLib.D3.API.Material] * @param materials [GameLib.D3.API.Material]
* @param activeCamera [GameLib.D3.Camera]
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -26,6 +27,7 @@ GameLib.D3.API.Scene = function(
lights, lights,
textures, textures,
materials, materials,
activeCamera,
parentEntity parentEntity
) { ) {
GameLib.Component.call( GameLib.Component.call(
@ -36,7 +38,8 @@ GameLib.D3.API.Scene = function(
'meshes' : [GameLib.D3.Mesh], 'meshes' : [GameLib.D3.Mesh],
'lights' : [GameLib.D3.Light], 'lights' : [GameLib.D3.Light],
'textures' : [GameLib.D3.Texture], 'textures' : [GameLib.D3.Texture],
'materials' : [GameLib.D3.Material] 'materials' : [GameLib.D3.Material],
'activeCamera' : GameLib.D3.Camera
}, },
false, false,
parentEntity parentEntity
@ -98,6 +101,11 @@ GameLib.D3.API.Scene = function(
} }
this.materials = materials; this.materials = materials;
if (GameLib.Utils.UndefinedOrNull(activeCamera)) {
activeCamera = null;
}
this.activeCamera = activeCamera;
}; };
GameLib.D3.API.Scene.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Scene.prototype = Object.create(GameLib.Component.prototype);
@ -121,6 +129,8 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
var apiQuaternion = new GameLib.API.Quaternion(); var apiQuaternion = new GameLib.API.Quaternion();
var apiScale = new GameLib.API.Vector3(1,1,1); var apiScale = new GameLib.API.Vector3(1,1,1);
var apiActiveCamera = null;
/** /**
* Passed in ImageFactory overrides API image factory * Passed in ImageFactory overrides API image factory
*/ */
@ -174,6 +184,10 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
apiScale = GameLib.API.Vector3.FromObjectVector(objectScene.scale); apiScale = GameLib.API.Vector3.FromObjectVector(objectScene.scale);
} }
if (objectScene.activeCamera) {
apiActiveCamera = objectScene.activeCamera;
}
return new GameLib.D3.API.Scene( return new GameLib.D3.API.Scene(
objectScene.id, objectScene.id,
objectScene.name, objectScene.name,
@ -186,6 +200,7 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
apiLights, apiLights,
apiTextures, apiTextures,
apiMaterials, apiMaterials,
apiActiveCamera,
objectScene.parentEntity objectScene.parentEntity
); );

View File

@ -6,10 +6,6 @@
* @param height * @param height
* @param x * @param x
* @param y * @param y
* @param composer GameLib.D3.API.Composer
* @param renderer GameLib.D3.API.Renderer
* @param scene GameLib.D3.API.Scene
* @param camera GameLib.D3.API.Camera
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -20,21 +16,12 @@ GameLib.D3.API.Viewport = function(
height, height,
x, x,
y, y,
composer,
renderer,
scene,
camera,
parentEntity parentEntity
) { ) {
GameLib.Component.call( GameLib.Component.call(
this, this,
GameLib.Component.COMPONENT_VIEWPORT, GameLib.Component.COMPONENT_VIEWPORT,
{ null,
'composer' : GameLib.D3.Composer,
'renderer' : GameLib.D3.Renderer,
'scene' : GameLib.D3.Scene,
'camera' : GameLib.D3.Camera
},
null, null,
parentEntity parentEntity
); );
@ -68,27 +55,6 @@ GameLib.D3.API.Viewport = function(
y = 0; y = 0;
} }
this.y = y; this.y = y;
if (GameLib.Utils.UndefinedOrNull(composer)) {
composer = null;
}
this.composer = composer;
if (GameLib.Utils.UndefinedOrNull(renderer)) {
renderer = null;
}
this.renderer = renderer;
if (GameLib.Utils.UndefinedOrNull(scene)) {
scene = null;
}
this.scene = scene;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.camera = camera;
}; };
GameLib.D3.API.Viewport.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Viewport.prototype = Object.create(GameLib.Component.prototype);
@ -107,10 +73,6 @@ GameLib.D3.API.Viewport.FromObjectViewport = function(objectViewport) {
objectViewport.height, objectViewport.height,
objectViewport.x, objectViewport.x,
objectViewport.y, objectViewport.y,
objectViewport.composer,
objectViewport.renderer,
objectViewport.scene,
objectViewport.camera,
objectViewport.parentEntity objectViewport.parentEntity
); );
}; };

View File

@ -112,8 +112,8 @@ GameLib.D3.Camera.prototype.createInstance = function(update) {
instance = new THREE.OrthographicCamera( instance = new THREE.OrthographicCamera(
this.minX, this.minX,
this.maxX, this.maxX,
this.minY,
this.maxY, this.maxY,
this.minY,
this.minZ, this.minZ,
this.maxZ this.maxZ
); );
@ -125,12 +125,12 @@ GameLib.D3.Camera.prototype.createInstance = function(update) {
if (update) { if (update) {
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance.minX = this.minX; instance.left = this.minX;
instance.maxX = this.maxX; instance.right = this.maxX;
instance.minY = this.minY; instance.bottom = this.minY;
instance.maxY = this.maxY; instance.top = this.maxY;
instance.minZ = this.minZ; instance.near = this.minZ;
instance.maxZ = this.maxZ; instance.far = this.maxZ;
} }
if ( if (
@ -178,6 +178,16 @@ GameLib.D3.Camera.prototype.updateInstance = function() {
this.instance = this.createInstance(true); this.instance = this.createInstance(true);
}; };
/**
* Resize default
* @param width
* @param height
*/
GameLib.D3.Camera.prototype.resize = function(width, height) {
camera.aspect = width / height;
camera.updateInstance();
};
/** /**
* Converts a GameLib.D3.Camera to a new GameLib.D3.API.Camera * Converts a GameLib.D3.Camera to a new GameLib.D3.API.Camera
* @returns {GameLib.D3.API.Camera} * @returns {GameLib.D3.API.Camera}

View File

@ -26,7 +26,6 @@ GameLib.D3.Input.Editor = function (
apiInputEditor.name, apiInputEditor.name,
apiInputEditor.domElement, apiInputEditor.domElement,
apiInputEditor.camera, apiInputEditor.camera,
apiInputEditor.selectDelayMs,
apiInputEditor.parentEntity apiInputEditor.parentEntity
); );
@ -56,6 +55,8 @@ GameLib.D3.Input.Editor = function (
this.mouseMove = null; this.mouseMove = null;
this.mouseDown = null; this.mouseDown = null;
this.keyDown = null; this.keyDown = null;
this.mouseUp = null;
this.mouseWheel = null;
this.raycaster = new GameLib.D3.Raycaster( this.raycaster = new GameLib.D3.Raycaster(
this.graphics this.graphics
@ -98,7 +99,6 @@ GameLib.D3.Input.Editor.prototype.toApiObject = function() {
this.name, this.name,
this.domElementId, this.domElementId,
GameLib.Utils.IdOrNull(this.camera), GameLib.Utils.IdOrNull(this.camera),
this.selectDelayMs,
GameLib.Utils.IdOrNull(this.parentEntity) GameLib.Utils.IdOrNull(this.parentEntity)
); );
@ -148,12 +148,28 @@ GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity) {
* @returns {Function} * @returns {Function}
*/ */
GameLib.D3.Input.Editor.prototype.onMouseMove = function(entity) { GameLib.D3.Input.Editor.prototype.onMouseMove = function(entity) {
return function(event) { return function(event) {
} }
};
/**
*
* @param entity
* @returns {Function}
*/
GameLib.D3.Input.Editor.prototype.onMouseUp = function(entity) {
return function(event) {
};
};
/**
*
* @param entity
* @returns {Function}
*/
GameLib.D3.Input.Editor.prototype.onMouseWheel = function(entity) {
return function(event) {
};
}; };
/** /**

View File

@ -40,6 +40,7 @@ GameLib.D3.Scene = function (
apiScene.lights, apiScene.lights,
apiScene.textures, apiScene.textures,
apiScene.materials, apiScene.materials,
apiScene.activeCamera,
apiScene.parentEntity apiScene.parentEntity
); );
@ -150,6 +151,13 @@ GameLib.D3.Scene = function (
}.bind(this) }.bind(this)
); );
if (this.activeCamera instanceof GameLib.D3.API.Camera) {
this.activeCamera = new GameLib.D3.Camera(
this.graphics,
this.activeCamera
);
}
this.idToObject[this.id] = this; this.idToObject[this.id] = this;
this.linkObjects(this.idToObject); this.linkObjects(this.idToObject);
@ -247,6 +255,7 @@ GameLib.D3.Scene.prototype.toApiObject = function() {
apiLights, apiLights,
apiTextures, apiTextures,
apiMaterials, apiMaterials,
GameLib.Utils.IdOrNull(this.activeCamera),
GameLib.Utils.IdOrNull(this.parentEntity) GameLib.Utils.IdOrNull(this.parentEntity)
); );
}; };

View File

@ -28,41 +28,9 @@ GameLib.D3.Viewport = function (
apiViewport.height, apiViewport.height,
apiViewport.x, apiViewport.x,
apiViewport.y, apiViewport.y,
apiViewport.composer,
apiViewport.renderer,
apiViewport.scene,
apiViewport.camera,
apiViewport.parentEntity apiViewport.parentEntity
); );
if (this.composer instanceof GameLib.D3.API.Composer) {
this.composer = new GameLib.D3.Composer(
this.graphics,
this.composer
)
}
if (this.renderer instanceof GameLib.D3.API.Renderer) {
this.renderer = new GameLib.D3.Renderer(
this.graphics,
this.renderer
)
}
if (this.scene instanceof GameLib.D3.API.Scene) {
this.scene = new GameLib.D3.Scene(
this.graphics,
this.scene
)
}
if (this.camera instanceof GameLib.D3.API.Camera) {
this.camera = new GameLib.D3.Camera(
this.graphics,
this.camera
)
}
this.buildIdToObject(); this.buildIdToObject();
this.instance = this.createInstance(); this.instance = this.createInstance();
@ -86,44 +54,6 @@ GameLib.D3.Viewport.prototype.createInstance = function(update) {
instance = this.instance; instance = this.instance;
} }
if (this.renderer) {
this.renderer.width = this.width - this.x;
this.renderer.height = this.height - this.y;
this.renderer.updateInstance();
this.renderer.instance.setViewport(
this.x,
this.y,
this.width,
this.height
);
} else if (this.composer) {
this.composer.renderer.width = this.width - this.x;
this.composer.renderer.height = this.height - this.y;
this.composer.renderer.updateInstance();
this.composer.renderer.instance.setViewport(
this.x,
this.y,
this.width,
this.height
);
this.composer.passes.map(
function(pass) {
if (pass.camera instanceof GameLib.D3.Camera) {
pass.camera.aspect = (this.width - this.x) / (this.height - this.y);
pass.camera.updateInstance();
}
}.bind(this)
)
}
if (this.camera) {
this.camera.aspect = (this.width - this.x) / (this.height - this.y);
this.camera.updateInstance();
}
return instance; return instance;
}; };
@ -147,9 +77,6 @@ GameLib.D3.Viewport.prototype.toApiObject = function() {
this.height, this.height,
this.x, this.x,
this.y, this.y,
GameLib.Utils.IdOrNull(this.composer),
GameLib.Utils.IdOrNull(this.scene),
GameLib.Utils.IdOrNull(this.camera),
GameLib.Utils.IdOrNull(this.parentEntity) GameLib.Utils.IdOrNull(this.parentEntity)
); );
@ -171,20 +98,4 @@ GameLib.D3.Viewport.FromObjectComponent = function(graphics, objectComponent) {
graphics, graphics,
apiViewport apiViewport
); );
};
/**
* Component update override
*/
GameLib.D3.Viewport.prototype.update = function() {
if (this.renderer && this.scene && this.camera) {
this.renderer.instance.render(
this.scene.instance,
this.camera.instance
)
} else if (this.composer) {
this.composer.instance.render();
}
}; };

View File

@ -72,14 +72,54 @@ GameLib.System.prototype.start = function() {
component.domElement.instance.addEventListener('mousedown', component.mouseDown, false); component.domElement.instance.addEventListener('mousedown', component.mouseDown, false);
component.domElement.instance.addEventListener('mousemove', component.mouseMove, false); component.domElement.instance.addEventListener('mousemove', component.mouseMove, false);
component.domElement.instance.addEventListener('keydown', component.keyDown, false); component.domElement.instance.addEventListener('keydown', component.keyDown, false);
component.controls = new THREE.EditorControls( component.controls = new THREE.EditorControls(
component.camera.instance, component.camera.instance,
component.domElement.instance component.domElement.instance
); );
}.bind(this))
/**
* After our mouse 'up' editor controls would have updated our camera
* instance, so we need to update our game-lib camera to reflect these
* changes - we override
*/
component.mouseUp = function(center) {
return function() {
var camera = entity.getFirstComponent(GameLib.D3.Camera);
camera.position.x = camera.instance.position.x;
camera.position.y = camera.instance.position.y;
camera.position.z = camera.instance.position.z;
camera.quaternion.x = camera.instance.quaternion.x;
camera.quaternion.y = camera.instance.quaternion.y;
camera.quaternion.z = camera.instance.quaternion.z;
camera.quaternion.w = camera.instance.quaternion.w;
camera.lookAt.x = center.x;
camera.lookAt.y = center.y;
camera.lookAt.z = center.z;
camera.lookAt.instance.copy(center);
};
}(component.controls.center).bind(component);
/**
* Same applies to our mouse 'scroll' event
*/
component.mouseWheel = function() {
return function() {
var camera = entity.getFirstComponent(GameLib.D3.Camera);
camera.position.x = camera.instance.position.x;
camera.position.y = camera.instance.position.y;
camera.position.z = camera.instance.position.z;
};
}.bind(component);
component.domElement.instance.addEventListener('mousewheel', component.mouseWheel, false);
component.domElement.instance.addEventListener('mouseup', component.mouseUp, false);
}.bind(this))
} }
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
@ -193,10 +233,10 @@ GameLib.System.prototype.update = function(deltaTime) {
var viewport = renderEntity.getFirstComponent(GameLib.D3.Viewport); var viewport = renderEntity.getFirstComponent(GameLib.D3.Viewport);
renderer.instance.setViewport( renderer.instance.setViewport(
viewport.x, viewport.x * renderer.width,
viewport.y, viewport.y * renderer.height,
viewport.width, viewport.width * renderer.width,
viewport.height viewport.height * renderer.height
); );
var scenes = renderEntity.getComponents(GameLib.D3.Scene); var scenes = renderEntity.getComponents(GameLib.D3.Scene);
@ -208,11 +248,19 @@ GameLib.System.prototype.update = function(deltaTime) {
renderer.instance.clear(); renderer.instance.clear();
scenes.map(function(scene){ scenes.map(function(scene){
renderer.instance.render(
scene.instance, if (scene.activeCamera) {
camera.instance renderer.instance.render(
); scene.instance,
//renderer.instance.clearDepth(); scene.activeCamera.instance
);
} else {
renderer.instance.render(
scene.instance,
camera.instance
);
}
}); });
stats.instance.end(); stats.instance.end();
@ -241,6 +289,8 @@ GameLib.System.prototype.stop = function() {
component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false); component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false);
component.domElement.instance.removeEventListener('keydown', component.keyDown, false); component.domElement.instance.removeEventListener('keydown', component.keyDown, false);
component.controls.dispose(); component.controls.dispose();
component.domElement.instance.removeEventListener('mouseup', component.mouseUp, false);
component.domElement.instance.removeEventListener('mousewheel', component.mouseWheel, false);
}); });
console.log('stopped all input systems'); console.log('stopped all input systems');