saving entities - start loading them
parent
c7cf4a42c0
commit
8f44b9c672
|
@ -36,6 +36,9 @@ 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,9 +138,11 @@ GameLib.Event.Emit = function(eventName, data) {
|
|||
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
|
||||
GameLib.Event.Subscriptions[eventName].map(
|
||||
function(callback) {
|
||||
if (callback) {
|
||||
callback(data);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -54,7 +54,6 @@ GameLib.EntityManager.prototype.createEntity = function(name) {
|
|||
);
|
||||
|
||||
var entity = new GameLib.Entity(
|
||||
this.graphics,
|
||||
apiEntity
|
||||
);
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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 = {};
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue