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;
};
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
* @param objectComponent (should be an ID string - components get loaded and linked)

View File

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

View File

@ -11,6 +11,7 @@
* @param lights [GameLib.D3.API.Light]
* @param textures [GameLib.D3.API.Texture]
* @param materials [GameLib.D3.API.Material]
* @param activeCamera [GameLib.D3.Camera]
* @param parentEntity
* @constructor
*/
@ -26,6 +27,7 @@ GameLib.D3.API.Scene = function(
lights,
textures,
materials,
activeCamera,
parentEntity
) {
GameLib.Component.call(
@ -36,7 +38,8 @@ GameLib.D3.API.Scene = function(
'meshes' : [GameLib.D3.Mesh],
'lights' : [GameLib.D3.Light],
'textures' : [GameLib.D3.Texture],
'materials' : [GameLib.D3.Material]
'materials' : [GameLib.D3.Material],
'activeCamera' : GameLib.D3.Camera
},
false,
parentEntity
@ -98,6 +101,11 @@ GameLib.D3.API.Scene = function(
}
this.materials = materials;
if (GameLib.Utils.UndefinedOrNull(activeCamera)) {
activeCamera = null;
}
this.activeCamera = activeCamera;
};
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 apiScale = new GameLib.API.Vector3(1,1,1);
var apiActiveCamera = null;
/**
* 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);
}
if (objectScene.activeCamera) {
apiActiveCamera = objectScene.activeCamera;
}
return new GameLib.D3.API.Scene(
objectScene.id,
objectScene.name,
@ -186,6 +200,7 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
apiLights,
apiTextures,
apiMaterials,
apiActiveCamera,
objectScene.parentEntity
);

View File

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

View File

@ -112,8 +112,8 @@ GameLib.D3.Camera.prototype.createInstance = function(update) {
instance = new THREE.OrthographicCamera(
this.minX,
this.maxX,
this.minY,
this.maxY,
this.minY,
this.minZ,
this.maxZ
);
@ -125,12 +125,12 @@ GameLib.D3.Camera.prototype.createInstance = function(update) {
if (update) {
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance.minX = this.minX;
instance.maxX = this.maxX;
instance.minY = this.minY;
instance.maxY = this.maxY;
instance.minZ = this.minZ;
instance.maxZ = this.maxZ;
instance.left = this.minX;
instance.right = this.maxX;
instance.bottom = this.minY;
instance.top = this.maxY;
instance.near = this.minZ;
instance.far = this.maxZ;
}
if (
@ -178,6 +178,16 @@ GameLib.D3.Camera.prototype.updateInstance = function() {
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
* @returns {GameLib.D3.API.Camera}

View File

@ -26,7 +26,6 @@ GameLib.D3.Input.Editor = function (
apiInputEditor.name,
apiInputEditor.domElement,
apiInputEditor.camera,
apiInputEditor.selectDelayMs,
apiInputEditor.parentEntity
);
@ -56,6 +55,8 @@ GameLib.D3.Input.Editor = function (
this.mouseMove = null;
this.mouseDown = null;
this.keyDown = null;
this.mouseUp = null;
this.mouseWheel = null;
this.raycaster = new GameLib.D3.Raycaster(
this.graphics
@ -98,7 +99,6 @@ GameLib.D3.Input.Editor.prototype.toApiObject = function() {
this.name,
this.domElementId,
GameLib.Utils.IdOrNull(this.camera),
this.selectDelayMs,
GameLib.Utils.IdOrNull(this.parentEntity)
);
@ -148,12 +148,28 @@ GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity) {
* @returns {Function}
*/
GameLib.D3.Input.Editor.prototype.onMouseMove = function(entity) {
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.textures,
apiScene.materials,
apiScene.activeCamera,
apiScene.parentEntity
);
@ -150,6 +151,13 @@ GameLib.D3.Scene = function (
}.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.linkObjects(this.idToObject);
@ -247,6 +255,7 @@ GameLib.D3.Scene.prototype.toApiObject = function() {
apiLights,
apiTextures,
apiMaterials,
GameLib.Utils.IdOrNull(this.activeCamera),
GameLib.Utils.IdOrNull(this.parentEntity)
);
};

View File

@ -28,41 +28,9 @@ GameLib.D3.Viewport = function (
apiViewport.height,
apiViewport.x,
apiViewport.y,
apiViewport.composer,
apiViewport.renderer,
apiViewport.scene,
apiViewport.camera,
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.instance = this.createInstance();
@ -86,44 +54,6 @@ GameLib.D3.Viewport.prototype.createInstance = function(update) {
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;
};
@ -147,9 +77,6 @@ GameLib.D3.Viewport.prototype.toApiObject = function() {
this.height,
this.x,
this.y,
GameLib.Utils.IdOrNull(this.composer),
GameLib.Utils.IdOrNull(this.scene),
GameLib.Utils.IdOrNull(this.camera),
GameLib.Utils.IdOrNull(this.parentEntity)
);
@ -171,20 +98,4 @@ GameLib.D3.Viewport.FromObjectComponent = function(graphics, objectComponent) {
graphics,
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('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.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) {
@ -193,10 +233,10 @@ GameLib.System.prototype.update = function(deltaTime) {
var viewport = renderEntity.getFirstComponent(GameLib.D3.Viewport);
renderer.instance.setViewport(
viewport.x,
viewport.y,
viewport.width,
viewport.height
viewport.x * renderer.width,
viewport.y * renderer.height,
viewport.width * renderer.width,
viewport.height * renderer.height
);
var scenes = renderEntity.getComponents(GameLib.D3.Scene);
@ -208,11 +248,19 @@ GameLib.System.prototype.update = function(deltaTime) {
renderer.instance.clear();
scenes.map(function(scene){
renderer.instance.render(
scene.instance,
camera.instance
);
//renderer.instance.clearDepth();
if (scene.activeCamera) {
renderer.instance.render(
scene.instance,
scene.activeCamera.instance
);
} else {
renderer.instance.render(
scene.instance,
camera.instance
);
}
});
stats.instance.end();
@ -241,6 +289,8 @@ GameLib.System.prototype.stop = function() {
component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false);
component.domElement.instance.removeEventListener('keydown', component.keyDown, false);
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');