saving entities - start loading them

beta.r3js.org
Theunis J. Botha 2017-06-25 13:31:24 +02:00
parent c7cf4a42c0
commit 8f44b9c672
12 changed files with 116 additions and 125 deletions

View File

@ -15,27 +15,30 @@ GameLib.Event.OnceSubscriptions = {};
/** /**
* Events we can subscribe to and publish * Events we can subscribe to and publish
*/ */
GameLib.Event.WINDOW_RESIZE = 0x1; GameLib.Event.WINDOW_RESIZE = 0x1;
GameLib.Event.PARENT_SCENE_CHANGE = 0x2; GameLib.Event.PARENT_SCENE_CHANGE = 0x2;
GameLib.Event.PARENT_ENTITY_CHANGE = 0x3; GameLib.Event.PARENT_ENTITY_CHANGE = 0x3;
GameLib.Event.IMAGE_LOADED = 0x5; GameLib.Event.IMAGE_LOADED = 0x5;
GameLib.Event.TEXTURE_LOADED = 0x6; GameLib.Event.TEXTURE_LOADED = 0x6;
GameLib.Event.LOAD_IMAGE = 0x7; GameLib.Event.LOAD_IMAGE = 0x7;
GameLib.Event.MATERIAL_LOADED = 0x8; GameLib.Event.MATERIAL_LOADED = 0x8;
GameLib.Event.IMAGE_CHANGE = 0x9; 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_COMPONENT = 0xd; GameLib.Event.SAVE_COMPONENT = 0xd;
GameLib.Event.SAVE_COMPONENT_ERROR = 0xe; GameLib.Event.SAVE_COMPONENT_ERROR = 0xe;
GameLib.Event.COMPONENT_SAVED = 0xf; GameLib.Event.COMPONENT_SAVED = 0xf;
GameLib.Event.LOAD_COMPONENT = 0x10; GameLib.Event.LOAD_COMPONENT = 0x10;
GameLib.Event.LOAD_COMPONENT_ERROR = 0x11; GameLib.Event.LOAD_COMPONENT_ERROR = 0x11;
GameLib.Event.COMPONENT_LOADED = 0x12; GameLib.Event.COMPONENT_LOADED = 0x12;
GameLib.Event.LOGGED_IN = 0x13; GameLib.Event.LOGGED_IN = 0x13;
GameLib.Event.COMPONENT_CREATED = 0x14; GameLib.Event.COMPONENT_CREATED = 0x14;
GameLib.Event.SCENE_INSTANCE_CREATED = 0x15; GameLib.Event.SCENE_INSTANCE_CREATED = 0x15;
GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x16; GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x16;
GameLib.Event.WORLD_INSTANCE_CREATED = 0x17;
GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x18;
/** /**
* Subscribe to some events * Subscribe to some events
* @param eventName * @param eventName
@ -55,15 +58,16 @@ GameLib.Event.prototype.subscribe = function(
GameLib.Event.Subscriptions[eventName].push(fn); GameLib.Event.Subscriptions[eventName].push(fn);
} }
var index = GameLib.Event.Subscriptions[eventName].indexOf(fn);
/** /**
* Return a handle to the caller to allow us to unsubscribe to this event * Return a handle to the caller to allow us to unsubscribe to this event
*/ */
return { return {
fn : fn, fn : fn,
remove : function() { remove : function() {
GameLib.Event.Subscriptions[eventName].splice(index, 1); GameLib.Event.Subscriptions[eventName].splice(
GameLib.Event.Subscriptions[eventName].indexOf(fn),
1
);
} }
} }
}; };
@ -78,27 +82,28 @@ GameLib.Event.prototype.subscribeOnce = function(
eventName, eventName,
callback callback
) { ) {
throw new Error('implement first properly');
var fn = callback.bind(this); // var fn = callback.bind(this);
//
if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) { // if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) {
GameLib.Event.OnceSubscriptions[eventName].push(fn); // GameLib.Event.OnceSubscriptions[eventName].push(fn);
} else { // } else {
GameLib.Event.OnceSubscriptions[eventName] = []; // GameLib.Event.OnceSubscriptions[eventName] = [];
GameLib.Event.OnceSubscriptions[eventName].push(fn); // GameLib.Event.OnceSubscriptions[eventName].push(fn);
} // }
//
var index = GameLib.Event.OnceSubscriptions[eventName].indexOf(fn); // /**
// * Return a handle to the caller to allow us to unsubscribe to this event
/** // */
* Return a handle to the caller to allow us to unsubscribe to this event // return {
*/ // fn : fn,
return { // remove : function() {
fn : fn, // GameLib.Event.Subscriptions[eventName].splice(
remove : function() { // GameLib.Event.Subscriptions[eventName].indexOf(fn),
GameLib.Event.Subscriptions[eventName].splice(index, 1); // 1
} // );
} // }
// }
}; };
/** /**
@ -133,8 +138,10 @@ GameLib.Event.Emit = function(eventName, data) {
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
GameLib.Event.Subscriptions[eventName].map( GameLib.Event.Subscriptions[eventName].map(
function(callback) { function(callback) {
callback(data); if (callback) {
count++; callback(data);
count++;
}
} }
) )
} }

View File

@ -61,6 +61,7 @@ GameLib.Component.prototype.getDependencies = function() {
property !== 'parentMesh' && property !== 'parentMesh' &&
property !== 'parentScene' && property !== 'parentScene' &&
property !== 'parentEntity' && property !== 'parentEntity' &&
property !== 'parentEntityManager' &&
this.hasOwnProperty(property) this.hasOwnProperty(property)
){ ){
if (typeof this[property] === 'string') { if (typeof this[property] === 'string') {
@ -224,7 +225,8 @@ GameLib.Component.prototype.buildIdToObject = function() {
this[property] && this[property] &&
property !== 'parentEntity' && property !== 'parentEntity' &&
property !== 'parentScene' && property !== 'parentScene' &&
property !== 'parentMesh' property !== 'parentMesh' &&
property !== 'parentEntityManager'
) { ) {
if (this.linkedObjects[property] instanceof Array) { if (this.linkedObjects[property] instanceof Array) {
this.idToObject = GameLib.Utils.LoadIdsFromArrayToIdObject(this[property], this.idToObject); this.idToObject = GameLib.Utils.LoadIdsFromArrayToIdObject(this[property], this.idToObject);

View File

@ -14,18 +14,6 @@ GameLib.API.Entity = function(
parentEntity, parentEntity,
parentEntityManager parentEntityManager
) { ) {
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
if (GameLib.Utils.UndefinedOrNull(parentEntityManager)) {
parentEntityManager = null;
}
this.parentEntityManager = parentEntityManager;
this.activeComponent = null;
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
} }
@ -40,6 +28,18 @@ GameLib.API.Entity = function(
components = []; components = [];
} }
this.components = components; this.components = components;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
if (GameLib.Utils.UndefinedOrNull(parentEntityManager)) {
parentEntityManager = null;
}
this.parentEntityManager = parentEntityManager;
this.activeComponent = null;
}; };
GameLib.API.Entity.prototype = Object.create(GameLib.Component.prototype); GameLib.API.Entity.prototype = Object.create(GameLib.Component.prototype);
@ -51,17 +51,10 @@ GameLib.API.Entity.prototype.constructor = GameLib.API.Entity;
* @constructor * @constructor
*/ */
GameLib.API.Entity.FromObject = function(objectEntity) { GameLib.API.Entity.FromObject = function(objectEntity) {
var apiComponents = objectEntity.components.map(
function (objectComponent) {
return GameLib.API.Component.FromObject(objectComponent);
}
);
return new GameLib.API.Entity( return new GameLib.API.Entity(
objectEntity.id, objectEntity.id,
objectEntity.name, objectEntity.name,
apiComponents, objectEntity.components,
objectEntity.parentEntity, objectEntity.parentEntity,
objectEntity.parentEntityManager objectEntity.parentEntityManager
) )

View File

@ -37,16 +37,8 @@ GameLib.D3.Graphics.GRAPHICS_TYPE_THREE = 0x1;
/** /**
* @returns {THREE.Graphics} * @returns {THREE.Graphics}
*/ */
GameLib.D3.Graphics.prototype.createInstance = function(update) { GameLib.D3.Graphics.prototype.createInstance = function() {
var instance = THREE;
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = THREE;
}
return instance; return instance;
}; };
@ -54,7 +46,6 @@ GameLib.D3.Graphics.prototype.createInstance = function(update) {
* Updates the instance with the current state * Updates the instance with the current state
*/ */
GameLib.D3.Graphics.prototype.updateInstance = function() { GameLib.D3.Graphics.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
}; };
/** /**
@ -62,14 +53,14 @@ GameLib.D3.Graphics.prototype.updateInstance = function() {
* @returns {boolean} * @returns {boolean}
*/ */
GameLib.D3.Graphics.prototype.isThree = function() { GameLib.D3.Graphics.prototype.isThree = function() {
return (this.graphicsType == GameLib.D3.Graphics.GRAPHICS_TYPE_THREE) return (this.graphicsType === GameLib.D3.Graphics.GRAPHICS_TYPE_THREE)
}; };
/** /**
* Logs a warning and throws an error if not cannon * Logs a warning and throws an error if not cannon
*/ */
GameLib.D3.Graphics.prototype.isNotThreeThrow = function() { GameLib.D3.Graphics.prototype.isNotThreeThrow = function() {
if (this.graphicsType != GameLib.D3.Graphics.GRAPHICS_TYPE_THREE) { if (this.graphicsType !== GameLib.D3.Graphics.GRAPHICS_TYPE_THREE) {
console.warn('Only THREE supported for this function'); console.warn('Only THREE supported for this function');
throw new Error('Only THREE supported for this function'); throw new Error('Only THREE supported for this function');
} }

View File

@ -54,7 +54,6 @@ GameLib.EntityManager.prototype.createEntity = function(name) {
); );
var entity = new GameLib.Entity( var entity = new GameLib.Entity(
this.graphics,
apiEntity apiEntity
); );

View File

@ -1,17 +1,11 @@
/** /**
* Runtime Entity * Runtime Entity
* @param graphics GameLib.D3.Graphics
* @param apiEntity GameLib.D3.API.Entity * @param apiEntity GameLib.D3.API.Entity
* @constructor * @constructor
*/ */
GameLib.Entity = function ( GameLib.Entity = function (
graphics,
apiEntity apiEntity
) { ) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiEntity)) { if (GameLib.Utils.UndefinedOrNull(apiEntity)) {
apiEntity = {}; apiEntity = {};
} }
@ -222,17 +216,25 @@ GameLib.Entity.prototype.toApiObject = function() {
}; };
/** /**
* * Entity from Object
* @param graphics GameLib.D3.Graphics
* @param objectEntity Object * @param objectEntity Object
* @param parentEntityManager GameLib.D3.EntityManager
* @returns {GameLib.Entity}
* @constructor * @constructor
*/ */
GameLib.Entity.FromObject = function(graphics, objectEntity) { GameLib.Entity.FromObject = function(objectEntity, parentEntityManager) {
var apiEntity = GameLib.API.Entity.FromObject(objectEntity); var apiEntity = GameLib.API.Entity.FromObject(objectEntity);
return new GameLib.Entity( var entity = new GameLib.Entity(
graphics,
apiEntity apiEntity
); );
if (GameLib.Utils.UndefinedOrNull(parentEntityManager)) {
return entity;
}
parentEntityManager.addEntity(entity);
return entity;
}; };

View File

@ -5,27 +5,9 @@
* @constructor * @constructor
*/ */
GameLib.System = function( GameLib.System = function(
implementation,
apiSystem apiSystem
) { ) {
this.implementation = implementation;
if (implementation instanceof GameLib.D3.Graphics) {
this.physics = null;
this.graphics = implementation;
this.graphics.isNotThreeThrow();
} else if (implementation instanceof GameLib.D3.Physics) {
this.graphics = null;
this.physics = implementation;
this.physics.isNotCannonThrow();
} else {
throw new Error('Unhandled implementation : ' + implementation);
}
this.implementation = implementation;
if (GameLib.Utils.UndefinedOrNull(apiSystem)) { if (GameLib.Utils.UndefinedOrNull(apiSystem)) {
apiSystem = {}; apiSystem = {};
} }

View File

@ -1,16 +1,13 @@
/** /**
* System takes care of updating all the entities (based on their component data) * System takes care of updating all the entities (based on their component data)
* @param graphics
* @param apiSystem GameLib.API.System * @param apiSystem GameLib.API.System
* @constructor * @constructor
*/ */
GameLib.System.GUI = function( GameLib.System.GUI = function(
graphics,
apiSystem apiSystem
) { ) {
GameLib.System.call( GameLib.System.call(
this, this,
graphics,
apiSystem apiSystem
); );
}; };

View File

@ -1,16 +1,13 @@
/** /**
* System takes care of updating all the entities (based on their component data) * System takes care of updating all the entities (based on their component data)
* @param graphics
* @param apiSystem GameLib.API.System * @param apiSystem GameLib.API.System
* @constructor * @constructor
*/ */
GameLib.System.Input = function( GameLib.System.Input = function(
graphics,
apiSystem apiSystem
) { ) {
GameLib.System.call( GameLib.System.call(
this, this,
graphics,
apiSystem apiSystem
); );
}; };

View File

@ -1,38 +1,55 @@
/** /**
* System takes care of updating all the entities (based on their component data) * System takes care of updating all the entities (based on their component data)
* @param physics
* @param apiSystem GameLib.API.System * @param apiSystem GameLib.API.System
* @constructor * @constructor
*/ */
GameLib.System.Physics = function( GameLib.System.Physics = function(
physics,
apiSystem apiSystem
) { ) {
GameLib.System.call( GameLib.System.call(
this, this,
physics,
apiSystem apiSystem
); );
this.worldSubscription = null;
this.rigidBodySubscription = null;
}; };
GameLib.System.Physics.prototype = Object.create(GameLib.System.prototype); GameLib.System.Physics.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Physics.prototype.constructor = GameLib.System.Physics; GameLib.System.Physics.prototype.constructor = GameLib.System.Physics;
GameLib.System.Physics.prototype.start = function() { GameLib.System.Physics.prototype.updateWorlds = function() {
this.worlds = GameLib.EntityManager.Instance.query( this.worlds = GameLib.EntityManager.Instance.query(
[ [
GameLib.D3.World GameLib.D3.World
] ]
); );
};
GameLib.System.Physics.prototype.updateRigidBodies = function() {
this.rigidBodyEntities = GameLib.EntityManager.Instance.query( this.rigidBodyEntities = GameLib.EntityManager.Instance.query(
[ [
GameLib.D3.RigidBody GameLib.D3.RigidBody
] ]
); );
};
GameLib.System.Physics.prototype.start = function() {
this.worldSubscription = this.subscribe(
GameLib.Event.WORLD_INSTANCE_CREATED,
this.updateWorlds
);
this.rigidBodySubscription = this.subscribe(
GameLib.Event.RIGID_BODY_INSTANCE_CREATED,
this.updateRigidBodies
);
this.updateWorlds();
this.updateRigidBodies();
}; };
@ -63,7 +80,16 @@ GameLib.System.Physics.prototype.update = function() {
}; };
GameLib.System.Physics.prototype.stop = function() { GameLib.System.Physics.prototype.stop = function() {
this.worlds = []; this.worlds = [];
this.rigidBodyEntities = []; this.rigidBodyEntities = [];
if (this.worldSubscription) {
this.worldSubscription.remove();
}
if (this.rigidBodySubscription) {
this.rigidBodySubscription.remove();
}
}; };

View File

@ -1,17 +1,14 @@
/** /**
* System takes care of updating all the entities (based on their component data) * System takes care of updating all the entities (based on their component data)
* @param graphics
* @param apiSystem GameLib.API.System * @param apiSystem GameLib.API.System
* @constructor * @constructor
*/ */
GameLib.System.Render = function( GameLib.System.Render = function(
graphics,
apiSystem apiSystem
) { ) {
GameLib.System.call( GameLib.System.call(
this, this,
graphics,
apiSystem apiSystem
); );

View File

@ -7,7 +7,6 @@
* @constructor * @constructor
*/ */
GameLib.System.Storage = function( GameLib.System.Storage = function(
graphics,
apiSystem, apiSystem,
apiUrl, apiUrl,
token token
@ -20,7 +19,6 @@ GameLib.System.Storage = function(
GameLib.System.call( GameLib.System.call(
this, this,
graphics,
apiSystem apiSystem
); );