| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- var o0 = require('o0');
- var o0Project = require('o0Project');
- var o0CC = require('o0CC');
- var o0Game = require('o0Game');
- cc.Class({
- extends: cc.Component,
- properties: {
- canvas: {
- default: null,
- type: cc.Canvas
- },
- camera: {
- default: null,
- type: cc.Camera
- },
- UICamera: {
- default: null,
- type: cc.Camera
- },
- gameScene:null,
- menuNode:null,
-
- DeviceMs:1,
- map:null,
- mapHit:null,
- mapRadius: 200,
- chart:null,
- TestButton:[],
- Filter:null,
- LastTime:new Date().getTime(),
- },
- // use this for initialization
- onLoad: function () {
- //cc.director.setAnimationInterval(1.0/30);
- //cc.game.setFrameRate(30);//模拟器不能正常显示
- var self = this;
- //cc.director.getPhysicsManager().enabled = true;
- //this.cocos.node.x;
- //node.parent = this.canvas.node;
- //this.label.node.parent = node;
- //this.label.node.x = 0;
- //this.label.node.y = 0;
- //this.label.string = this.text;
- //var Snake = require("Snake");
- //cc.log("oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo "+Snake);
- /*var snake = new cc.Node('snake');
- snake.parent = this.camera.node;
- snake.addComponent('Snake');/** */
- //newSnake(this.camera.node);
- this.menuNode = new cc.Node();
- this.node.addChild(this.menuNode,1);
-
- this.map = o0CC.addScriptNode(this.node,'cc.Graphics',10);
- this.mapHit = o0CC.addScriptNode(this.node,'cc.Graphics',10);
- this.chart = o0CC.addScriptNode(this.node,'cc.Graphics',10);
- var col = 1;
- for(var i = 0;i<col;++i){
- this.TestButton.push([]);
- for(var j = 0;j<2;++j){
- var testButton = o0CC.addScriptNode(this.menuNode,'o0CCButton',11);
- o0CC.setGroup(testButton,o0Game.GroupIndex.UI);
- testButton.node.x = (i-(col-1)/2)*this.canvas.node.width/col;
- testButton.node.y = this.canvas.node.height/2 - 20 - j * 40;
- testButton.node.width = this.canvas.node.width/col;
- testButton.node.height = 40;/** */
- testButton.name = "null";
- this.TestButton[i].push(testButton);
- }
- }
-
- /*
- this.gameScene = o0CC.addScriptNode(this.node,'GameScene',0);
- this.gameScene.canvas = this.canvas;
- this.gameScene.camera = this.camera;/** */
- this.backgroundGraphic = this.menuNode.addComponent(cc.Graphics,0);
- o0CC.setGroup(this.backgroundGraphic,o0Game.GroupIndex.UI);
- cc.director.getScheduler().setTimeScale(50);
- this.Filter = new o0Project.Filter();
- cc.systemEvent.setAccelerometerEnabled(true);
- cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
- if(typeof(uni) != "undefined" && typeof(window) != "undefined" ){
- uni.postMessage({
- data: {
- funName: "addDeviceUpdateListener",
- gameData: {}
-
- }
- });
- var printed = false;
- window.onWebViewMessage = function(data){
- let name= data.funName;
- if(name == "onDeviceUpdateData")
- {
- if(printed == false){
- console.log(data.gameData);
- printed = true;
- }
- //webView.onDeviceUpdateData(data.gameData);
- let {min, s, ms} = data.gameData.data;
- var msGap = ms - self.DeviceMs;
- self.DeviceMs = ms;
- while(msGap < 0){
- msGap+=1000;
- }
- //console.log(data);
- let {ax,ay,az} = data.gameData.data.acc;
- let {gx,gy,gz} = data.gameData.data.gyro;
- self.Filter.Update(new o0.Vector3(ax,ay,az),msGap,new o0.Vector3(gx,gy,gz));//我自己的更新acc的函数
- cc.systemEvent.setAccelerometerEnabled(false);
- }
- };
- uni.postMessage({
- data: {
- funName: "writeBLEConnectionValue",
- gameData: {
- value: "3" //开启设备数据
- }
- }
- });
- uni.postMessage({
- data: {
- funName: "writeBLEConnectionValue",
- gameData: {
- value: "b" //60ms
- }
- }
- })
- }
- console.log("init finished");
- },
- onDeviceMotionEvent (event) {
- let newTime = new Date().getTime();
- var [acc,forceChanged,momentum] = this.Filter.Update(new o0.Vector3(event.acc),newTime - this.LastTime,new o0.Vector3(0,0,0));
- this.LastTime = newTime;
- },
- // called every frame
- update: function (dt) {
- this.chart.node.x = this.canvas.node.width/2 - 100;
- this.chart.node.y = -this.canvas.node.height/2 + 50;
- this.chart.clear();
- for(var i = 0;i< this.Filter.frame.length;++i){
- var io = this.Filter.frame[i];
-
- this.chart.strokeColor = new cc.Color(255,255,255,255);
- this.chart.moveTo(-i, 0);
- this.chart.lineTo(-i, io.hit);
- this.chart.stroke();/* */
-
- this.chart.strokeColor = new cc.Color(255,255,255,50);
- this.chart.moveTo(-i, 200);
- this.chart.lineTo(-i, 300);
- this.chart.stroke();/** */
- this.chart.strokeColor = new cc.Color(255,0,0,255);
- this.chart.moveTo(-i, 200);
- this.chart.lineTo(-i, 200+io.reliable*100);
- this.chart.stroke();/** */
-
- this.chart.strokeColor = new cc.Color(0,255,0,255);
- this.chart.moveTo(-i, 300);
- this.chart.lineTo(-i, 300+io.shakeFixed);
- this.chart.stroke();/** */
- this.chart.strokeColor = new cc.Color(255,255,255,255);
- this.chart.moveTo(-i, 300);
- this.chart.lineTo(-i, 300+io.shake);
- this.chart.stroke();/* */
- this.chart.strokeColor = new cc.Color(255,0,0,255);
- this.chart.moveTo(-i, 300);
- this.chart.lineTo(-i, 300+io.shakeSlope);
- this.chart.stroke();/* */
-
- /*
- this.chart.strokeColor = new cc.Color(255,0,0,255);
- this.chart.moveTo(-i, 0);
- this.chart.lineTo(-i, io.slope);
- this.chart.stroke();/** */
- this.chart.strokeColor = new cc.Color(0,255,0,255);
- this.chart.moveTo(-i, 400);
- this.chart.lineTo(-i, 400+io.accFixed);
- this.chart.stroke();
- this.chart.strokeColor = new cc.Color(255,255,255,255);
- this.chart.moveTo(-i, 400);
- this.chart.lineTo(-i, 400+io.acc.length*100);
- this.chart.stroke();
-
- this.chart.strokeColor = new cc.Color(255,0,0,255);
- this.chart.moveTo(-i, 380);
- this.chart.lineTo(-i, 380+io.accSlope);
- this.chart.stroke();
- /*
- if(io.hit!=0){
- this.chart.strokeColor = new cc.Color(255,0,0,255);
- this.chart.moveTo(-i, 0);
- this.chart.lineTo(-i, io.hit);
- this.chart.stroke();
- }/** */
- }
- //console.log(this.Filter.frame[0].shake);
- this.map.node.x = this.canvas.node.width/2 - this.mapRadius - 10;
- this.map.node.y = this.canvas.node.height/2 - this.mapRadius - 10;
- this.map.clear();
- this.map.circle(0, 0, this.mapRadius);
- this.map.fillColor = new cc.Color(255,255,255,30);
- this.map.fill();
- this.mapHit.node.x = this.canvas.node.width/2 - this.mapRadius - 10;
- this.mapHit.node.y = -this.canvas.node.height/2 + this.mapRadius - 10;
- this.mapHit.clear();
- this.mapHit.circle(0, 0, this.mapRadius);
- this.mapHit.fillColor = new cc.Color(255,255,255,30);
- this.mapHit.fill();
- this.map.lineWidth = 1;
- var lastFrame = this.Filter.frame[this.Filter.frame.length-1];
- var newestTime = lastFrame.time;
- for(var i = this.Filter.frame.length-2;i>=0 && i>=this.Filter.frame.length-30;--i){
- var io = this.Filter.frame[i];
- var color = 255 - (newestTime - io.time) / 6;
-
- this.map.strokeColor = new cc.Color(255 - color,color,0,255);
- this.map.moveTo(lastFrame.acc.x * this.mapRadius, lastFrame.acc.y * this.mapRadius);
- this.map.lineTo(io.acc.x * this.mapRadius, io.acc.y * this.mapRadius);
- this.map.stroke();
-
- this.map.strokeColor = new cc.Color(color,color,color,255);
- this.map.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
- this.map.lineTo(io.pos.x * this.mapRadius, io.pos.y * this.mapRadius);
- this.map.stroke();
-
- this.map.strokeColor = new cc.Color(255,0,0,255);
- this.map.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
- this.map.lineTo(io.predict.x * this.mapRadius, io.predict.y * this.mapRadius);
- this.map.stroke();/* */
-
- this.map.strokeColor = new cc.Color(255,255,255,255);
- this.map.moveTo(lastFrame.gyr.x / 50 * this.mapRadius, lastFrame.gyr.y / 50 * this.mapRadius);
- this.map.lineTo(io.gyr.x / 50 * this.mapRadius, io.gyr.y / 50 * this.mapRadius);
- this.map.stroke();
-
- this.mapHit.strokeColor = new cc.Color(255 - color,color,0,255);
- this.mapHit.moveTo(lastFrame.speed.x * this.mapRadius, lastFrame.speed.y * this.mapRadius);
- this.mapHit.lineTo(io.speed.x * this.mapRadius, io.speed.y * this.mapRadius);
- this.mapHit.stroke();
- lastFrame = io;
- }
- if(this.Filter.frameHit.length > 0){
- var lastFrameHit = this.Filter.frameHit[this.Filter.frameHit.length-1];
- var firstFrameHit = this.Filter.frameHit[0];
- this.mapHit.lineWidth = 2;
- /*
- this.mapHit.strokeColor = new cc.Color(255,0,0,255);
- this.mapHit.moveTo(0,10);
- this.mapHit.lineTo(lastFrameHit.pos.x* this.mapRadius,10+ lastFrameHit.pos.y* this.mapRadius);
- this.mapHit.stroke();/** */
-
- var direction = new o0.Vector2(0,0);
- var directionDistance = 0;
- for(var fi = 1;fi<this.Filter.frameHit.length-1;++fi){
- for(var li = fi+1;li<this.Filter.frameHit.length;++li){
- let firstGyr = this.Filter.frameHit[fi].gyr;
- let lastGyr = this.Filter.frameHit[li].gyr;
- let newDirectionDistance = o0.distance2(firstGyr, lastGyr);
- if(directionDistance < newDirectionDistance){
- directionDistance = newDirectionDistance;
- direction = lastGyr.minus(firstGyr);
- }
- }
- }
- this.mapHit.strokeColor = new cc.Color(255,0,0,255);
- this.mapHit.moveTo(0,0);
- //this.mapHit.lineTo((lastFrameHit.gyr.x - firstFrameHit.gyr.x)*5,(lastFrameHit.gyr.y - firstFrameHit.gyr.y)*5);
- this.mapHit.lineTo(direction.x*5,direction.y*5);
- this.mapHit.stroke();/** */
-
- /*
- this.mapHit.strokeColor = new cc.Color(0,255,0,255);
- this.mapHit.moveTo(0,0);
- this.mapHit.lineTo(this.Filter.frameSwing.acc.x* this.mapRadius, this.Filter.frameSwing.acc.y* this.mapRadius);
- this.mapHit.stroke();/* */
- var newestTime = lastFrameHit.time;
- this.mapHit.lineWidth = 1;
- for(var i = this.Filter.frameHit.length-2;i>=0;--i){
- var io = this.Filter.frameHit[i];
-
- var color = 255 - (newestTime - io.time);
-
- /*
- this.mapHit.strokeColor = new cc.Color(color,color,color,255);
- this.mapHit.moveTo(lastFrameHit.pos.x * this.mapRadius, lastFrameHit.pos.y * this.mapRadius);
- this.mapHit.lineTo(io.pos.x * this.mapRadius, io.pos.y * this.mapRadius);
- this.mapHit.stroke();/* */
- this.mapHit.strokeColor = new cc.Color(color,color,color,255);
- this.mapHit.moveTo((lastFrameHit.gyr.x - firstFrameHit.gyr.x)*5,(lastFrameHit.gyr.y - firstFrameHit.gyr.y)*5);
- this.mapHit.lineTo((io.gyr.x - firstFrameHit.gyr.x)*5,(io.gyr.y - firstFrameHit.gyr.y)*5);
- this.mapHit.stroke();
-
- /*
- this.mapHit.strokeColor = new cc.Color(255,0,0,255);
- this.mapHit.moveTo(lastFrame.pos.x * this.mapRadius, lastFrame.pos.y * this.mapRadius);
- this.mapHit.lineTo(io.predict.x * this.mapRadius, io.predict.y * this.mapRadius);
- this.mapHit.stroke();/* */
- /*
- this.map.circle(lastFrame.pos.x * this.mapRadius, lastFrame.pos.z * this.mapRadius, 1);
- this.map.fillColor = new cc.Color(255,0,0,255);
- this.map.fill();/* */
-
- lastFrameHit = io;
- }
-
- }
- this.TestButton[0][0].name = "power";
- for(var i = this.Filter.frame.length-1;i>=0;--i){
- var io = this.Filter.frame[i];
- if(io.hit != 0){
- this.TestButton[0][1].name = io.hit;
- break;
- }
- }
- /*
- this.TestButton[1][12].name = this.Filter.angle;
- this.TestButton[3][12].name = this.Filter.punchCount;
- this.TestButton[2][12].name = this.Filter.leftRight;
- for(var i =0;i<this.TestButton.length;++i){
- for(var j =10;j>=0;--j){
- this.TestButton[i][j+1].name = this.TestButton[i][j].name;
- this.TestButton[i][j+1].button.normalColor = this.TestButton[i][j].button.normalColor;
- }
- }/* */
- }
- });
|