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.COMPONENT_DELETED = 0x33;
GameLib.Event.COMPONENT_TYPES_UPDATED = 0x34;
GameLib.Event.SHAPE_INSTANCE_CREATED = 0x35;
/**
* Returns string name of event ID
@ -129,6 +130,7 @@ GameLib.Event.GetEventName = function(number) {
case 0x32 : return 'delete_component_error';
case 0x33 : return 'component_deleted';
case 0x34 : return 'component_types_updated';
case 0x35 : return 'shape_instance_created';
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) {
componentType = GameLib.Component.COMPONENT_SHAPE_BOX;
linkedObjects.activeMesh = GameLib.D3.Mesh;
linkedObjects.parentMesh = GameLib.D3.Mesh;
}
if (this instanceof GameLib.D3.Shape.Sphere) {

View File

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

View File

@ -61,7 +61,7 @@ GameLib.EntityManager.prototype.registerComponent = 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);
}

View File

@ -37,6 +37,7 @@ GameLib.System.Linking = function(
this.meshDeletedSubscription = null;
this.imageChangedSubscription = null;
this.materialTypeChangedSubscription = null;
this.shapeInstanceCreatedSubscription = null;
};
GameLib.System.Linking.prototype = Object.create(GameLib.System.prototype);
@ -107,7 +108,12 @@ GameLib.System.Linking.prototype.start = function() {
this.materialTypeChangedSubscription = this.subscribe(
GameLib.Event.MATERIAL_TYPE_CHANGED,
this.materialTypeChanged
)
);
this.shapeInstanceCreatedSubscription = this.subscribe(
GameLib.Event.SHAPE_INSTANCE_CREATED,
this.shapeInstanceCreated
);
};
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);
/**
* Link Parent Meshes
* Link Parent Meshes for other meshes
*/
meshes.map(
function(mesh) {
@ -443,6 +449,34 @@ GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
}.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) {
@ -709,5 +743,6 @@ GameLib.System.Linking.prototype.stop = function() {
this.meshDeletedSubscription.remove();
this.imageChangedSubscription.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);
}
runtimeComponent.parentEntity = parentEntity;
if (parentEntity !== null) {
runtimeComponent.parentEntity = parentEntity;
}
}
if (runtimeComponent instanceof GameLib.D3.Image) {