cubeletModel.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. this.initialize = function(){}
  2. this.getCube = function(){
  3. return this.parent.parent
  4. }
  5. this.initCubeletFaces = function(){
  6. let cube = this.getCube();
  7. let cubeletModel = cube.getCubelet(this.cubeletID);
  8. //let cubeModel = cube.cubeModel;
  9. //let cubeletModel = cubeModel.cubelets[this.cubleteID];
  10. //cubeletModel.nodeID = this.id;
  11. let half = this.size / 2;
  12. //MISTAKE in UP/DOWN position (in original code)
  13. let faces = {
  14. "front":{
  15. "faceID": "0",
  16. "rotation": [0,0,0],
  17. "position": [0,0,half]
  18. },
  19. "up":{
  20. "faceID": "1",
  21. "rotation": [-90,0,0],
  22. "position": [0,half,0]
  23. },
  24. "right":{
  25. "faceID": "2",
  26. "rotation": [0,90,0],
  27. "position": [half,0,0]
  28. },
  29. "down":{
  30. "faceID": "3",
  31. "rotation": [90,0,0],
  32. "position": [0,-half,0]
  33. },
  34. "left":{
  35. "faceID": "4",
  36. "rotation": [0,-90,0],
  37. "position": [-half,0,0]
  38. },
  39. "back":{
  40. "faceID": "5",
  41. "rotation": [0,180,0],
  42. "position": [0,0,-half]
  43. }
  44. }
  45. Object.keys(faces).forEach(key=>{
  46. let data = faces[key];
  47. let face = //getFace(faces[key]);
  48. {
  49. "extends": "proxy/aframe/aplane.vwf",
  50. "properties": {
  51. "faceID": data.faceID,
  52. "rotation": data.rotation,
  53. "position": data.position
  54. },
  55. "children":{
  56. "label":{
  57. "extends": "proxy/aframe/atext.vwf",
  58. "properties": {
  59. "displayName": key,
  60. "color": "black",
  61. "value": this.cubeletID,
  62. "side": "double",
  63. "position": [-0.3,0,0.01]
  64. }
  65. },
  66. "material": {
  67. "extends": "proxy/aframe/aMaterialComponent.vwf",
  68. "type": "component",
  69. "properties": {
  70. "side": "double",
  71. "color": cubeletModel.faces[data.faceID].color.hex
  72. }
  73. }
  74. }
  75. }
  76. //let color = cubeletModel[key].color.hex;
  77. //face.children.material.properties.color = color;
  78. this.wrapper.children.create(key, face)
  79. })
  80. }
  81. // this.rotateCublet = function(data){
  82. // // this.rotateBy([0,0,90],0.5);
  83. // // this.parent["1"].rotateBy([0,0,90],0.5);
  84. // // this.parent["2"].rotateBy([0,0,90],0.5);
  85. // // this.parent["3"].rotateBy([0,0,90],0.5);
  86. // // this.parent["4"].rotateBy([0,0,90],0.5);
  87. // this.rotation = data;
  88. // let k = this.getMatrix().clone();
  89. // let t = this.wrapper.getMatrix().clone();
  90. // t.premultiply(k);
  91. // console.log(t);
  92. // let p = new THREE.Vector3();
  93. // let q = new THREE.Quaternion();
  94. // let s = new THREE.Vector3();
  95. // t.decompose( p, q, s );
  96. // let angle = (new THREE.Euler()).setFromQuaternion(q, 'XYZ');
  97. // let rotation = (new THREE.Vector3(THREE.Math.radToDeg(angle.x),
  98. // THREE.Math.radToDeg(angle.y), THREE.Math.radToDeg(angle.z)));
  99. // console.log(p, rotation, s);
  100. // this.wrapper.position = p;
  101. // this.wrapper.rotation = rotation;
  102. // this.rotation = [0,0,0];
  103. // }
  104. this.rotateCubelet = function(rotation, speed, cubeCallback) {
  105. if(cubeCallback){
  106. this.rotateBy(rotation, speed, 'cubeletOnStopRotation:[' + this.cubeletID + ',"' + cubeCallback +'"]')
  107. } else {
  108. this.rotateBy(rotation, speed, "cubeletOnStopRotation:[" + this.cubeletID + "]" )
  109. }
  110. }
  111. this.cubeletOnStopRotation = function(cubeletID, cubeCallback){
  112. let cube = this.getCube();
  113. cube.cubeletsRemap(cubeletID, cubeCallback);
  114. if(cubeCallback){
  115. if(cube.robotID && cube.withRobot){
  116. let robot = cube.getRobot();
  117. robot.rotateFace(cubeCallback);
  118. }
  119. }
  120. }
  121. // this.cubeletOnStopRotation = function(cubeletID, cubeCallback){
  122. // debugger;
  123. // let threshold = 0.001
  124. // // Here's some complexity.
  125. // // We need to support partial rotations of arbitrary degrees
  126. // // yet ensure our internal model is always in a valid state.
  127. // // This means only remapping the Cubelet when it makes sense
  128. // // and also remapping the Cube if this Cubelet is allowed to do so.
  129. // let myCube = this.getCube();
  130. // let cube = myCube.cubeModel;
  131. // let cubelet = cube.cubelets[cubeletID];
  132. // var
  133. // xRemaps = cubelet.x.divide( 90 ).round()
  134. // .subtract( cubelet.xPrevious.divide( 90 ).round() )
  135. // .absolute(),
  136. // yRemaps = cubelet.y.divide( 90 ).round()
  137. // .subtract( cubelet.yPrevious.divide( 90 ).round() )
  138. // .absolute(),
  139. // zRemaps = cubelet.z.divide( 90 ).round()
  140. // .subtract( cubelet.zPrevious.divide( 90 ).round() )
  141. // .absolute()
  142. // if( Cube.verbosity >= 0.9 ){
  143. // console.log( 'Cublet #'+ ( cubelet.id < 10 ? '0'+ cubelet.id : cubelet.id ),
  144. // ' | xRemaps:', xRemaps, ' yRemaps:', yRemaps, ' zRemaps:', zRemaps,
  145. // ' | xPrev:', cubelet.xPrevious, ' x:', cubelet.x,
  146. // ' | yPrev:', cubelet.yPrevious, ' y:', cubelet.y,
  147. // ' | zPrev:', cubelet.zPrevious, ' z:', cubelet.z )
  148. // }
  149. // if( xRemaps ){
  150. // while( xRemaps -- ){
  151. // if( cubelet.x < cubelet.xPrevious ) cubelet.faces = [ cubelet.up, cubelet.back, cubelet.right, cubelet.front, cubelet.left, cubelet.down ]
  152. // else cubelet.faces = [ cubelet.down, cubelet.front, cubelet.right, cubelet.back, cubelet.left, cubelet.up ]
  153. // cubelet.map()
  154. // if( cubeCallback !== undefined ){
  155. // let swapMap = Cube.swapMaps[cubeCallback];
  156. // let swap = cubelet.cube.cubelets.slice();
  157. // swapMap.forEach(el=>{
  158. // cube.cubelets[el[0]] = swap[el[1]]
  159. // })
  160. // //cubeCallback( cubelet.cube.cubelets.slice())
  161. // cubelet.cube.map()
  162. // }
  163. // }
  164. // cubelet.xPrevious = cubelet.x
  165. // }
  166. // if( cubelet.x.modulo( 90 ).absolute() < threshold ){
  167. // cubelet.x = 0
  168. // cubelet.xPrevious = cubelet.x
  169. // cubelet.isEngagedX = false
  170. // }
  171. // if( yRemaps ){
  172. // while( yRemaps -- ){
  173. // if( cubelet.y < cubelet.yPrevious ) cubelet.faces = [ cubelet.left, cubelet.up, cubelet.front, cubelet.down, cubelet.back, cubelet.right ]
  174. // else cubelet.faces = [ cubelet.right, cubelet.up, cubelet.back, cubelet.down, cubelet.front, cubelet.left ]
  175. // cubelet.map()
  176. // if( cubeCallback !== undefined ){
  177. // let swapMap = Cube.swapMaps[cubeCallback];
  178. // let swap = cubelet.cube.cubelets.slice();
  179. // swapMap.forEach(el=>{
  180. // cube.cubelets[el[0]] = swap[el[1]]
  181. // })
  182. // //cubeCallback( cubelet.cube.cubelets.slice())
  183. // cubelet.cube.map()
  184. // }
  185. // }
  186. // cubelet.yPrevious = cubelet.y
  187. // }
  188. // if( cubelet.y.modulo( 90 ).absolute() < threshold ){
  189. // cubelet.y = 0
  190. // cubelet.yPrevious = cubelet.y
  191. // cubelet.isEngagedY = false
  192. // }
  193. // if( zRemaps ){
  194. // while( zRemaps -- ){
  195. // if( cubelet.z < cubelet.zPrevious ) cubelet.faces = [ cubelet.front, cubelet.right, cubelet.down, cubelet.left, cubelet.up, cubelet.back ]
  196. // else cubelet.faces = [ cubelet.front, cubelet.left, cubelet.up, cubelet.right, cubelet.down, cubelet.back ]
  197. // cubelet.map()
  198. // if( cubeCallback !== undefined ){
  199. // //debugger;
  200. // let swapMap = Cube.swapMaps[cubeCallback];
  201. // let swap = cubelet.cube.cubelets.slice();
  202. // swapMap.forEach(el=>{
  203. // cube.cubelets[el[0]] = swap[el[1]]
  204. // })
  205. // //cubeCallback( cubelet.cube.cubelets.slice())
  206. // cubelet.cube.map()
  207. // }
  208. // }
  209. // cubelet.zPrevious = cubelet.z
  210. // }
  211. // if( cubelet.z.modulo( 90 ).absolute() < threshold ){
  212. // cubelet.z = 0
  213. // cubelet.zPrevious = cubelet.z
  214. // cubelet.isEngagedZ = false
  215. // }
  216. // // Phew! Now we can turn off the tweening flag.
  217. // cubelet.isTweening = false
  218. // }