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.TEXTURE_TYPE_CHANGE = 0xa;
GameLib.Event.NEW_ENTITY = 0xb; GameLib.Event.NEW_ENTITY = 0xb;
GameLib.Event.MATERIAL_TYPE_CHANGED = 0xc; GameLib.Event.MATERIAL_TYPE_CHANGED = 0xc;
GameLib.Event.SAVE = 0xd; GameLib.Event.SAVE_COMPONENT = 0xd;
GameLib.Event.LOAD = 0xe; GameLib.Event.SAVE_COMPONENT_ERROR = 0xe;
GameLib.Event.LOADED = 0xf; GameLib.Event.COMPONENT_SAVED = 0xf;
GameLib.Event.SAVE_SUCCESS = 0x10; GameLib.Event.LOAD_COMPONENT = 0x10;
GameLib.Event.SAVE_FAILURE = 0x11; GameLib.Event.LOAD_COMPONENT_ERROR = 0x11;
GameLib.Event.LOGGED_IN = 0x12; GameLib.Event.COMPONENT_LOADED = 0x12;
GameLib.Event.COMPONENT_CREATED = 0x13; GameLib.Event.LOGGED_IN = 0x13;
GameLib.Event.COMPONENT_CREATED = 0x14;
/** /**
* Subscribe to some events * Subscribe to some events

View File

@ -59,17 +59,12 @@ GameLib.Component.prototype.getDependencies = function() {
for (var property in this.linkedObjects) { for (var property in this.linkedObjects) {
if ( if (
property === 'parentMesh' || this.linkedObjects.hasOwnProperty(property) &&
property === 'parentScene' || property !== 'parentMesh' &&
property === 'parentEntity' property !== 'parentScene' &&
property !== 'parentEntity' &&
this.hasOwnProperty(property)
){ ){
continue;
}
//TODO array linked objects
if (this.linkedObjects.hasOwnProperty(property)){
if (this.hasOwnProperty(property)) {
if (typeof this[property] === 'string') { if (typeof this[property] === 'string') {
dependencies.push(this[property]); dependencies.push(this[property]);
} }
@ -82,9 +77,6 @@ GameLib.Component.prototype.getDependencies = function() {
}); });
} }
} }
}
} }
return dependencies; return dependencies;
@ -179,6 +171,10 @@ GameLib.Component.prototype.toApiObject = function() {
return this.id; 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() { GameLib.Component.prototype.buildIdToObject = function() {
if (!this.traverse) { 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, * 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 * we link the objects
*/ */
GameLib.Component.prototype.linkObjects = function(idToObject) { // GameLib.Component.prototype.linkObjects = function(idToObject) {
//
if (this.loaded) { // if (this.loaded) {
return; // return;
} // }
//
this.loaded = true; // this.loaded = true;
//
for (var property in this.linkedObjects) { // for (var property in this.linkedObjects) {
if ( // if (
this.linkedObjects.hasOwnProperty(property) && // this.linkedObjects.hasOwnProperty(property) &&
this.hasOwnProperty(property) && // this.hasOwnProperty(property) &&
this[property] // this[property]
) { // ) {
//
if (this.linkedObjects[property] instanceof Array) { // if (this.linkedObjects[property] instanceof Array) {
if (this[property] instanceof Array) { // if (this[property] instanceof Array) {
//
this[property] = this[property].map( // this[property] = this[property].map(
function(p) { // function(p) {
if (p instanceof Object) { // if (p instanceof Object) {
//
//p.parentObjects.push(this); // /**
// * This object is already an object, does not need to be linked
/** // */
* This object is already an object, does not need to be linked // if (p.linkObjects) {
*/ // p.linkObjects(idToObject);
if (p.linkObjects) { // }
p.linkObjects(idToObject); //
} // return p;
// } else if (typeof p === 'string') {
return p; //
} else if (typeof p === 'string') { // if (!idToObject[p]) {
// console.warn('Could not locate the object to be linked in array - fix this');
if (!idToObject[p]) { // throw new Error('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'); // }
throw new Error('Could not locate the object to be linked in array - fix this'); //
} // /**
// * Perform deep-linking
//idToObject[p].parentObjects.push(this); // */
// if (idToObject[p].linkObjects) {
/** // idToObject[p].linkObjects(idToObject);
* Perform deep-linking // }
*/ //
if (idToObject[p].linkObjects) { // return idToObject[p];
idToObject[p].linkObjects(idToObject); // } else {
} // console.warn('Unhandled type : ', p);
// throw new Error('Unhandled type : ', p);
return idToObject[p]; // }
} else { //
console.warn('Unhandled type : ', p); // }.bind(this)
throw new Error('Unhandled type : ', p); // )
} // } else {
// console.warn('Incompatible Link Type - should be instance of array');
}.bind(this) // throw new Error('Incompatible Link Type - should be instance of array');
) // }
} else { // } else {
console.warn('Incompatible Link Type - should be instance of array'); //
throw new Error('Incompatible Link Type - should be instance of array'); // if (this[property] instanceof Object) {
} //
} else { // /**
// * This object is already an object, perform deep linking
if (this[property] instanceof Object) { // */
// if (this[property].linkObjects) {
/** // this[property].linkObjects(idToObject);
* This object is already an object, perform deep linking // }
*/ //
if (this[property].linkObjects) { // } else if (typeof this[property] === 'string') {
this[property].linkObjects(idToObject); //
} // if (!idToObject[this[property]]) {
// console.warn('Could not locate the object to be linked - fix this');
} else if (typeof this[property] === 'string') { // throw new Error('Could not locate the object to be linked - fix this');
// }
if (!idToObject[this[property]]) { //
console.warn('Could not locate the object to be linked - fix this'); // this[property] = idToObject[this[property]];
throw new Error('Could not locate the object to be linked - fix this'); //
} // //this[property].parentObjects.push(this);
//
this[property] = idToObject[this[property]]; // /**
// * Perform deep-linking
//this[property].parentObjects.push(this); // */
// if (this[property].linkObjects) {
/** // this[property].linkObjects(idToObject);
* Perform deep-linking // }
*/ // } else {
if (this[property].linkObjects) { // console.warn('Unhandled property type - fix this : ' + typeof this[property]);
this[property].linkObjects(idToObject); // throw new Error('Unhandled property type - fix this : ' + typeof this[property]);
} // }
} 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;
} // };
}
this.loaded = false;
};
GameLib.Component.prototype.clone = function() { GameLib.Component.prototype.clone = function() {
@ -336,7 +328,7 @@ GameLib.Component.prototype.clone = function() {
GameLib.Component.prototype.getStorageDependencies = function() { GameLib.Component.prototype.getStorageDependencies = function() {
var dependencies = []; var dependencies = {};
for (var property in this.linkedObjects) { for (var property in this.linkedObjects) {
if (this.linkedObjects.hasOwnProperty(property)){ if (this.linkedObjects.hasOwnProperty(property)){
@ -346,15 +338,23 @@ GameLib.Component.prototype.getStorageDependencies = function() {
this[property] instanceof Array && this[property] instanceof Array &&
this.linkedObjects[property] instanceof Array this.linkedObjects[property] instanceof Array
) { ) {
this[property].map(
function(arrayProperty){ if (this.linkedObjects[property].length !== 1) {
if (arrayProperty instanceof this.linkedObjects[0]) { console.log('Invalid formed argument type');
dependencies.push(GameLib.Utils.IdOrNull(arrayProperty)); }
dependencies[property] = this[property].map(
function(__constructor) {
return function(arrayProperty){
if (arrayProperty instanceof __constructor) {
return GameLib.Utils.IdOrNull(arrayProperty);
} }
}.bind(this) }.bind(this)
}(this.linkedObjects[property][0])
); );
} else if (this[property] instanceof this.linkedObjects[property]) { } 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() { GameLib.Component.prototype.save = function() {
//
// if (!this.canSave) {
// return;
// }
//
// this.canSave = false;
this.buildIdToObject(); this.buildIdToObject();
@ -380,16 +374,20 @@ GameLib.Component.prototype.save = function() {
) { ) {
var apiObject = this.idToObject[property].toApiObject(); 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( this.publish(
GameLib.Event.SAVE, GameLib.Event.SAVE_COMPONENT,
apiObject apiObject
); );
} }
} }
}; };

View File

@ -190,3 +190,15 @@ GameLib.D3.Image.FromObject = function(graphics, objectLight) {
GameLib.D3.API.Image.FromObject(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.loginSubscription = null;
this.saveSubscription = null; this.saveSubscription = null;
this.loadSubscription = null; this.loadSubscription = null;
}; };
GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype); GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype);
@ -48,12 +47,12 @@ GameLib.System.Storage.prototype.start = function() {
); );
this.saveSubscription = this.subscribe( this.saveSubscription = this.subscribe(
GameLib.Event.SAVE, GameLib.Event.SAVE_COMPONENT,
this.save this.save
); );
this.loadSubscription = this.subscribe( this.loadSubscription = this.subscribe(
GameLib.Event.LOAD, GameLib.Event.LOAD_COMPONENT,
this.load this.load
) )
}; };
@ -84,7 +83,7 @@ GameLib.System.Storage.prototype.save = function(data) {
var response = JSON.parse(this.responseText) var response = JSON.parse(this.responseText)
} catch (error) { } catch (error) {
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.SAVE_FAILURE, GameLib.Event.SAVE_COMPONENT_ERROR,
{ {
message: this.responseText message: this.responseText
} }
@ -93,14 +92,14 @@ GameLib.System.Storage.prototype.save = function(data) {
if (response.result === 'success') { if (response.result === 'success') {
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.SAVE_SUCCESS, GameLib.Event.COMPONENT_SAVED,
{ {
message: response.message || 'Successfully saved the component' message: response.message || 'Successfully saved the component'
} }
) )
} else { } else {
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.SAVE_FAILURE, GameLib.Event.SAVE_COMPONENT_ERROR,
{ {
message: response.message || 'The server responded but failed to save the component' 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) { GameLib.System.Storage.prototype.load = function(data) {
@ -130,16 +129,39 @@ GameLib.System.Storage.prototype.load = function(data) {
xhr.open( xhr.open(
'GET', 'GET',
this.apiUrl + '/component/' + data.id data.url
); );
xhr.onreadystatechange = function (xhr) { xhr.onreadystatechange = function (xhr) {
return function () { return function () {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
try {
var object = JSON.parse(xhr.responseText);
} catch (error) {
this.publish( 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
} }
) )
} }