Newer
Older
/**
* Created by fab on 3/16/15.
*/
function Machine() {
this._buildMeshes(function(meshes){
self.meshes = meshes;
_.each(meshes, function(mesh){
dmaGlobals.three.sceneAdd(mesh);
});
this.cell = this._makeStockCell();
Machine.prototype.setVisibility = function(visible){
if (visible == null || visible === undefined) visible = dmaGlobals.assembler.isVisible();
if (visible && this.hasStock) this.cell.draw();
else this.cell.hide();
this._setMeshesVisiblity(visible);
};
Machine.prototype._setMeshesVisiblity = function(visible){
_.each(this.meshes, function(mesh){
mesh.visible = visible;
});
};
Machine.prototype._makeStockCell = function(){
return dmaGlobals.lattice.makeCellForLatticeType(null, dmaGlobals.lattice.get("scale"));
};
Machine.prototype.updateCellType = function(){
if (this.cell) this.cell.destroy();
this.cell = this._makeStockCell();
};
Machine.prototype.updatePartType = function(){
this.cell.destroyParts();
this.cell.draw();
Machine.prototype.pickUpStock = function(){
this.hasStock = true;
this.cell.draw();
};
Machine.prototype.releaseStock = function(index){
this.hasStock = false;
dmaGlobals.lattice.showCellAtIndex(JSON.parse(index));
this.cell.hide();
};
Machine.prototype._animateMesh = function(mesh, axis, speed, target, callback){
dmaGlobals.three.startAnimationLoop();
var increment = (target-mesh.position[axis])/10;
this._incrementalMove(mesh, axis, increment, mesh.position[axis]+increment*10, callback);
};
Machine.prototype._incrementalMove = function(mesh, axis, increment, target, callback){
var self = this;
setTimeout(function(){
if (mesh.position[axis] == target) {
if (callback) callback();
return;
}
mesh.position[axis] += increment;
console.log(mesh.position[axis]);
self._incrementalMove(mesh, axis, increment, target, callback)
},100);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Machine.prototype.destroy = function(){
this.cell.destroy();
_.each(this.meshes, function(mesh){
dmaGlobals.three.sceneRemove(mesh);
mesh = null;
});
this.meshes = null;
};
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////SHOPBOT///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function Shopbot(){
Machine.call(this);
}
Shopbot.prototype = Object.create(Machine.prototype);
Shopbot.prototype._buildMeshes = function(callback){
var meshes = [];
(new THREE.STLLoader()).load("data/shopbotEndEffector.stl", function(geometry){
geometry.computeBoundingBox();
var unitScale = 1.5/geometry.boundingBox.max.y;
geometry.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0,0, Math.sqrt(2)/2));
var mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({color:0xaaaaaa, shading: THREE.FlatShading}));
mesh.visible = false;
meshes.push(mesh);
callback(meshes);
});
};
Shopbot.prototype.moveTo = function(x, y, z, speed, wcs, callback){
var endEffector = this.meshes[0];
var totalThreads = 3;
function sketchyCallback(){
totalThreads -= 1;
if (totalThreads > 0) return;
}
this._moveAxis(endEffector, x, "x", speed.xy, wcs, sketchyCallback);
this._moveAxis(endEffector, y, "y", speed.xy, wcs, sketchyCallback);
this._moveAxis(endEffector, z, "z", speed.z, wcs, sketchyCallback);
this.cell.updateForScale();//todo why is this here?
};
Shopbot.prototype._moveAxis = function(mesh, target, axis, speed, wcs, callback){
if (target != "") {
target = parseFloat(target)+wcs[axis];
this._animateMesh(mesh, axis, speed, target, callback);
this._animateMesh(this.cell.cellMesh, axis, speed, target, null);//todo reaching a little deep here, might want to find a better solution
} else callback();
};
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////ONE BIT///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function OneBitBot(){
Machine.call(this);
}
OneBitBot.prototype = Object.create(Machine.prototype);