don't store clones, deep clone objects, continue loading when components fail to load, fix entity and renderer toApiObject
parent
b6bb9cb9a7
commit
b59c118ee7
|
@ -478,6 +478,16 @@ GameLib.Component.prototype.buildIdToObject = function() {
|
|||
}
|
||||
}
|
||||
|
||||
if (this instanceof GameLib.D3.Scene) {
|
||||
this.clones.map(
|
||||
function(clone) {
|
||||
if (this.idToObject.hasOwnProperty(clone.id)) {
|
||||
delete this.idToObject[clone.id];
|
||||
}
|
||||
}.bind(this)
|
||||
)
|
||||
}
|
||||
|
||||
this.idToObject[this.id] = this;
|
||||
|
||||
this.building = false;
|
||||
|
@ -487,6 +497,8 @@ GameLib.Component.prototype.generateNewIds = function() {
|
|||
|
||||
this.buildIdToObject();
|
||||
|
||||
var codeComponents = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.CustomCode);
|
||||
|
||||
for (var property in this.idToObject) {
|
||||
if (this.idToObject.hasOwnProperty(property)) {
|
||||
|
||||
|
@ -495,9 +507,14 @@ GameLib.Component.prototype.generateNewIds = function() {
|
|||
|
||||
this.idToObject[property].id = newId;
|
||||
this.idToObject[property].name = this.idToObject[property].name.replace(oldId,newId);
|
||||
codeComponents.map(function(codeComponent){
|
||||
codeComponent.code = codeComponent.code.replace(oldId,newId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
GameLib.Component.prototype.remove = function() {
|
||||
|
@ -601,42 +618,42 @@ GameLib.Component.prototype.cloneInstance = function() {
|
|||
return clone;
|
||||
};
|
||||
|
||||
GameLib.Component.prototype.getStorageDependencies = function() {
|
||||
|
||||
var dependencies = {};
|
||||
|
||||
for (var property in this.linkedObjects) {
|
||||
if (this.linkedObjects.hasOwnProperty(property)){
|
||||
if (this.hasOwnProperty(property)) {
|
||||
|
||||
if (
|
||||
this[property] instanceof Array &&
|
||||
this.linkedObjects[property] instanceof Array
|
||||
) {
|
||||
|
||||
if (this.linkedObjects[property].length !== 1) {
|
||||
console.log('Invalid formed argument type');
|
||||
}
|
||||
|
||||
dependencies[property] = this[property].map(
|
||||
function(__constructor) {
|
||||
return function(arrayProperty){
|
||||
if (arrayProperty instanceof __constructor) {
|
||||
return GameLib.Utils.IdOrNull(arrayProperty);
|
||||
}
|
||||
}.bind(this)
|
||||
}(this.linkedObjects[property][0])
|
||||
);
|
||||
|
||||
} else if (this[property] instanceof this.linkedObjects[property]) {
|
||||
dependencies[property] = GameLib.Utils.IdOrNull(this[property]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dependencies;
|
||||
};
|
||||
// GameLib.Component.prototype.getStorageDependencies = function() {
|
||||
//
|
||||
// var dependencies = {};
|
||||
//
|
||||
// for (var property in this.linkedObjects) {
|
||||
// if (this.linkedObjects.hasOwnProperty(property)){
|
||||
// if (this.hasOwnProperty(property)) {
|
||||
//
|
||||
// if (
|
||||
// this[property] instanceof Array &&
|
||||
// this.linkedObjects[property] instanceof Array
|
||||
// ) {
|
||||
//
|
||||
// if (this.linkedObjects[property].length !== 1) {
|
||||
// console.log('Invalid formed argument type');
|
||||
// }
|
||||
//
|
||||
// dependencies[property] = this[property].map(
|
||||
// function(__constructor) {
|
||||
// return function(arrayProperty){
|
||||
// if (arrayProperty instanceof __constructor) {
|
||||
// return GameLib.Utils.IdOrNull(arrayProperty);
|
||||
// }
|
||||
// }.bind(this)
|
||||
// }(this.linkedObjects[property][0])
|
||||
// );
|
||||
//
|
||||
// } else if (this[property] instanceof this.linkedObjects[property]) {
|
||||
// dependencies[property] = GameLib.Utils.IdOrNull(this[property]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return dependencies;
|
||||
// };
|
||||
|
||||
GameLib.Component.prototype.saveToRemoteAPI = function() {
|
||||
this.save(true);
|
||||
|
@ -656,13 +673,13 @@ GameLib.Component.prototype.save = function(remote) {
|
|||
|
||||
apiObject.componentType = this.idToObject[property].componentType;
|
||||
|
||||
var storageDependencies = this.idToObject[property].getStorageDependencies();
|
||||
|
||||
for (var storageProperty in storageDependencies) {
|
||||
if (storageDependencies.hasOwnProperty(storageProperty)) {
|
||||
apiObject[storageProperty] = storageDependencies[storageProperty];
|
||||
}
|
||||
}
|
||||
// var storageDependencies = this.idToObject[property].getStorageDependencies();
|
||||
//
|
||||
// for (var storageProperty in storageDependencies) {
|
||||
// if (storageDependencies.hasOwnProperty(storageProperty)) {
|
||||
// apiObject[storageProperty] = storageDependencies[storageProperty];
|
||||
// }
|
||||
// }
|
||||
|
||||
this.publish(
|
||||
GameLib.Event.SAVE_COMPONENT,
|
||||
|
|
|
@ -274,7 +274,7 @@ GameLib.D3.Renderer.prototype.toApiObject = function() {
|
|||
this.width,
|
||||
this.height,
|
||||
this.preserveDrawingBuffer,
|
||||
this.domElement.toApiObject(),
|
||||
GameLib.Utils.IdOrNull(this.domElement),
|
||||
this.clearColor.toApiObject(),
|
||||
GameLib.Utils.IdOrNull(this.camera),
|
||||
this.scenes.map(function(scene){
|
||||
|
|
|
@ -290,16 +290,35 @@ GameLib.D3.Scene.prototype.updateInstance = function(property) {
|
|||
*/
|
||||
GameLib.D3.Scene.prototype.toApiObject = function() {
|
||||
|
||||
var apiMeshes = this.meshes.map(
|
||||
function(mesh) {
|
||||
return GameLib.Utils.IdOrNull(mesh);
|
||||
var apiMeshes = this.meshes.reduce(
|
||||
function(result, mesh) {
|
||||
|
||||
/**
|
||||
* Do not store any cloned meshes
|
||||
*/
|
||||
if (this.clones.indexOf(mesh) === -1) {
|
||||
result.push(GameLib.Utils.IdOrNull(mesh));
|
||||
}
|
||||
|
||||
return result;
|
||||
}.bind(this),
|
||||
[]
|
||||
);
|
||||
|
||||
var apiLights = this.lights.map(
|
||||
function(light) {
|
||||
return GameLib.Utils.IdOrNull(light);
|
||||
var apiLights = this.lights.reduce(
|
||||
function(result, light) {
|
||||
|
||||
/**
|
||||
* Do not store any cloned lights
|
||||
*/
|
||||
if (this.clones.indexOf(light) === -1) {
|
||||
result.push(GameLib.Utils.IdOrNull(light));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}.bind(this),
|
||||
[]
|
||||
);
|
||||
|
||||
var apiTextures = this.textures.map(
|
||||
|
|
|
@ -188,16 +188,10 @@ GameLib.Entity.prototype.updateInstance = function() {
|
|||
*/
|
||||
GameLib.Entity.prototype.toApiObject = function() {
|
||||
|
||||
var apiComponents = this.components.reduce(
|
||||
function(result, component) {
|
||||
if (typeof component.toApiObject === 'function') {
|
||||
result.push(component.toApiObject());
|
||||
} else {
|
||||
console.log('ignored runtime component : ' + component.name);
|
||||
var apiComponents = this.components.map(
|
||||
function(component) {
|
||||
return GameLib.Utils.IdOrNull(component);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return new GameLib.API.Entity(
|
||||
|
|
|
@ -479,8 +479,10 @@ GameLib.System.Linking.prototype.componentCloned = function(data) {
|
|||
throw new Error('no scene parent');
|
||||
}
|
||||
|
||||
if (data.parent.parentScene) {
|
||||
data.parent.parentScene.addClone(data.component);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -362,31 +362,33 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
|
||||
return function () {
|
||||
|
||||
var error = false;
|
||||
|
||||
try {
|
||||
var object = JSON.parse(this.responseText);
|
||||
} catch (error) {
|
||||
} catch (errorObject) {
|
||||
|
||||
if (onComponentError) {
|
||||
onComponentError(error);
|
||||
onComponentError(errorObject);
|
||||
}
|
||||
|
||||
if (clientErrorCallback) {
|
||||
clientErrorCallback({
|
||||
message : error.message || 'JSON parse error'
|
||||
message : errorObject.message || 'JSON parse error'
|
||||
})
|
||||
}
|
||||
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.LOAD_COMPONENT_ERROR,
|
||||
{
|
||||
error: error
|
||||
error: errorObject
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (object.result !== 'success') {
|
||||
if (!error && object.result !== 'success') {
|
||||
|
||||
if (onComponentError) {
|
||||
onComponentError(id, object);
|
||||
|
@ -403,15 +405,19 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
{error : object}
|
||||
);
|
||||
|
||||
return;
|
||||
error = true;
|
||||
}
|
||||
|
||||
var runtimeComponent = null;
|
||||
|
||||
if (!error) {
|
||||
|
||||
/**
|
||||
* Now we need to create the runtime component - this happens systematically.
|
||||
* First, we create an API object from the Object, then a Runtime object from the API object
|
||||
* Each component has a function 'FromObject' which essentially does this for you
|
||||
*/
|
||||
object.component.map(function (component) {
|
||||
var component = object.component[0];
|
||||
|
||||
var componentName = GameLib.Component.GetComponentName(component.componentType);
|
||||
|
||||
|
@ -419,8 +425,6 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
|
||||
var fn = componentClass['FromObject'];
|
||||
|
||||
var runtimeComponent = null;
|
||||
|
||||
if (component.componentType === GameLib.Component.COMPONENT_ENTITY) {
|
||||
runtimeComponent = fn(component, GameLib.EntityManager.Instance);
|
||||
runtimeComponent.parentEntity = parentEntity;
|
||||
|
@ -465,7 +469,10 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
|
||||
if (!runtimeComponent) {
|
||||
if (clientErrorCallback) {
|
||||
clientErrorCallback({result : 'failure', message : 'Could not create a runtime component: ' + component.name});
|
||||
clientErrorCallback({
|
||||
result: 'failure',
|
||||
message: 'Could not create a runtime component: ' + component.name
|
||||
});
|
||||
}
|
||||
//throw new Error('Could not create a runtime component: ' + component.name);
|
||||
}
|
||||
|
@ -474,6 +481,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
runtimeComponent.parentEntity = parentEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (runtimeComponent) {
|
||||
loaded.push(runtimeComponent);
|
||||
|
@ -603,7 +611,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
|
|||
} else {
|
||||
download.bind(__system)(toProcess.pop(), parentEntity);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
}(this);
|
||||
|
||||
|
|
Loading…
Reference in New Issue