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
*/
GameLib.Event.WINDOW_RESIZE = 0x1;
GameLib.Event.PARENT_SCENE_CHANGE = 0x2;
GameLib.Event.PARENT_ENTITY_CHANGE = 0x3;
GameLib.Event.IMAGE_LOADED = 0x5;
GameLib.Event.TEXTURE_LOADED = 0x6;
GameLib.Event.LOAD_IMAGE = 0x7;
GameLib.Event.MATERIAL_LOADED = 0x8;
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_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;
GameLib.Event.WINDOW_RESIZE = 0x1;
GameLib.Event.PARENT_SCENE_CHANGE = 0x2;
GameLib.Event.PARENT_ENTITY_CHANGE = 0x3;
GameLib.Event.IMAGE_LOADED = 0x5;
GameLib.Event.TEXTURE_LOADED = 0x6;
GameLib.Event.LOAD_IMAGE = 0x7;
GameLib.Event.MATERIAL_LOADED = 0x8;
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_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;
GameLib.Event.SCENE_INSTANCE_CREATED = 0x15;
GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x16;
GameLib.Event.WORLD_INSTANCE_CREATED = 0x17;
GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x18;
/**
* Subscribe to some events
* @param eventName
@ -55,15 +58,16 @@ GameLib.Event.prototype.subscribe = function(
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 {
fn : fn,
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,
callback
) {
var fn = callback.bind(this);
if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) {
GameLib.Event.OnceSubscriptions[eventName].push(fn);
} else {
GameLib.Event.OnceSubscriptions[eventName] = [];
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 {
fn : fn,
remove : function() {
GameLib.Event.Subscriptions[eventName].splice(index, 1);
}
}
throw new Error('implement first properly');
// var fn = callback.bind(this);
//
// if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) {
// GameLib.Event.OnceSubscriptions[eventName].push(fn);
// } else {
// GameLib.Event.OnceSubscriptions[eventName] = [];
// GameLib.Event.OnceSubscriptions[eventName].push(fn);
// }
//
// /**
// * Return a handle to the caller to allow us to unsubscribe to this event
// */
// return {
// fn : fn,
// remove : function() {
// GameLib.Event.Subscriptions[eventName].splice(
// GameLib.Event.Subscriptions[eventName].indexOf(fn),
// 1
// );
// }
// }
};
/**
@ -133,8 +138,10 @@ GameLib.Event.Emit = function(eventName, data) {
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
GameLib.Event.Subscriptions[eventName].map(
function(callback) {
callback(data);
count++;
if (callback) {
callback(data);
count++;
}
}
)
}

View File

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

View File

@ -14,18 +14,6 @@ GameLib.API.Entity = function(
parentEntity,
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)) {
id = GameLib.Utils.RandomId();
}
@ -40,6 +28,18 @@ GameLib.API.Entity = function(
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);
@ -51,17 +51,10 @@ GameLib.API.Entity.prototype.constructor = GameLib.API.Entity;
* @constructor
*/
GameLib.API.Entity.FromObject = function(objectEntity) {
var apiComponents = objectEntity.components.map(
function (objectComponent) {
return GameLib.API.Component.FromObject(objectComponent);
}
);
return new GameLib.API.Entity(
objectEntity.id,
objectEntity.name,
apiComponents,
objectEntity.components,
objectEntity.parentEntity,
objectEntity.parentEntityManager
)

View File

@ -37,16 +37,8 @@ GameLib.D3.Graphics.GRAPHICS_TYPE_THREE = 0x1;
/**
* @returns {THREE.Graphics}
*/
GameLib.D3.Graphics.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = THREE;
}
GameLib.D3.Graphics.prototype.createInstance = function() {
var instance = THREE;
return instance;
};
@ -54,7 +46,6 @@ GameLib.D3.Graphics.prototype.createInstance = function(update) {
* Updates the instance with the current state
*/
GameLib.D3.Graphics.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
@ -62,14 +53,14 @@ GameLib.D3.Graphics.prototype.updateInstance = function() {
* @returns {boolean}
*/
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
*/
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');
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(
this.graphics,
apiEntity
);

View File

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

View File

@ -5,27 +5,9 @@
* @constructor
*/
GameLib.System = function(
implementation,
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)) {
apiSystem = {};
}

View File

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

View File

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

View File

@ -1,38 +1,55 @@
/**
* System takes care of updating all the entities (based on their component data)
* @param physics
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System.Physics = function(
physics,
apiSystem
) {
GameLib.System.call(
this,
physics,
apiSystem
);
this.worldSubscription = null;
this.rigidBodySubscription = null;
};
GameLib.System.Physics.prototype = Object.create(GameLib.System.prototype);
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(
[
GameLib.D3.World
]
);
};
GameLib.System.Physics.prototype.updateRigidBodies = function() {
this.rigidBodyEntities = GameLib.EntityManager.Instance.query(
[
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() {
this.worlds = [];
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)
* @param graphics
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System.Render = function(
graphics,
apiSystem
) {
GameLib.System.call(
this,
graphics,
apiSystem
);

View File

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