stats and gui components
parent
bef39bf474
commit
80f5ac9663
|
@ -4,13 +4,15 @@
|
||||||
* @param linkedObjects
|
* @param linkedObjects
|
||||||
* @param loaded (indicates whether the linked Objects for this component has been loaded)
|
* @param loaded (indicates whether the linked Objects for this component has been loaded)
|
||||||
* @param parentEntity
|
* @param parentEntity
|
||||||
|
* @param selected
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
GameLib.API.Component = function(
|
GameLib.API.Component = function(
|
||||||
componentType,
|
componentType,
|
||||||
linkedObjects,
|
linkedObjects,
|
||||||
loaded,
|
loaded,
|
||||||
parentEntity
|
parentEntity,
|
||||||
|
selected
|
||||||
) {
|
) {
|
||||||
this.componentType = componentType;
|
this.componentType = componentType;
|
||||||
|
|
||||||
|
@ -28,6 +30,11 @@ GameLib.API.Component = function(
|
||||||
parentEntity = null;
|
parentEntity = null;
|
||||||
}
|
}
|
||||||
this.parentEntity = parentEntity;
|
this.parentEntity = parentEntity;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(selected)) {
|
||||||
|
selected = false;
|
||||||
|
}
|
||||||
|
this.selected = selected;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,19 +5,22 @@
|
||||||
* @param linkedObjects
|
* @param linkedObjects
|
||||||
* @param loaded
|
* @param loaded
|
||||||
* @param parentEntity
|
* @param parentEntity
|
||||||
|
* @param selected
|
||||||
*/
|
*/
|
||||||
GameLib.Component = function(
|
GameLib.Component = function(
|
||||||
componentType,
|
componentType,
|
||||||
linkedObjects,
|
linkedObjects,
|
||||||
loaded,
|
loaded,
|
||||||
parentEntity
|
parentEntity,
|
||||||
|
selected
|
||||||
) {
|
) {
|
||||||
GameLib.API.Component.call(
|
GameLib.API.Component.call(
|
||||||
this,
|
this,
|
||||||
componentType,
|
componentType,
|
||||||
linkedObjects,
|
linkedObjects,
|
||||||
loaded,
|
loaded,
|
||||||
parentEntity
|
parentEntity,
|
||||||
|
selected
|
||||||
);
|
);
|
||||||
|
|
||||||
this.idToObject = {};
|
this.idToObject = {};
|
||||||
|
@ -60,6 +63,8 @@ GameLib.Component.COMPONENT_TEXTURE = 0x1a;
|
||||||
GameLib.Component.COMPONENT_ENTITY_MANAGER = 0x1b;
|
GameLib.Component.COMPONENT_ENTITY_MANAGER = 0x1b;
|
||||||
GameLib.Component.COMPONENT_DOM_ELEMENT = 0x1c;
|
GameLib.Component.COMPONENT_DOM_ELEMENT = 0x1c;
|
||||||
GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1d;
|
GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1d;
|
||||||
|
GameLib.Component.COMPONENT_STATS = 0x1e;
|
||||||
|
GameLib.Component.COMPONENT_GUI = 0x1f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Components are linked at runtime - for storing, we just store the ID
|
* Components are linked at runtime - for storing, we just store the ID
|
||||||
|
@ -138,7 +143,7 @@ GameLib.Component.prototype.linkObjects = function(idToObject) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
} else if (typeof p == 'string') {
|
} else if (typeof p === 'string') {
|
||||||
|
|
||||||
if (!idToObject[p]) {
|
if (!idToObject[p]) {
|
||||||
console.warn('Could not locate the object to be linked in array - fix this');
|
console.warn('Could not locate the object to be linked in array - fix this');
|
||||||
|
@ -177,7 +182,7 @@ GameLib.Component.prototype.linkObjects = function(idToObject) {
|
||||||
this[property].linkObjects(idToObject);
|
this[property].linkObjects(idToObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (typeof this[property] == 'string') {
|
} else if (typeof this[property] === 'string') {
|
||||||
|
|
||||||
if (!idToObject[this[property]]) {
|
if (!idToObject[this[property]]) {
|
||||||
console.warn('Could not locate the object to be linked - fix this');
|
console.warn('Could not locate the object to be linked - fix this');
|
||||||
|
|
|
@ -46,10 +46,6 @@ GameLib.D3.API.Input.Editor = function (
|
||||||
}
|
}
|
||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(selectDelayMs)) {
|
|
||||||
selectDelayMs = 300;
|
|
||||||
}
|
|
||||||
this.selectDelayMs = selectDelayMs;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype);
|
GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype);
|
||||||
|
|
|
@ -521,7 +521,7 @@ GameLib.D3.Editor.prototype.addScene = function(scene) {
|
||||||
/**
|
/**
|
||||||
* Now we need to notify all systems of this new components
|
* Now we need to notify all systems of this new components
|
||||||
*/
|
*/
|
||||||
var systems = entity.getComponent(GameLib.System);
|
var systems = entity.getComponents(GameLib.System);
|
||||||
|
|
||||||
systems.map(
|
systems.map(
|
||||||
function(system){
|
function(system){
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* @param helperType
|
* @param helperType
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
GameLib.D3.Helper = function Helper(
|
GameLib.D3.Helper = function(
|
||||||
graphics,
|
graphics,
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
|
@ -39,21 +39,21 @@ GameLib.D3.Helper = function Helper(
|
||||||
|
|
||||||
if (
|
if (
|
||||||
object instanceof GameLib.D3.Mesh &&
|
object instanceof GameLib.D3.Mesh &&
|
||||||
object.meshType != GameLib.D3.Mesh.TYPE_CURVE
|
object.meshType !== GameLib.D3.Mesh.TYPE_CURVE
|
||||||
) {
|
) {
|
||||||
helperType = GameLib.D3.Helper.HELPER_TYPE_EDGES;
|
helperType = GameLib.D3.Helper.HELPER_TYPE_EDGES;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object instanceof GameLib.D3.Light) {
|
if (object instanceof GameLib.D3.Light) {
|
||||||
if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
|
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
|
||||||
helperType = GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT;
|
helperType = GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
|
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT) {
|
||||||
helperType = GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT;
|
helperType = GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT) {
|
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT) {
|
||||||
helperType = GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT;
|
helperType = GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,27 +91,27 @@ GameLib.D3.Helper.prototype.createInstance = function(update) {
|
||||||
instance = this.instance;
|
instance = this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_EDGES) {
|
||||||
instance = new THREE.EdgesHelper(this.object.instance, 0x007700);
|
instance = new THREE.EdgesHelper(this.object.instance, 0x00FF00);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
|
||||||
instance = new THREE.DirectionalLightHelper(this.object.instance);
|
instance = new THREE.DirectionalLightHelper(this.object.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
|
||||||
instance = new THREE.PointLightHelper(this.object.instance, 1);
|
instance = new THREE.PointLightHelper(this.object.instance, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
|
||||||
instance = new THREE.SpotLightHelper(this.object.instance);
|
instance = new THREE.SpotLightHelper(this.object.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
|
||||||
instance = new THREE.WireframeGeometry(this.object.instance, 0x007700);
|
instance = new THREE.WireframeGeometry(this.object.instance, 0x00FF00);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SKELETON) {
|
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SKELETON) {
|
||||||
instance = new THREE.SkeletonHelper(this.object.instance);
|
instance = new THREE.SkeletonHelper(this.object.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,6 @@ GameLib.D3.Input.Editor = function (
|
||||||
this.mouseMove = null;
|
this.mouseMove = null;
|
||||||
this.mouseDown = null;
|
this.mouseDown = null;
|
||||||
this.keyDown = null;
|
this.keyDown = null;
|
||||||
this.contextMenu = null;
|
|
||||||
|
|
||||||
this.raycaster = new GameLib.D3.Raycaster(
|
this.raycaster = new GameLib.D3.Raycaster(
|
||||||
this.graphics
|
this.graphics
|
||||||
|
@ -128,7 +127,7 @@ GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity) {
|
||||||
|
|
||||||
// BELOW is just a test to stop systems from keypress - it works nicely :)
|
// BELOW is just a test to stop systems from keypress - it works nicely :)
|
||||||
// but of course, when the system is stopped it can no longer be started by keypress
|
// but of course, when the system is stopped it can no longer be started by keypress
|
||||||
// var inputSystem = entity.getComponent(GameLib.System).reduce(
|
// var inputSystem = entity.getComponents(GameLib.System).reduce(
|
||||||
// function(result, system){
|
// function(result, system){
|
||||||
// if (system.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
|
// if (system.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
|
||||||
// result = system;
|
// result = system;
|
||||||
|
@ -159,7 +158,7 @@ GameLib.D3.Input.Editor.prototype.onMouseMove = function(entity) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MouseDown events
|
* MouseDown events
|
||||||
* @param entity
|
* @param entity GameLib.Entity
|
||||||
* @returns {Function}
|
* @returns {Function}
|
||||||
*/
|
*/
|
||||||
GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) {
|
GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) {
|
||||||
|
@ -178,7 +177,7 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) {
|
||||||
this.camera.instance
|
this.camera.instance
|
||||||
);
|
);
|
||||||
|
|
||||||
var meshes = entity.getComponent(GameLib.D3.Mesh);
|
var meshes = entity.getComponents(GameLib.D3.Mesh);
|
||||||
|
|
||||||
var intersects = this.raycaster.getIntersectedObjects(meshes);
|
var intersects = this.raycaster.getIntersectedObjects(meshes);
|
||||||
|
|
||||||
|
@ -194,31 +193,82 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) {
|
||||||
*/
|
*/
|
||||||
event.stopImmediatePropagation();
|
event.stopImmediatePropagation();
|
||||||
|
|
||||||
intersects[0].instance.material.wireframe = !intersects[0].instance.material.wireframe;
|
/**
|
||||||
|
* Notify our component as being 'selected'
|
||||||
console.log('object(s) instersected');
|
* @type {boolean}
|
||||||
}
|
*/
|
||||||
}
|
intersects[0].selected = !intersects[0].selected;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevent Context Menu creation
|
* Add a helper to the scene
|
||||||
* @param event
|
* @type GameLib.D3.Helper
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
GameLib.D3.Input.Editor.prototype.onContextMenu = function(event){
|
var helper = null;
|
||||||
|
|
||||||
if (event.stopPropagation) {
|
var scene = intersects[0].parentScene;
|
||||||
event.stopPropagation();
|
|
||||||
|
if (!scene) {
|
||||||
|
console.warn('the scene object for this mesh could not be located : ' + intersects[0].name);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.preventDefault) {
|
if (intersects[0].selected) {
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
event.cancelBubble = true;
|
helper = new GameLib.D3.Helper(
|
||||||
return false;
|
this.graphics,
|
||||||
|
null,
|
||||||
|
intersects[0].name + ' Helper',
|
||||||
|
intersects[0],
|
||||||
|
GameLib.D3.Helper.HELPER_TYPE_EDGES
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Backup the polygonOffset value, then set it to 'true' - helps for clear nice outlines
|
||||||
|
*/
|
||||||
|
intersects[0].polygonOffset = intersects[0].instance.material.polygonOffset;
|
||||||
|
|
||||||
|
intersects[0].instance.material.polygonOffset = true;
|
||||||
|
|
||||||
|
entity.addComponent(helper);
|
||||||
|
|
||||||
|
scene.instance.add(helper.instance);
|
||||||
|
|
||||||
|
var gui = entity.getFirstComponent(GameLib.GUI);
|
||||||
|
|
||||||
|
gui.instance.add(intersects[0], 'name');
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
var components = entity.getComponents(GameLib.D3.Helper);
|
||||||
|
|
||||||
|
helper = components.reduce(
|
||||||
|
function(result, component) {
|
||||||
|
if (component.object === intersects[0]) {
|
||||||
|
result = component;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
if (helper) {
|
||||||
|
|
||||||
|
scene.instance.remove(helper.instance);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore the polygonOffset value
|
||||||
|
*/
|
||||||
|
intersects[0].instance.material.polygonOffset = intersects[0].polygonOffset;
|
||||||
|
|
||||||
|
entity.removeComponent(helper);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.warn('failed to locate helper object which should exist for ' + intersects[0].name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -274,88 +274,3 @@ GameLib.D3.Scene.FromObjectScene = function(
|
||||||
computeNormals
|
computeNormals
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Transforms raw scene data into a GameLib.D3.Scene
|
|
||||||
* @param graphics
|
|
||||||
* @param objectScene Object (as it comes from the API)
|
|
||||||
* @param computeNormals
|
|
||||||
* @param onLoaded
|
|
||||||
* @param imageFactory GameLib.D3.ImageFactory
|
|
||||||
* @constructor
|
|
||||||
*/
|
|
||||||
GameLib.D3.Scene.LoadScene = function(
|
|
||||||
graphics,
|
|
||||||
objectScene,
|
|
||||||
computeNormals,
|
|
||||||
onLoaded,
|
|
||||||
imageFactory
|
|
||||||
) {
|
|
||||||
var scene = GameLib.D3.Scene.FromObjectScene(
|
|
||||||
graphics,
|
|
||||||
objectScene,
|
|
||||||
computeNormals,
|
|
||||||
imageFactory
|
|
||||||
);
|
|
||||||
|
|
||||||
onLoaded(scene);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a scene directly from the API
|
|
||||||
* @param graphics GameLib.D3.Graphics
|
|
||||||
* @param partialSceneObject Object {path: '', name: ''}
|
|
||||||
* @param apiUrl
|
|
||||||
* @param onLoaded
|
|
||||||
*/
|
|
||||||
GameLib.D3.Scene.LoadSceneFromApi = function(
|
|
||||||
graphics,
|
|
||||||
partialSceneObject,
|
|
||||||
apiUrl,
|
|
||||||
onLoaded
|
|
||||||
) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* First check if this is a client or server side request
|
|
||||||
*/
|
|
||||||
if (typeof XMLHttpRequest == 'undefined') {
|
|
||||||
console.warn('implement server side loading from API here');
|
|
||||||
return onLoaded(null, new Error('not implemented'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
xhr.open(
|
|
||||||
'GET',
|
|
||||||
apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name
|
|
||||||
);
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function(xhr) {
|
|
||||||
|
|
||||||
return function() {
|
|
||||||
|
|
||||||
if (xhr.readyState == 4) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
var response = JSON.parse(xhr.responseText);
|
|
||||||
} catch (e) {
|
|
||||||
return onLoaded(null, new Error('Could not load scene : ' + e.message));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.scene || response.scene.length == 0) {
|
|
||||||
return onLoaded(null, new Error('Could not load scene'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var objectScene = response.scene[0];
|
|
||||||
|
|
||||||
GameLib.D3.Scene.LoadScene(
|
|
||||||
graphics,
|
|
||||||
objectScene,
|
|
||||||
true,
|
|
||||||
onLoaded
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(xhr);
|
|
||||||
|
|
||||||
xhr.send();
|
|
||||||
};
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/**
|
||||||
|
* Stats component for displaying some render statistics (framerate, memory consumption, etc)
|
||||||
|
* @param stats
|
||||||
|
* @param id
|
||||||
|
* @param name
|
||||||
|
* @param domElement
|
||||||
|
* @param parentEntity
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.D3.Stats = function(
|
||||||
|
stats,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
domElement,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(stats)) {
|
||||||
|
throw new Error('Need Stats object')
|
||||||
|
}
|
||||||
|
this.stats = stats;
|
||||||
|
|
||||||
|
GameLib.Component.call(
|
||||||
|
this,
|
||||||
|
GameLib.Component.COMPONENT_STATS,
|
||||||
|
{
|
||||||
|
'domElement': GameLib.DomElement
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
parentEntity
|
||||||
|
);
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(id)) {
|
||||||
|
id = GameLib.Utils.RandomId();
|
||||||
|
}
|
||||||
|
this.id = id;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(name)) {
|
||||||
|
name = 'Stats (' + id + ')';
|
||||||
|
}
|
||||||
|
this.name = name;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(domElement)) {
|
||||||
|
console.warn('Need a DOM element for stats');
|
||||||
|
throw new Error('Need a DOM element for stats');
|
||||||
|
}
|
||||||
|
this.domElement = domElement;
|
||||||
|
|
||||||
|
this.instance = this.createInstance();
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Stats.prototype = Object.create(GameLib.Component.prototype);
|
||||||
|
GameLib.D3.Stats.prototype.constructor = GameLib.D3.Stats;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a helper instance
|
||||||
|
* @param update
|
||||||
|
*/
|
||||||
|
GameLib.D3.Stats.prototype.createInstance = function(update) {
|
||||||
|
|
||||||
|
var instance = null;
|
||||||
|
|
||||||
|
if (update) {
|
||||||
|
this.instance = new this.stats();
|
||||||
|
instance = this.instance;
|
||||||
|
} else {
|
||||||
|
instance = new this.stats();
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the instance with the current state
|
||||||
|
*/
|
||||||
|
GameLib.D3.Stats.prototype.updateInstance = function() {
|
||||||
|
this.instance = this.createInstance(true);
|
||||||
|
};
|
|
@ -193,7 +193,7 @@ GameLib.EntityManager.prototype.queryComponents = function(constructor) {
|
||||||
|
|
||||||
var components = entities.reduce(
|
var components = entities.reduce(
|
||||||
function(result, entity){
|
function(result, entity){
|
||||||
var ecs = entity.getComponent(constructor);
|
var ecs = entity.getComponents(constructor);
|
||||||
ecs.map(function(ec){
|
ecs.map(function(ec){
|
||||||
result.push(ec);
|
result.push(ec);
|
||||||
});
|
});
|
||||||
|
|
|
@ -105,13 +105,15 @@ GameLib.Entity.prototype.createInstance = function() {
|
||||||
GameLib.Entity.prototype.addComponent = function(component) {
|
GameLib.Entity.prototype.addComponent = function(component) {
|
||||||
this.components.push(component);
|
this.components.push(component);
|
||||||
component.parentEntity = this;
|
component.parentEntity = this;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all components of type 'constructor'
|
* Returns all components of type 'constructor'
|
||||||
* @param constructor
|
* @param constructor
|
||||||
*/
|
*/
|
||||||
GameLib.Entity.prototype.getComponent = function(constructor) {
|
GameLib.Entity.prototype.getComponents = function(constructor) {
|
||||||
|
|
||||||
var components = this.components.reduce(
|
var components = this.components.reduce(
|
||||||
function(result, component) {
|
function(result, component) {
|
||||||
|
@ -132,7 +134,7 @@ GameLib.Entity.prototype.getComponent = function(constructor) {
|
||||||
*/
|
*/
|
||||||
GameLib.Entity.prototype.getFirstComponent = function(constructor) {
|
GameLib.Entity.prototype.getFirstComponent = function(constructor) {
|
||||||
|
|
||||||
var components = this.getComponent(constructor);
|
var components = this.getComponents(constructor);
|
||||||
|
|
||||||
if (components.length > 0) {
|
if (components.length > 0) {
|
||||||
return components[0];
|
return components[0];
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/**
|
||||||
|
* Stats component for displaying some render statistics (framerate, memory consumption, etc)
|
||||||
|
* @param gui
|
||||||
|
* @param id
|
||||||
|
* @param name
|
||||||
|
* @param domElement
|
||||||
|
* @param parentEntity
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.GUI = function(
|
||||||
|
gui,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
domElement,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(gui)) {
|
||||||
|
throw new Error('Need Stats object')
|
||||||
|
}
|
||||||
|
this.gui = gui;
|
||||||
|
|
||||||
|
GameLib.Component.call(
|
||||||
|
this,
|
||||||
|
GameLib.Component.COMPONENT_GUI,
|
||||||
|
{
|
||||||
|
'domElement': GameLib.DomElement
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
parentEntity
|
||||||
|
);
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(id)) {
|
||||||
|
id = GameLib.Utils.RandomId();
|
||||||
|
}
|
||||||
|
this.id = id;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(name)) {
|
||||||
|
name = 'GUI (' + id + ')';
|
||||||
|
}
|
||||||
|
this.name = name;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(domElement)) {
|
||||||
|
console.warn('Need a DOM element for stats');
|
||||||
|
throw new Error('Need a DOM element for stats');
|
||||||
|
}
|
||||||
|
this.domElement = domElement;
|
||||||
|
|
||||||
|
this.instance = this.createInstance();
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.GUI.prototype = Object.create(GameLib.Component.prototype);
|
||||||
|
GameLib.GUI.prototype.constructor = GameLib.GUI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a helper instance
|
||||||
|
* @param update
|
||||||
|
*/
|
||||||
|
GameLib.GUI.prototype.createInstance = function(update) {
|
||||||
|
|
||||||
|
var instance = null;
|
||||||
|
|
||||||
|
if (update) {
|
||||||
|
this.instance = new this.gui( { autoPlace: false } );
|
||||||
|
instance = this.instance;
|
||||||
|
} else {
|
||||||
|
instance = new this.gui( { autoPlace: false } );
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the instance with the current state
|
||||||
|
*/
|
||||||
|
GameLib.GUI.prototype.updateInstance = function() {
|
||||||
|
this.instance = this.createInstance(true);
|
||||||
|
};
|
|
@ -48,10 +48,12 @@ GameLib.System = function(
|
||||||
GameLib.System.prototype = Object.create(GameLib.API.System.prototype);
|
GameLib.System.prototype = Object.create(GameLib.API.System.prototype);
|
||||||
GameLib.System.prototype.constructor = GameLib.System;
|
GameLib.System.prototype.constructor = GameLib.System;
|
||||||
|
|
||||||
|
GameLib.System.SYSTEM_TYPE_NONE = 0x0;
|
||||||
GameLib.System.SYSTEM_TYPE_RENDER = 0x1;
|
GameLib.System.SYSTEM_TYPE_RENDER = 0x1;
|
||||||
GameLib.System.SYSTEM_TYPE_ANIMATION = 0x2;
|
GameLib.System.SYSTEM_TYPE_ANIMATION = 0x2;
|
||||||
GameLib.System.SYSTEM_TYPE_INPUT = 0x4;
|
GameLib.System.SYSTEM_TYPE_INPUT = 0x4;
|
||||||
GameLib.System.SYSTEM_TYPE_ALL = 0x7;
|
GameLib.System.SYSTEM_TYPE_STORAGE = 0x8;
|
||||||
|
GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @callback
|
* @callback
|
||||||
|
@ -73,40 +75,21 @@ GameLib.System.prototype.start = function() {
|
||||||
component.mouseDown = component.onMouseDown(entity).bind(component);
|
component.mouseDown = component.onMouseDown(entity).bind(component);
|
||||||
component.mouseMove = component.onMouseMove(entity).bind(component);
|
component.mouseMove = component.onMouseMove(entity).bind(component);
|
||||||
component.keyDown = component.onKeyDown(entity).bind(component);
|
component.keyDown = component.onKeyDown(entity).bind(component);
|
||||||
// component.contextMenu = component.onContextMenu(entity).bind(component);
|
|
||||||
|
|
||||||
|
|
||||||
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.camera.instance, component.domElement.instance);
|
|
||||||
|
|
||||||
// component.domElement.instance.addEventListener('contextmenu', component.contextMenu, false);
|
component.controls = new THREE.EditorControls(
|
||||||
|
component.camera.instance,
|
||||||
|
component.domElement.instance
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
|
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(this.domElement)) {
|
|
||||||
console.warn('Cannot start a rendering system without a valid DOM element');
|
|
||||||
throw new Error('Cannot start a rendering system without a valid DOM element');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(this.domStats)) {
|
|
||||||
console.warn('No stats DOM - will run the render process without statistics information');
|
|
||||||
} else {
|
|
||||||
//this.domElement.instance.appendChild(this.domStats.instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
// this.renderers = this.entityManager.queryComponents(GameLib.D3.Renderer);
|
|
||||||
|
|
||||||
// this.renderers.forEach(
|
|
||||||
// function (renderer) {
|
|
||||||
// renderer.domElement.instance.appendChild(renderer.instance.domElement);
|
|
||||||
// }.bind(this)
|
|
||||||
// );
|
|
||||||
|
|
||||||
this.renderEntities = this.entityManager.query(
|
this.renderEntities = this.entityManager.query(
|
||||||
[
|
[
|
||||||
GameLib.D3.Viewport,
|
GameLib.D3.Viewport,
|
||||||
|
@ -116,6 +99,23 @@ GameLib.System.prototype.start = function() {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.renderEntities.map(
|
||||||
|
function(entity) {
|
||||||
|
var stats = entity.getFirstComponent(GameLib.D3.Stats);
|
||||||
|
stats.instance.dom.style.position = 'absolute';
|
||||||
|
// stats.instance.dom.style.float = 'left';
|
||||||
|
stats.instance.dom.style.top = '34px';
|
||||||
|
stats.instance.dom.style.left = 'unset';
|
||||||
|
stats.domElement.instance.parentElement.appendChild(stats.instance.dom);
|
||||||
|
|
||||||
|
var gui = entity.getFirstComponent(GameLib.GUI);
|
||||||
|
gui.instance.domElement.style.position = 'absolute';
|
||||||
|
gui.instance.domElement.style.top = '34px';
|
||||||
|
gui.instance.domElement.style.right = '0px';
|
||||||
|
gui.domElement.instance.parentElement.appendChild(gui.instance.domElement);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) {
|
if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) {
|
||||||
|
@ -190,6 +190,9 @@ GameLib.System.prototype.update = function(deltaTime) {
|
||||||
|
|
||||||
function(renderEntity) {
|
function(renderEntity) {
|
||||||
|
|
||||||
|
var stats = renderEntity.getFirstComponent(GameLib.D3.Stats);
|
||||||
|
stats.instance.begin();
|
||||||
|
|
||||||
var renderer = renderEntity.getFirstComponent(GameLib.D3.Renderer);
|
var renderer = renderEntity.getFirstComponent(GameLib.D3.Renderer);
|
||||||
var camera = renderEntity.getFirstComponent(GameLib.D3.Camera);
|
var camera = renderEntity.getFirstComponent(GameLib.D3.Camera);
|
||||||
var viewport = renderEntity.getFirstComponent(GameLib.D3.Viewport);
|
var viewport = renderEntity.getFirstComponent(GameLib.D3.Viewport);
|
||||||
|
@ -201,7 +204,7 @@ GameLib.System.prototype.update = function(deltaTime) {
|
||||||
viewport.height
|
viewport.height
|
||||||
);
|
);
|
||||||
|
|
||||||
var scenes = renderEntity.getComponent(GameLib.D3.Scene);
|
var scenes = renderEntity.getComponents(GameLib.D3.Scene);
|
||||||
|
|
||||||
if (scenes.length > 1) {
|
if (scenes.length > 1) {
|
||||||
renderer.instance.autoClear = false;
|
renderer.instance.autoClear = false;
|
||||||
|
@ -216,6 +219,8 @@ GameLib.System.prototype.update = function(deltaTime) {
|
||||||
);
|
);
|
||||||
//renderer.instance.clearDepth();
|
//renderer.instance.clearDepth();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
stats.instance.end();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -292,7 +297,7 @@ GameLib.System.prototype.toApiSystem = function() {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.System.prototype.notify = function(property, constructor) {
|
GameLib.System.prototype.notify = function(property, constructor, action) {
|
||||||
|
|
||||||
this[property] = this.entityManager.queryComponents(constructor);
|
this[property] = this.entityManager.queryComponents(constructor);
|
||||||
|
|
||||||
|
@ -304,3 +309,101 @@ GameLib.System.prototype.notify = function(property, constructor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.System.prototype.load = function() {
|
||||||
|
|
||||||
|
if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First check if this is a client or server side request
|
||||||
|
*/
|
||||||
|
if (typeof XMLHttpRequest === 'undefined') {
|
||||||
|
console.warn('implement server side loading from API here');
|
||||||
|
return onLoaded(
|
||||||
|
null,
|
||||||
|
new Error('not implemented')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open(
|
||||||
|
'GET',
|
||||||
|
apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name
|
||||||
|
);
|
||||||
|
|
||||||
|
xhr.onreadystatechange = function(xhr) {
|
||||||
|
|
||||||
|
return function() {
|
||||||
|
|
||||||
|
if (xhr.readyState === 4) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
var response = JSON.parse(xhr.responseText);
|
||||||
|
} catch (e) {
|
||||||
|
return onLoaded(null, new Error('Could not load scene : ' + e.message));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.scene || response.scene.length === 0) {
|
||||||
|
return onLoaded(null, new Error('Could not load scene'));
|
||||||
|
}
|
||||||
|
|
||||||
|
var objectScene = response.scene[0];
|
||||||
|
|
||||||
|
GameLib.D3.Scene.LoadScene(
|
||||||
|
graphics,
|
||||||
|
objectScene,
|
||||||
|
true,
|
||||||
|
onLoaded
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(xhr);
|
||||||
|
|
||||||
|
xhr.send();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms raw scene data into a GameLib.D3.Scene
|
||||||
|
* @param graphics
|
||||||
|
* @param objectScene Object (as it comes from the API)
|
||||||
|
* @param computeNormals
|
||||||
|
* @param onLoaded
|
||||||
|
* @param imageFactory GameLib.D3.ImageFactory
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.D3.Scene.LoadScene = function(
|
||||||
|
graphics,
|
||||||
|
objectScene,
|
||||||
|
computeNormals,
|
||||||
|
onLoaded,
|
||||||
|
imageFactory
|
||||||
|
) {
|
||||||
|
var scene = GameLib.D3.Scene.FromObjectScene(
|
||||||
|
graphics,
|
||||||
|
objectScene,
|
||||||
|
computeNormals,
|
||||||
|
imageFactory
|
||||||
|
);
|
||||||
|
|
||||||
|
onLoaded(scene);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a scene directly from the API
|
||||||
|
* @param graphics GameLib.D3.Graphics
|
||||||
|
* @param partialSceneObject Object {path: '', name: ''}
|
||||||
|
* @param apiUrl
|
||||||
|
* @param onLoaded
|
||||||
|
*/
|
||||||
|
GameLib.D3.Scene.LoadSceneFromApi = function(
|
||||||
|
graphics,
|
||||||
|
partialSceneObject,
|
||||||
|
apiUrl,
|
||||||
|
onLoaded
|
||||||
|
) {
|
||||||
|
|
||||||
|
|
||||||
|
};
|
Loading…
Reference in New Issue