Menu.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. var o0 = require('o0');
  2. var o0Project = require('o0Project');
  3. var o0CC = require('o0CC');
  4. var o0Game = require('o0Game');
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. canvas: {
  9. default: null,
  10. type: cc.Canvas
  11. },
  12. camera: {
  13. default: null,
  14. type: cc.Camera
  15. },
  16. UICamera: {
  17. default: null,
  18. type: cc.Camera
  19. },
  20. gameScene:null,
  21. menuNode:null,
  22. DeviceMs:1,
  23. map:null,
  24. mapHit:null,
  25. mapRadius: 200,
  26. chart:null,
  27. TestButton:[],
  28. Filter:null,
  29. LastTime:new Date().getTime(),
  30. },
  31. // use this for initialization
  32. onLoad: function () {
  33. //cc.director.setAnimationInterval(1.0/30);
  34. //cc.game.setFrameRate(30);//模拟器不能正常显示
  35. var self = this;
  36. //cc.director.getPhysicsManager().enabled = true;
  37. //this.cocos.node.x;
  38. //node.parent = this.canvas.node;
  39. //this.label.node.parent = node;
  40. //this.label.node.x = 0;
  41. //this.label.node.y = 0;
  42. //this.label.string = this.text;
  43. //var Snake = require("Snake");
  44. //cc.log("oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo "+Snake);
  45. /*var snake = new cc.Node('snake');
  46. snake.parent = this.camera.node;
  47. snake.addComponent('Snake');/** */
  48. //newSnake(this.camera.node);
  49. this.menuNode = new cc.Node();
  50. this.node.addChild(this.menuNode,1);
  51. this.map = o0CC.addScriptNode(this.node,'cc.Graphics',10);
  52. this.mapHit = o0CC.addScriptNode(this.node,'cc.Graphics',10);
  53. this.chart = o0CC.addScriptNode(this.node,'cc.Graphics',10);
  54. var col = 1;
  55. for(var i = 0;i<col;++i){
  56. this.TestButton.push([]);
  57. for(var j = 0;j<2;++j){
  58. var testButton = o0CC.addScriptNode(this.menuNode,'o0CCButton',11);
  59. o0CC.setGroup(testButton,o0Game.GroupIndex.UI);
  60. testButton.node.x = (i-(col-1)/2)*this.canvas.node.width/col;
  61. testButton.node.y = this.canvas.node.height/2 - 20 - j * 40;
  62. testButton.node.width = this.canvas.node.width/col;
  63. testButton.node.height = 40;/** */
  64. testButton.name = "null";
  65. this.TestButton[i].push(testButton);
  66. }
  67. }
  68. /*
  69. this.gameScene = o0CC.addScriptNode(this.node,'GameScene',0);
  70. this.gameScene.canvas = this.canvas;
  71. this.gameScene.camera = this.camera;/** */
  72. this.backgroundGraphic = this.menuNode.addComponent(cc.Graphics,0);
  73. o0CC.setGroup(this.backgroundGraphic,o0Game.GroupIndex.UI);
  74. cc.director.getScheduler().setTimeScale(50);
  75. this.Filter = new o0Project.Filter();
  76. cc.systemEvent.setAccelerometerEnabled(true);
  77. cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
  78. if(typeof(uni) != "undefined" && typeof(window) != "undefined" ){
  79. uni.postMessage({
  80. data: {
  81. funName: "addDeviceUpdateListener",
  82. gameData: {}
  83. }
  84. });
  85. var printed = false;
  86. window.onWebViewMessage = function(data){
  87. let name= data.funName;
  88.    if(name == "onDeviceUpdateData")
  89.     {
  90. if(printed == false){
  91. console.log(data.gameData);
  92. printed = true;
  93. }
  94.         //webView.onDeviceUpdateData(data.gameData);
  95. let {min, s, ms} = data.gameData.data;
  96. var msGap = ms - self.DeviceMs;
  97. self.DeviceMs = ms;
  98. while(msGap < 0){
  99. msGap+=1000;
  100. }
  101. //console.log(data);
  102. let {ax,ay,az} = data.gameData.data.acc;
  103. let {gx,gy,gz} = data.gameData.data.gyro;
  104. self.Filter.Update(new o0.Vector3(ax,ay,az),msGap,new o0.Vector3(gx,gy,gz));//我自己的更新acc的函数
  105. cc.systemEvent.setAccelerometerEnabled(false);
  106.     }
  107. };
  108. uni.postMessage({
  109. data: {
  110. funName: "writeBLEConnectionValue",
  111. gameData: {
  112. value: "3" //开启设备数据
  113. }
  114. }
  115. });
  116. uni.postMessage({
  117. data: {
  118. funName: "writeBLEConnectionValue",
  119. gameData: {
  120. value: "b" //60ms
  121. }
  122. }
  123. })
  124. }
  125. console.log("init finished");
  126. },
  127. onDeviceMotionEvent (event) {
  128. let newTime = new Date().getTime();
  129. var [acc,forceChanged,momentum] = this.Filter.Update(new o0.Vector3(event.acc),newTime - this.LastTime,new o0.Vector3(0,0,0));
  130. this.LastTime = newTime;
  131. },
  132. // called every frame
  133. update: function (dt) {
  134. this.chart.node.x = this.canvas.node.width/2 - 100;
  135. this.chart.node.y = -this.canvas.node.height/2 + 50;
  136. this.chart.clear();
  137. for(var i = 0;i< this.Filter.frame.length;++i){
  138. var io = this.Filter.frame[i];
  139. this.chart.strokeColor = new cc.Color(255,255,255,255);
  140. this.chart.moveTo(-i, 0);
  141. this.chart.lineTo(-i, io.hit);
  142. this.chart.stroke();/* */
  143. this.chart.strokeColor = new cc.Color(255,255,255,50);
  144. this.chart.moveTo(-i, 200);
  145. this.chart.lineTo(-i, 300);
  146. this.chart.stroke();/** */
  147. this.chart.strokeColor = new cc.Color(255,0,0,255);
  148. this.chart.moveTo(-i, 200);
  149. this.chart.lineTo(-i, 200+io.reliable*100);
  150. this.chart.stroke();/** */
  151. this.chart.strokeColor = new cc.Color(0,255,0,255);
  152. this.chart.moveTo(-i, 300);
  153. this.chart.lineTo(-i, 300+io.shakeFixed);
  154. this.chart.stroke();/** */
  155. this.chart.strokeColor = new cc.Color(255,255,255,255);
  156. this.chart.moveTo(-i, 300);
  157. this.chart.lineTo(-i, 300+io.shake);
  158. this.chart.stroke();/* */
  159. this.chart.strokeColor = new cc.Color(255,0,0,255);
  160. this.chart.moveTo(-i, 300);
  161. this.chart.lineTo(-i, 300+io.shakeSlope);
  162. this.chart.stroke();/* */
  163. /*
  164. this.chart.strokeColor = new cc.Color(255,0,0,255);
  165. this.chart.moveTo(-i, 0);
  166. this.chart.lineTo(-i, io.slope);
  167. this.chart.stroke();/** */
  168. this.chart.strokeColor = new cc.Color(0,255,0,255);
  169. this.chart.moveTo(-i, 400);
  170. this.chart.lineTo(-i, 400+io.accFixed);
  171. this.chart.stroke();
  172. this.chart.strokeColor = new cc.Color(255,255,255,255);
  173. this.chart.moveTo(-i, 400);
  174. this.chart.lineTo(-i, 400+io.acc.length*100);
  175. this.chart.stroke();
  176. this.chart.strokeColor = new cc.Color(255,0,0,255);
  177. this.chart.moveTo(-i, 380);
  178. this.chart.lineTo(-i, 380+io.accSlope);
  179. this.chart.stroke();
  180. /*
  181. if(io.hit!=0){
  182. this.chart.strokeColor = new cc.Color(255,0,0,255);
  183. this.chart.moveTo(-i, 0);
  184. this.chart.lineTo(-i, io.hit);
  185. this.chart.stroke();
  186. }/** */
  187. }
  188. //console.log(this.Filter.frame[0].shake);
  189. this.map.node.x = this.canvas.node.width/2 - this.mapRadius - 10;
  190. this.map.node.y = this.canvas.node.height/2 - this.mapRadius - 10;
  191. this.map.clear();
  192. this.map.circle(0, 0, this.mapRadius);
  193. this.map.fillColor = new cc.Color(255,255,255,30);
  194. this.map.fill();
  195. this.mapHit.node.x = this.canvas.node.width/2 - this.mapRadius - 10;
  196. this.mapHit.node.y = -this.canvas.node.height/2 + this.mapRadius - 10;
  197. this.mapHit.clear();
  198. this.mapHit.circle(0, 0, this.mapRadius);
  199. this.mapHit.fillColor = new cc.Color(255,255,255,30);
  200. this.mapHit.fill();
  201. this.map.lineWidth = 1;
  202. var lastFrame = this.Filter.frame[this.Filter.frame.length-1];
  203. var newestTime = lastFrame.time;
  204. for(var i = this.Filter.frame.length-2;i>=0 && i>=this.Filter.frame.length-30;--i){
  205. var io = this.Filter.frame[i];
  206. var color = 255 - (newestTime - io.time) / 6;
  207. this.map.strokeColor = new cc.Color(255 - color,color,0,255);
  208. this.map.moveTo(lastFrame.acc.x * this.mapRadius, lastFrame.acc.y * this.mapRadius);
  209. this.map.lineTo(io.acc.x * this.mapRadius, io.acc.y * this.mapRadius);
  210. this.map.stroke();
  211. this.map.strokeColor = new cc.Color(color,color,color,255);
  212. this.map.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
  213. this.map.lineTo(io.pos.x * this.mapRadius, io.pos.y * this.mapRadius);
  214. this.map.stroke();
  215. this.map.strokeColor = new cc.Color(255,0,0,255);
  216. this.map.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
  217. this.map.lineTo(io.predict.x * this.mapRadius, io.predict.y * this.mapRadius);
  218. this.map.stroke();/* */
  219. this.map.strokeColor = new cc.Color(255,255,255,255);
  220. this.map.moveTo(lastFrame.gyr.x / 50 * this.mapRadius, lastFrame.gyr.y / 50 * this.mapRadius);
  221. this.map.lineTo(io.gyr.x / 50 * this.mapRadius, io.gyr.y / 50 * this.mapRadius);
  222. this.map.stroke();
  223. this.mapHit.strokeColor = new cc.Color(255 - color,color,0,255);
  224. this.mapHit.moveTo(lastFrame.speed.x * this.mapRadius, lastFrame.speed.y * this.mapRadius);
  225. this.mapHit.lineTo(io.speed.x * this.mapRadius, io.speed.y * this.mapRadius);
  226. this.mapHit.stroke();
  227. lastFrame = io;
  228. }
  229. if(this.Filter.frameHit.length > 0){
  230. var lastFrameHit = this.Filter.frameHit[this.Filter.frameHit.length-1];
  231. var firstFrameHit = this.Filter.frameHit[0];
  232. this.mapHit.lineWidth = 2;
  233. /*
  234. this.mapHit.strokeColor = new cc.Color(255,0,0,255);
  235. this.mapHit.moveTo(0,10);
  236. this.mapHit.lineTo(lastFrameHit.pos.x* this.mapRadius,10+ lastFrameHit.pos.y* this.mapRadius);
  237. this.mapHit.stroke();/** */
  238. var direction = new o0.Vector2(0,0);
  239. var directionDistance = 0;
  240. for(var fi = 1;fi<this.Filter.frameHit.length-1;++fi){
  241. for(var li = fi+1;li<this.Filter.frameHit.length;++li){
  242. let firstGyr = this.Filter.frameHit[fi].gyr;
  243. let lastGyr = this.Filter.frameHit[li].gyr;
  244. let newDirectionDistance = o0.distance2(firstGyr, lastGyr);
  245. if(directionDistance < newDirectionDistance){
  246. directionDistance = newDirectionDistance;
  247. direction = lastGyr.minus(firstGyr);
  248. }
  249. }
  250. }
  251. this.mapHit.strokeColor = new cc.Color(255,0,0,255);
  252. this.mapHit.moveTo(0,0);
  253. //this.mapHit.lineTo((lastFrameHit.gyr.x - firstFrameHit.gyr.x)*5,(lastFrameHit.gyr.y - firstFrameHit.gyr.y)*5);
  254. this.mapHit.lineTo(direction.x*5,direction.y*5);
  255. this.mapHit.stroke();/** */
  256. /*
  257. this.mapHit.strokeColor = new cc.Color(0,255,0,255);
  258. this.mapHit.moveTo(0,0);
  259. this.mapHit.lineTo(this.Filter.frameSwing.acc.x* this.mapRadius, this.Filter.frameSwing.acc.y* this.mapRadius);
  260. this.mapHit.stroke();/* */
  261. var newestTime = lastFrameHit.time;
  262. this.mapHit.lineWidth = 1;
  263. for(var i = this.Filter.frameHit.length-2;i>=0;--i){
  264. var io = this.Filter.frameHit[i];
  265. var color = 255 - (newestTime - io.time);
  266. /*
  267. this.mapHit.strokeColor = new cc.Color(color,color,color,255);
  268. this.mapHit.moveTo(lastFrameHit.pos.x * this.mapRadius, lastFrameHit.pos.y * this.mapRadius);
  269. this.mapHit.lineTo(io.pos.x * this.mapRadius, io.pos.y * this.mapRadius);
  270. this.mapHit.stroke();/* */
  271. this.mapHit.strokeColor = new cc.Color(color,color,color,255);
  272. this.mapHit.moveTo((lastFrameHit.gyr.x - firstFrameHit.gyr.x)*5,(lastFrameHit.gyr.y - firstFrameHit.gyr.y)*5);
  273. this.mapHit.lineTo((io.gyr.x - firstFrameHit.gyr.x)*5,(io.gyr.y - firstFrameHit.gyr.y)*5);
  274. this.mapHit.stroke();
  275. /*
  276. this.mapHit.strokeColor = new cc.Color(255,0,0,255);
  277. this.mapHit.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
  278. this.mapHit.lineTo(io.predict.x * this.mapRadius, io.predict.y * this.mapRadius);
  279. this.mapHit.stroke();/* */
  280. /*
  281. this.map.circle(lastFrame.pos.x * this.mapRadius, lastFrame.pos.z * this.mapRadius, 1);
  282. this.map.fillColor = new cc.Color(255,0,0,255);
  283. this.map.fill();/* */
  284. lastFrameHit = io;
  285. }
  286. }
  287. this.TestButton[0][0].name = "power";
  288. for(var i = this.Filter.frame.length-1;i>=0;--i){
  289. var io = this.Filter.frame[i];
  290. if(io.hit != 0){
  291. this.TestButton[0][1].name = io.hit;
  292. break;
  293. }
  294. }
  295. /*
  296. this.TestButton[1][12].name = this.Filter.angle;
  297. this.TestButton[3][12].name = this.Filter.punchCount;
  298. this.TestButton[2][12].name = this.Filter.leftRight;
  299. for(var i =0;i<this.TestButton.length;++i){
  300. for(var j =10;j>=0;--j){
  301. this.TestButton[i][j+1].name = this.TestButton[i][j].name;
  302. this.TestButton[i][j+1].button.normalColor = this.TestButton[i][j].button.normalColor;
  303. }
  304. }/* */
  305. }
  306. });