component loading continues

beta.r3js.org
-=yb4f310 2017-06-19 21:35:51 +02:00
parent 21b79c6515
commit bc56fe4088
4 changed files with 192 additions and 159 deletions

View File

@ -25,13 +25,14 @@ GameLib.Event.IMAGE_CHANGE = 0x9;
GameLib.Event.TEXTURE_TYPE_CHANGE = 0xa;
GameLib.Event.NEW_ENTITY = 0xb;
GameLib.Event.MATERIAL_TYPE_CHANGED = 0xc;
GameLib.Event.SAVE = 0xd;
GameLib.Event.LOAD = 0xe;
GameLib.Event.LOADED = 0xf;
GameLib.Event.SAVE_SUCCESS = 0x10;
GameLib.Event.SAVE_FAILURE = 0x11;
GameLib.Event.LOGGED_IN = 0x12;
GameLib.Event.COMPONENT_CREATED = 0x13;
GameLib.Event.SAVE_COMPONENT = 0xd;
GameLib.Event.SAVE_COMPONENT_ERROR = 0xe;
GameLib.Event.COMPONENT_SAVED = 0xf;
GameLib.Event.LOAD_COMPONENT = 0x10;
GameLib.Event.LOAD_COMPONENT_ERROR = 0x11;
GameLib.Event.COMPONENT_LOADED = 0x12;
GameLib.Event.LOGGED_IN = 0x13;
GameLib.Event.COMPONENT_CREATED = 0x14;
/**
* Subscribe to some events

View File

@ -59,17 +59,12 @@ GameLib.Component.prototype.getDependencies = function() {
for (var property in this.linkedObjects) {
if (
property === 'parentMesh' ||
property === 'parentScene' ||
property === 'parentEntity'
) {
continue;
}
//TODO array linked objects
if (this.linkedObjects.hasOwnProperty(property)){
if (this.hasOwnProperty(property)) {
this.linkedObjects.hasOwnProperty(property) &&
property !== 'parentMesh' &&
property !== 'parentScene' &&
property !== 'parentEntity' &&
this.hasOwnProperty(property)
){
if (typeof this[property] === 'string') {
dependencies.push(this[property]);
}
@ -82,9 +77,6 @@ GameLib.Component.prototype.getDependencies = function() {
});
}
}
}
}
return dependencies;
@ -179,6 +171,10 @@ GameLib.Component.prototype.toApiObject = function() {
return this.id;
};
/**
* This function - builds an 'id to object' object - which contains the ids which point directly
* to its corresponding object, for all the objects contained inside this object
*/
GameLib.Component.prototype.buildIdToObject = function() {
if (!this.traverse) {
@ -223,104 +219,100 @@ GameLib.Component.prototype.buildIdToObject = function() {
* Instead, we store them on the scene object, reference them by string id from the mesh, and after scene has loaded,
* we link the objects
*/
GameLib.Component.prototype.linkObjects = function(idToObject) {
if (this.loaded) {
return;
}
this.loaded = true;
for (var property in this.linkedObjects) {
if (
this.linkedObjects.hasOwnProperty(property) &&
this.hasOwnProperty(property) &&
this[property]
) {
if (this.linkedObjects[property] instanceof Array) {
if (this[property] instanceof Array) {
this[property] = this[property].map(
function(p) {
if (p instanceof Object) {
//p.parentObjects.push(this);
/**
* This object is already an object, does not need to be linked
*/
if (p.linkObjects) {
p.linkObjects(idToObject);
}
return p;
} else if (typeof p === 'string') {
if (!idToObject[p]) {
console.warn('Could not locate the object to be linked in array - fix this');
throw new Error('Could not locate the object to be linked in array - fix this');
}
//idToObject[p].parentObjects.push(this);
/**
* Perform deep-linking
*/
if (idToObject[p].linkObjects) {
idToObject[p].linkObjects(idToObject);
}
return idToObject[p];
} else {
console.warn('Unhandled type : ', p);
throw new Error('Unhandled type : ', p);
}
}.bind(this)
)
} else {
console.warn('Incompatible Link Type - should be instance of array');
throw new Error('Incompatible Link Type - should be instance of array');
}
} else {
if (this[property] instanceof Object) {
/**
* This object is already an object, perform deep linking
*/
if (this[property].linkObjects) {
this[property].linkObjects(idToObject);
}
} else if (typeof this[property] === 'string') {
if (!idToObject[this[property]]) {
console.warn('Could not locate the object to be linked - fix this');
throw new Error('Could not locate the object to be linked - fix this');
}
this[property] = idToObject[this[property]];
//this[property].parentObjects.push(this);
/**
* Perform deep-linking
*/
if (this[property].linkObjects) {
this[property].linkObjects(idToObject);
}
} else {
console.warn('Unhandled property type - fix this : ' + typeof this[property]);
throw new Error('Unhandled property type - fix this : ' + typeof this[property]);
}
}
}
}
this.loaded = false;
};
// GameLib.Component.prototype.linkObjects = function(idToObject) {
//
// if (this.loaded) {
// return;
// }
//
// this.loaded = true;
//
// for (var property in this.linkedObjects) {
// if (
// this.linkedObjects.hasOwnProperty(property) &&
// this.hasOwnProperty(property) &&
// this[property]
// ) {
//
// if (this.linkedObjects[property] instanceof Array) {
// if (this[property] instanceof Array) {
//
// this[property] = this[property].map(
// function(p) {
// if (p instanceof Object) {
//
// /**
// * This object is already an object, does not need to be linked
// */
// if (p.linkObjects) {
// p.linkObjects(idToObject);
// }
//
// return p;
// } else if (typeof p === 'string') {
//
// if (!idToObject[p]) {
// console.warn('Could not locate the object to be linked in array - fix this');
// throw new Error('Could not locate the object to be linked in array - fix this');
// }
//
// /**
// * Perform deep-linking
// */
// if (idToObject[p].linkObjects) {
// idToObject[p].linkObjects(idToObject);
// }
//
// return idToObject[p];
// } else {
// console.warn('Unhandled type : ', p);
// throw new Error('Unhandled type : ', p);
// }
//
// }.bind(this)
// )
// } else {
// console.warn('Incompatible Link Type - should be instance of array');
// throw new Error('Incompatible Link Type - should be instance of array');
// }
// } else {
//
// if (this[property] instanceof Object) {
//
// /**
// * This object is already an object, perform deep linking
// */
// if (this[property].linkObjects) {
// this[property].linkObjects(idToObject);
// }
//
// } else if (typeof this[property] === 'string') {
//
// if (!idToObject[this[property]]) {
// console.warn('Could not locate the object to be linked - fix this');
// throw new Error('Could not locate the object to be linked - fix this');
// }
//
// this[property] = idToObject[this[property]];
//
// //this[property].parentObjects.push(this);
//
// /**
// * Perform deep-linking
// */
// if (this[property].linkObjects) {
// this[property].linkObjects(idToObject);
// }
// } else {
// console.warn('Unhandled property type - fix this : ' + typeof this[property]);
// throw new Error('Unhandled property type - fix this : ' + typeof this[property]);
// }
// }
// }
// }
//
// this.loaded = false;
// };
GameLib.Component.prototype.clone = function() {
@ -336,7 +328,7 @@ GameLib.Component.prototype.clone = function() {
GameLib.Component.prototype.getStorageDependencies = function() {
var dependencies = [];
var dependencies = {};
for (var property in this.linkedObjects) {
if (this.linkedObjects.hasOwnProperty(property)){
@ -346,15 +338,23 @@ GameLib.Component.prototype.getStorageDependencies = function() {
this[property] instanceof Array &&
this.linkedObjects[property] instanceof Array
) {
this[property].map(
function(arrayProperty){
if (arrayProperty instanceof this.linkedObjects[0]) {
dependencies.push(GameLib.Utils.IdOrNull(arrayProperty));
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.push(GameLib.Utils.IdOrNull(this[property]));
dependencies[property] = GameLib.Utils.IdOrNull(this[property]);
}
}
}
@ -364,12 +364,6 @@ GameLib.Component.prototype.getStorageDependencies = function() {
};
GameLib.Component.prototype.save = function() {
//
// if (!this.canSave) {
// return;
// }
//
// this.canSave = false;
this.buildIdToObject();
@ -380,16 +374,20 @@ GameLib.Component.prototype.save = function() {
) {
var apiObject = this.idToObject[property].toApiObject();
delete apiObject.linkedObjects;
apiObject.componentType = this.idToObject[property].componentType;
delete apiObject.idToObject;
var storageDependencies = this.idToObject[property].getStorageDependencies();
for (var storageProperty in storageDependencies) {
if (storageDependencies.hasOwnProperty(storageProperty)) {
apiObject[storageProperty] = storageDependencies[storageProperty];
}
}
this.publish(
GameLib.Event.SAVE,
GameLib.Event.SAVE_COMPONENT,
apiObject
);
}
}
};

View File

@ -190,3 +190,15 @@ GameLib.D3.Image.FromObject = function(graphics, objectLight) {
GameLib.D3.API.Image.FromObject(objectLight)
);
};
GameLib.D3.Image.prototype.load = function(baseUrl, path, textureType) {
GameLib.Event.Emit(
GameLib.Event.LOAD_IMAGE,
{
baseUrl : baseUrl,
path : path,
textureType : textureType
}
);
};

View File

@ -32,7 +32,6 @@ GameLib.System.Storage = function(
this.loginSubscription = null;
this.saveSubscription = null;
this.loadSubscription = null;
};
GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype);
@ -48,12 +47,12 @@ GameLib.System.Storage.prototype.start = function() {
);
this.saveSubscription = this.subscribe(
GameLib.Event.SAVE,
GameLib.Event.SAVE_COMPONENT,
this.save
);
this.loadSubscription = this.subscribe(
GameLib.Event.LOAD,
GameLib.Event.LOAD_COMPONENT,
this.load
)
};
@ -84,7 +83,7 @@ GameLib.System.Storage.prototype.save = function(data) {
var response = JSON.parse(this.responseText)
} catch (error) {
GameLib.Event.Emit(
GameLib.Event.SAVE_FAILURE,
GameLib.Event.SAVE_COMPONENT_ERROR,
{
message: this.responseText
}
@ -93,14 +92,14 @@ GameLib.System.Storage.prototype.save = function(data) {
if (response.result === 'success') {
GameLib.Event.Emit(
GameLib.Event.SAVE_SUCCESS,
GameLib.Event.COMPONENT_SAVED,
{
message: response.message || 'Successfully saved the component'
}
)
} else {
GameLib.Event.Emit(
GameLib.Event.SAVE_FAILURE,
GameLib.Event.SAVE_COMPONENT_ERROR,
{
message: response.message || 'The server responded but failed to save the component'
}
@ -117,7 +116,7 @@ GameLib.System.Storage.prototype.save = function(data) {
};
/**
* 'Loads' data from baseURL
* 'Loads' data from a url
*/
GameLib.System.Storage.prototype.load = function(data) {
@ -130,16 +129,39 @@ GameLib.System.Storage.prototype.load = function(data) {
xhr.open(
'GET',
this.apiUrl + '/component/' + data.id
data.url
);
xhr.onreadystatechange = function (xhr) {
return function () {
if (xhr.readyState === 4) {
try {
var object = JSON.parse(xhr.responseText);
} catch (error) {
this.publish(
GameLib.Event.LOADED,
GameLib.Event.LOAD_COMPONENT_ERROR,
{
component: JSON.parse(xhr.responseText)
error : error
}
);
return;
}
if (object.result !== 'success') {
this.publish(
GameLib.Event.LOAD_COMPONENT_ERROR,
{
error : object
}
);
return;
}
this.publish(
GameLib.Event.COMPONENT_LOADED,
{
response : object
}
)
}