var o0 = require('o0'); module.exports = { SandbagAlgorithm: class{ constructor() { this.stableAcc = new o0.Vector2(0,0); this.stableGyr = new o0.Vector2(0,0); this.stableCount = 0; this.stableCountMax = 3000; this.frameCapacity = 6; this.frame = []; this.frameLength = 5; this.frameOffset = 0; this.frameHitCapacity = 11; this.frameHit = [];//打击后的n帧 for(var i = 0;i newFrame.shakeFixed){ newFrame.shakeFixed = newFrame.shake; }/* */ lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed,last2Frame.shakeFixed), Math.min(newFrame.shakeFixed,last3Frame.shakeFixed)); //////////////////////////////////////////////////////////////// newFrame.shakeSlope = Math.max(newFrame.shakeFixed-lastFrame.shakeFixed,0); /////////////////////////////////////////////////////////////// var direction = new o0.Vector2(0,0); if(lastFrame.hit==0 && last2Frame.hit==0 && last3Frame.hit==0 && last4Frame.hit==0 && last5Frame.hit==0 && (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20) && (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)){ newFrame.hit = 1; if (this.frameHit.length < this.frameHitCapacity) { direction = this.GetDirection(); }/**/ this.frameHit = []; var o = new Object(); o.time = 0; o.gyr = lastFrame.gyr; this.frameHit.push(o); }else{ newFrame.hit = 0; } if (this.frameHit.length < this.frameHitCapacity) { var o = new Object(); o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap; o.gyr = newFrame.gyr; if (this.frameHit.length == this.frameHitCapacity) { direction = this.GetDirection(); } } if ((this.frameOffset+=1) >= this.frameCapacity){ this.frameOffset -= this.frameCapacity; } if(newFrame.hit != 0){ console.log('hit:' + newFrame.hit); } if(direction.x != 0 || direction.y !=0){ console.log(JSON.stringify(newFrame.hit) +" == " + JSON.stringify(direction)); } return (newFrame.hit, direction); } test(){ return "123123131"; } getValue(hitValue, callback) { var direction = new o0.Vector2(0, 0); var directionDistance = 0; for (var fi = 1; fi < this.frameHit.length - 1; ++fi) { for (var li = fi + 1; li < this.frameHit.length; ++li) { let firstGyr = this.frameHit[fi].gyr; let lastGyr = this.frameHit[li].gyr; let newDirectionDistance = o0.distance2(firstGyr, lastGyr); if (directionDistance < newDirectionDistance) { directionDistance = newDirectionDistance; direction = lastGyr.minus(firstGyr); } } } let curAngle = new o0.Vector2(direction.x, direction.y).mod.angle(new o0.Vector2(0, 1)); let directionPunch = "all", name = "击中", ename = "hit"; let curAngleCeil = Math.ceil(curAngle); //已坐标 (0,1) 向量为参考 if (direction.y < 0) { //相反方向击打 //正方向 if (curAngleCeil > 150) { directionPunch = "straightPunch"; name = "负向的直拳"; ename = "back-straight"; } else if (direction.x > 0) { directionPunch = "rightPunch"; name = "负向的右拳"; ename = "back-right"; } else if (direction.x < 0) { directionPunch = "leftPunch"; name = "负向的左拳"; ename = "back-left"; } } else { // y 大于零 手柄正方向击打 if (curAngleCeil < 30) { directionPunch = "straightPunch"; name = "正向的直拳"; ename = "front-straight"; } else if (direction.x < 0) { directionPunch = "rightPunch"; name = "正向的右拳"; ename = "front-right"; } else if (direction.x > 0) { directionPunch = "leftPunch"; name = "正向的左拳"; ename = "front-left"; } } this.quitHitCount++; if (callback) { let temp = { type: 'hit', hit: hitValue / 5, hitCount: this.quitHitCount, direction: directionPunch, directionVect: { 'x': direction.x, 'y': direction.y }, angle: curAngleCeil, name: name, ename: ename } callback(temp); } } } };