cubeModel.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. this.initialize = function(){
  2. if(Object.getPrototypeOf(this).id == 'cubeModel.vwf'){
  3. console.log("Initialize of child..", this.id);
  4. this.getCubelets();
  5. this.twistLoop();
  6. // this.cubeModel = new Cube(this.id);
  7. // this.cubeModel.nodeID = this.id;
  8. } else {
  9. console.log("Initialize proto..", this.id);
  10. }
  11. }
  12. this.cubeID_set = function(value){
  13. this.cubeID = value;
  14. //this.initializeCubelets();
  15. }
  16. this.cubeID_get = function(){
  17. // if(!this.cubeModel){
  18. // this.cubeModel = new Cube(value);
  19. // this.cubeModel.nodeID = this.id;
  20. // }
  21. return this.cubeID
  22. }
  23. this.initializeCubelets = function(){
  24. let that = this;
  25. let cubeModelID = this.getCubeModelID();
  26. let cubelets = this.getCubelets();
  27. cubelets.forEach(el=>{
  28. let distance = 1.1;
  29. let position = [el.addressX*distance, el.addressY*distance, el.addressZ*distance];
  30. let cubeletID = el.id;
  31. let cubelet = {
  32. "extends": "cubeletModel.vwf",
  33. "properties":{
  34. "cubeletID": cubeletID,
  35. "cubeID": cubeModelID,
  36. "cubeNodeID": this.id,
  37. "size": 1
  38. },
  39. "children":{
  40. "wrapper":{
  41. "extends": "proxy/aframe/aentity.vwf",
  42. "properties":{
  43. "position": position
  44. }
  45. },
  46. "interpolation":
  47. {
  48. "extends": "proxy/aframe/interpolation-component.vwf",
  49. "type": "component",
  50. "properties": {
  51. "enabled": true
  52. }
  53. }
  54. }
  55. }
  56. this.cubelets.children.create(cubeletID, cubelet, function(child){
  57. that.setCubeletID(cubeletID, child.id);
  58. child.initCubeletFaces();
  59. })
  60. })
  61. }
  62. this.addGUI = function(){
  63. let half = 2;
  64. let faces = {
  65. "front":{
  66. "rotation": [0,0,0],
  67. "position": [0,0,half]
  68. },
  69. "up":{
  70. "rotation": [-90,0,0],
  71. "position": [0,half,0]
  72. },
  73. "right":{
  74. "rotation": [0,90,0],
  75. "position": [half,0,0]
  76. },
  77. "down":{
  78. "rotation": [90,0,0],
  79. "position": [0,-half,0]
  80. },
  81. "left":{
  82. "rotation": [0,-90,0],
  83. "position": [-half,0,0]
  84. },
  85. "back":{
  86. "rotation": [0,180,0],
  87. "position": [0,0,-half]
  88. }
  89. }
  90. let guiSize = 1.2;
  91. let split = 0.2;
  92. let height = 0.6
  93. Object.keys(faces).forEach(key=>{
  94. let data = faces[key];
  95. let buttosnGUI = {}
  96. let buttons = [
  97. {
  98. "command": key.toUpperCase(),
  99. "position":[split, 0, 0]
  100. },
  101. {
  102. "command": key,
  103. "position":[-split, 0, 0]
  104. }
  105. ]
  106. buttons.forEach((b,i) => {
  107. let rotateButton = {
  108. "extends": "proxy/aframe/aplane.vwf",
  109. "properties": {
  110. "displayName": b["command"] + '-button-' + i,
  111. //"radius": 0.15,
  112. "height": height,
  113. "width": 0.4,
  114. "position": b["position"],
  115. "class": "clickable intersectable"
  116. },
  117. "methods": {
  118. "intersectEventMethod": {
  119. "body": `
  120. console.log('I was intersected at point: ', point);//
  121. this.material.opacity = 0.6;
  122. this.material.color = 'green'
  123. `,
  124. "parameters": [
  125. "point"
  126. ],
  127. "type": "application/javascript"
  128. },
  129. "clearIntersectEventMethod": {
  130. "body": `
  131. console.log('Clear intersection');
  132. this.material.opacity = 0.3;
  133. this.material.color = 'white'
  134. `,
  135. "type": "application/javascript"
  136. },
  137. "mousedownAction":{
  138. "body": `
  139. this.triggerdownAction();
  140. `,
  141. "type": "application/javascript"
  142. },
  143. "mouseupAction":{
  144. "body": `
  145. this.triggerupAction();
  146. `,
  147. "type": "application/javascript"
  148. },
  149. "triggerdownAction":{
  150. "body": `
  151. this.material.color = 'blue';
  152. let command = this.displayName.charAt(0);
  153. this.parent.parent.parent.parent.do(command);
  154. `,
  155. "type": "application/javascript"
  156. },
  157. "triggerupAction":{
  158. "body": `
  159. this.material.color = 'green';
  160. `,
  161. "type": "application/javascript"
  162. }
  163. },
  164. "children":{
  165. "cursor-listener": {
  166. "extends": "proxy/aframe/app-cursor-listener-component.vwf",
  167. "type": "component"
  168. },
  169. "raycaster-listener": {
  170. "extends": "proxy/aframe/app-raycaster-listener-component.vwf",
  171. "type": "component"
  172. },
  173. "material": {
  174. "extends": "proxy/aframe/aMaterialComponent.vwf",
  175. "type": "component",
  176. "properties": {
  177. "transprent": true,
  178. "opacity": 0.3,
  179. "color": "white"
  180. }
  181. }
  182. }
  183. }
  184. buttosnGUI[i] = rotateButton
  185. })
  186. let face = //getFace(faces[key]);
  187. {
  188. "extends": "proxy/aframe/aentity.vwf",
  189. "properties": {
  190. "displayName": key,
  191. "rotation": data.rotation,
  192. "position": data.position,
  193. "width": 1,
  194. "height": 1
  195. },
  196. "children":{
  197. "rotateButtons":{
  198. "extends": "proxy/aframe/aentity.vwf",
  199. "children": buttosnGUI
  200. },
  201. "label":{
  202. "extends": "proxy/aframe/atext.vwf",
  203. "properties": {
  204. "displayName": key,
  205. "color": "black",
  206. "value": key,
  207. "side": "double",
  208. "opacity": 0.6,
  209. "position": [-0.3,0,0.01]
  210. }
  211. }
  212. // "material": {
  213. // "extends": "proxy/aframe/aMaterialComponent.vwf",
  214. // "type": "component",
  215. // "properties": {
  216. // "side": "double",
  217. // "transprent": true,
  218. // "opacity": 0.6,
  219. // "color": "white"
  220. // }
  221. // }
  222. }
  223. }
  224. //let color = cubeletModel[key].color.hex;
  225. //face.children.material.properties.color = color;
  226. this.gui.children.create(key, face)
  227. })
  228. }
  229. this.addTwistKey = function(key){
  230. this.twistQueue.push(key);
  231. this.cubeModel.twistQueue.add( key )
  232. }
  233. //cube.twist( cube.twistQueue.do() )
  234. this.rotateFront = function(){
  235. debugger;
  236. this.twistQueue.push('f');
  237. this.cubeModel.twistQueue.add('f');
  238. this.cubeModel.twist( this.cubeModel.twistQueue.do() );
  239. }
  240. this.do = function(key){
  241. this.twistQueue = this.twistQueue.concat([key]);
  242. this.twistQueueHistory = this.twistQueueHistory.concat([key]);
  243. this.twistAction(key);
  244. // this.cubeModel.twistQueue.add(key);
  245. // this.cubeModel.twist( this.cubeModel.twistQueue.do() );
  246. }
  247. this.twistLoop = function(){
  248. this.future(1).twistLoop();
  249. let isCubeTweening = this.isCubeTweening();
  250. //console.log(this.isCubeTweening());
  251. if(!isCubeTweening){
  252. this.progressQueue();
  253. }
  254. }
  255. this.getRobot = function(){
  256. return this.getScene()[this.robotID]
  257. }
  258. this.doButtonTriggerdownAction = function(buttonID){
  259. let button = this.getScene().findNodeByID(buttonID);
  260. let buttonName = button.displayName;
  261. if(buttonName == "noRobotButton"){
  262. console.log("SWITCH NO ROBOT!");
  263. this.withRobot = this.withRobot ? false : true;
  264. if(this.withRobot){
  265. button.baseColor = 'yellow';
  266. } else {
  267. button.baseColor = 'red';
  268. }
  269. }
  270. if(buttonName == "editCubeButton"){
  271. console.log("EDIT CUBE!");
  272. this.editCube = this.editCube ? false : true;
  273. this.editRubik();
  274. }
  275. if(buttonName == "currentCubeButton"){
  276. console.log("MAKE CURRENT CUBE!");
  277. this.getScene().currentCube = this.displayName;
  278. }
  279. }
  280. this.editRubik = function(){
  281. let clientThatCallThis = vwf.client();
  282. let me = vwf.moniker();
  283. if(clientThatCallThis == me) {
  284. console.log('EDIT CUBE!!');
  285. let viewNode = document.querySelector("[id='" + this.id + "']");
  286. let gizmo = viewNode.getAttribute('gizmo');
  287. if(!gizmo && this.editCube){
  288. viewNode.setAttribute('gizmo', {mode: "rotate"});
  289. let inter = viewNode.getAttribute('interpolation');
  290. if (inter) {
  291. viewNode.components.interpolation.node.viewEdit = true;
  292. }
  293. }
  294. if(gizmo && !this.editCube){
  295. viewNode.removeAttribute('gizmo');
  296. let inter = viewNode.getAttribute('interpolation');
  297. if (inter) {
  298. viewNode.components.interpolation.node.viewEdit = false;
  299. }
  300. }
  301. }
  302. // if(!this.gizmo && this.editCube){
  303. // let gizmoNode =
  304. // {
  305. // "extends": "proxy/aframe/gizmoComponent.vwf",
  306. // "type": "component",
  307. // "properties":
  308. // {
  309. // "mode": "rotate"
  310. // }
  311. // }
  312. // this.children.create("gizmo", gizmoNode);
  313. // }
  314. // if(this.gizmo && !this.editCube){
  315. // this.children.delete(this.gizmo)
  316. // }
  317. }