box shape object loading and saving ok

beta.r3js.org
-=yb4f310 2017-09-01 08:56:39 +02:00
parent 4811749319
commit 71be2f19af
6 changed files with 65 additions and 15 deletions

View File

@ -67,6 +67,7 @@ GameLib.Event.MATERIAL_TEXTURES_UPDATED = 0x31;
GameLib.Event.DELETE_COMPONENT_ERROR = 0x32; GameLib.Event.DELETE_COMPONENT_ERROR = 0x32;
GameLib.Event.COMPONENT_DELETED = 0x33; GameLib.Event.COMPONENT_DELETED = 0x33;
GameLib.Event.COMPONENT_TYPES_UPDATED = 0x34; GameLib.Event.COMPONENT_TYPES_UPDATED = 0x34;
GameLib.Event.SHAPE_INSTANCE_CREATED = 0x35;
/** /**
* Returns string name of event ID * Returns string name of event ID
@ -129,6 +130,7 @@ GameLib.Event.GetEventName = function(number) {
case 0x32 : return 'delete_component_error'; case 0x32 : return 'delete_component_error';
case 0x33 : return 'component_deleted'; case 0x33 : return 'component_deleted';
case 0x34 : return 'component_types_updated'; case 0x34 : return 'component_types_updated';
case 0x35 : return 'shape_instance_created';
break; break;
} }
@ -214,6 +216,17 @@ GameLib.Event.EmitInstanceEvents = function(component) {
} }
) )
} }
if (
component instanceof GameLib.D3.Shape
) {
GameLib.Event.Emit(
GameLib.Event.SHAPE_INSTANCE_CREATED,
{
shape: component
}
)
}
}; };
// /** // /**

View File

@ -32,7 +32,7 @@ GameLib.D3.Shape = function (
if (this instanceof GameLib.D3.Shape.Box) { if (this instanceof GameLib.D3.Shape.Box) {
componentType = GameLib.Component.COMPONENT_SHAPE_BOX; componentType = GameLib.Component.COMPONENT_SHAPE_BOX;
linkedObjects.activeMesh = GameLib.D3.Mesh; linkedObjects.parentMesh = GameLib.D3.Mesh;
} }
if (this instanceof GameLib.D3.Shape.Sphere) { if (this instanceof GameLib.D3.Shape.Sphere) {

View File

@ -3,14 +3,14 @@
* @param physics * @param physics
* @param apiShape GameLib.D3.API.Shape * @param apiShape GameLib.D3.API.Shape
* @param halfExtents * @param halfExtents
* @param activeMesh * @param parentMesh
* @constructor * @constructor
*/ */
GameLib.D3.Shape.Box = function ( GameLib.D3.Shape.Box = function (
physics, physics,
apiShape, apiShape,
halfExtents, halfExtents,
activeMesh parentMesh
) { ) {
this.physics = physics; this.physics = physics;
this.physics.isNotCannonThrow(); this.physics.isNotCannonThrow();
@ -31,10 +31,10 @@ GameLib.D3.Shape.Box = function (
} }
this.halfExtents = halfExtents; this.halfExtents = halfExtents;
if (GameLib.Utils.UndefinedOrNull(activeMesh)) { if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
activeMesh = null; parentMesh = null;
} }
this.activeMesh = activeMesh; this.parentMesh = parentMesh;
GameLib.D3.Shape.call( GameLib.D3.Shape.call(
this, this,
@ -76,19 +76,19 @@ GameLib.D3.Shape.Box.prototype.toApiObject = function() {
var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this); var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this);
apiShape.halfExtents = this.halfExtents.toApiObject(); apiShape.halfExtents = this.halfExtents.toApiObject();
apiShape.activeMesh = GameLib.Utils.IdOrNull(this.activeMesh); apiShape.parentMesh = GameLib.Utils.IdOrNull(this.parentMesh);
return apiShape; return apiShape;
}; };
GameLib.D3.Shape.Box.prototype.setFromMesh = function() { GameLib.D3.Shape.Box.prototype.setFromMesh = function() {
if (this.activeMesh === null) { if (this.parentMesh === null) {
console.log('select a mesh first'); console.log('select a mesh first');
return; return;
} }
var box = this.activeMesh.getBoundingBox(); var box = this.parentMesh.getBoundingBox();
this.halfExtents.x = box.x / 2; this.halfExtents.x = box.x / 2;
this.halfExtents.y = box.y / 2; this.halfExtents.y = box.y / 2;
@ -102,13 +102,13 @@ GameLib.D3.Shape.Box.FromObject = function(physics, objectShape) {
var apiShape = GameLib.D3.API.Shape.FromObject(objectShape); var apiShape = GameLib.D3.API.Shape.FromObject(objectShape);
apiShape.halfExtents = GameLib.API.Vector3.FromObject(objectShape.halfExtents); apiShape.halfExtents = GameLib.API.Vector3.FromObject(objectShape.halfExtents);
apiShape.activeMesh = objectShape.activeMesh; apiShape.parentMesh = objectShape.parentMesh;
return new GameLib.D3.Shape.Box( return new GameLib.D3.Shape.Box(
physics, physics,
apiShape, apiShape,
apiShape.halfExtents, apiShape.halfExtents,
apiShape.activeMesh apiShape.parentMesh
); );
}; };

View File

@ -61,7 +61,7 @@ GameLib.EntityManager.prototype.registerComponent = function(data) {
GameLib.EntityManager.prototype.removeComponent = function(data) { GameLib.EntityManager.prototype.removeComponent = function(data) {
if (data.component.parentEntity) { if (data.component.parentEntity instanceof GameLib.Entity) {
data.component.parentEntity.removeComponent(data.component); data.component.parentEntity.removeComponent(data.component);
} }

View File

@ -37,6 +37,7 @@ GameLib.System.Linking = function(
this.meshDeletedSubscription = null; this.meshDeletedSubscription = null;
this.imageChangedSubscription = null; this.imageChangedSubscription = null;
this.materialTypeChangedSubscription = null; this.materialTypeChangedSubscription = null;
this.shapeInstanceCreatedSubscription = null;
}; };
GameLib.System.Linking.prototype = Object.create(GameLib.System.prototype); GameLib.System.Linking.prototype = Object.create(GameLib.System.prototype);
@ -107,7 +108,12 @@ GameLib.System.Linking.prototype.start = function() {
this.materialTypeChangedSubscription = this.subscribe( this.materialTypeChangedSubscription = this.subscribe(
GameLib.Event.MATERIAL_TYPE_CHANGED, GameLib.Event.MATERIAL_TYPE_CHANGED,
this.materialTypeChanged this.materialTypeChanged
) );
this.shapeInstanceCreatedSubscription = this.subscribe(
GameLib.Event.SHAPE_INSTANCE_CREATED,
this.shapeInstanceCreated
);
}; };
GameLib.System.Linking.prototype.link = function(component, data) { GameLib.System.Linking.prototype.link = function(component, data) {
@ -424,7 +430,7 @@ GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh); var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
/** /**
* Link Parent Meshes * Link Parent Meshes for other meshes
*/ */
meshes.map( meshes.map(
function(mesh) { function(mesh) {
@ -443,6 +449,34 @@ GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
}.bind(this) }.bind(this)
); );
/**
* Link Parent Meshes for shapes
*/
var shapes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Shape);
shapes.map(function(shape){
if (shape.parentMesh === data.mesh.id) {
shape.parentMesh = data.mesh;
}
})
};
GameLib.System.Linking.prototype.shapeInstanceCreated = function(data) {
/**
* When a shape instance is created, just check if the parentMesh is loaded somewhere and set it.
*
* We also do the reverse, when a mesh instance is created, we check if its the parent of any shape.
*/
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
meshes.map(
function(mesh) {
if (data.shape.parentMesh === mesh.id) {
data.shape.parentMesh = mesh;
}
}
);
}; };
GameLib.System.Linking.prototype.lightInstanceCreated = function(data) { GameLib.System.Linking.prototype.lightInstanceCreated = function(data) {
@ -709,5 +743,6 @@ GameLib.System.Linking.prototype.stop = function() {
this.meshDeletedSubscription.remove(); this.meshDeletedSubscription.remove();
this.imageChangedSubscription.remove(); this.imageChangedSubscription.remove();
this.materialTypeChangedSubscription.remove(); this.materialTypeChangedSubscription.remove();
this.shapeInstanceCreatedSubscription.remove();
}; };

View File

@ -380,7 +380,9 @@ GameLib.System.Storage.prototype.loadComponent = function(toProcess, includeDepe
throw new Error('Could not create a runtime component: ', component); throw new Error('Could not create a runtime component: ', component);
} }
runtimeComponent.parentEntity = parentEntity; if (parentEntity !== null) {
runtimeComponent.parentEntity = parentEntity;
}
} }
if (runtimeComponent instanceof GameLib.D3.Image) { if (runtimeComponent instanceof GameLib.D3.Image) {