123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- declare global {
- interface Window {
- SHOES_SDK: any;
- }
- }
- const _delay = 1000 / 60.0; // 16 ms
- const LABEL = ["right pos x", "right pos y", "right pos z", "right att x", "right att y", "right att z", "right acc x", "right acc y", "right acc z", "right front mag", "right back mag ", "s zupt",
- "left pos x", "left pos y", "left pos z", "left att x", "left att y", "left att z", "left acc x", "left acc y", "left acc z", "left front mag", "left back mag", "h zupt",
- "down", "jump", "rssi", "timestamp", "right step", "left step", "rssi mobile"];
- enum CMD {
- NONE_INTERATION = 2000,
- FRONT_LEFT,//左上键
- FRONT_RIGHT,//右上键
- BACK_LEFT,//左下键
- BACK_RIGHT,//右下键
- ENTER_KEY,//确认键
- CANCLE_KEY,//取消键
- }
- enum MOTION {
- MOTION_STOP = 0,
- MOTION_RUN, // 跑
- MOTION_JUMP, // 跳
- MOTION_DOWN, // 蹲
- MOTION_LEFT, // 滑左
- MOTION_RIGHT, // 滑右
- MOTION_FRONT, // 滑前
- MOTION_BACK, // 滑后
- MOTION_LEFT_UP, // 点击-左上
- MOTION_LEFT_DOWN, // 点击-左下
- MOTION_RIGHT_UP, // 点击-右上
- MOTION_RIGHT_DOWN, // 点击-右下
- MOTION_STEP, // 点击-原地踩
- MOTION_JUMP_OC, // 开合跳
- MOTION_JUMP_VERTICAL, // 垂直跳
- MOTION_ROCK, //石头
- MOTION_SCISSORS, //剪刀
- MOTION_PAPER, //布
- MOTION_STRETCH_LEFT, //左伸腿
- MOTION_LIFT_LEFT, //左抬腿
- MOTION_STRETCH_RIGHT, //右伸腿
- MOTION_LIFT_RIGHT, //右抬腿
- MOTION_STRETCH_FRONT_LEFT,
- MOTION_STRETCH_FRONT_RIGHT,
- MOTION_DOWN_FRONT, //踮脚蹲
- NUMBERS_OF_MOTION,
- }
- enum STEP {
- FREQ= 1000,
- }
- class Subscriber {
- _target: Object;
- _fn: Function;
- constructor(target: Object, fn: Function) {
- this._target = target;
- this._fn = fn;
- }
- }
- class _SDK {
- _ver: number = 1;
- _events: { [key: string]: Subscriber[] } = {};
- _cmd: number;
- _cmdTime: number;
- _motion: Array<number> = [0, 0, 0, 0];
- _motionTime: Array<number> = [0, 0, 0, 0];
- _stepFreq: number;
- _stepCount: number;
- _velocity: number;
- _responseCallMap = {string:Function};
- _gameDataCallback:Function;
- _inited: boolean = false;
- constructor() {
- }
- callback(name:string, args:Object){
- console.log("callback " + name + " " + args);
- const func = this._responseCallMap[name];
- if(func)func.call(func, args)
- }
- /**
- * 是否在单位时间里动作
- * @param index
- * @param delay
- */
- isMotion(index: number, delay: number) {
- let ts = new Date();
- let result = (this._motion[index] > 0) && (ts.getTime() - this._motionTime[index]) <= delay;
- if(result){
- this._motion[index] = 0;
- }
- return result;
- }
- /**
- * 左脚
- * @param delay 延时时间,默认 1帧 = 16ms
- */
- isLeftFootDown(delay: number = _delay) {
- return this.isMotion(0, delay);
- }
- /**
- * 右脚
- * @param delay
- */
- isRightFootDown(delay: number = _delay) {
- return this.isMotion(1, delay);
- }
- /**
- * 获取操作指定
- */
- getCmd(): number {
- let result = this._cmd;
- if(result > 0){
- this._cmd = 0;
- }
- return result;
- }
- cmd(id: number, v0: number) {
- this._cmd = v0 + CMD.NONE_INTERATION;
- let ts = new Date().getTime();
- if(ts - this._cmdTime < 500) return;
- this._cmdTime = ts;
- console.log("cmd set " + v0 + " " + (this._cmd)) ;
- this.emit((this._cmd).toString(), ts);
- }
- /**
- * 获取运动数据 [0,0,0,0]
- */
- getMotion(): Array<number> {
- return this._motion;
- }
- motion(type: number, id: number, v0: number, v1: number, v2: number, v3: number) {
- this._motion[0] = v0;
- this._motion[1] = v1;
- this._motion[2] = v2;
- this._motion[3] = v3;
- console.log("motion set " + this._motion);
- let now = new Date();
- let ts = now.getTime();
- let arr = this._motion;
- for (let i = 0; i < arr.length; i++){
- if(arr[i] > 0){
- if(ts - this._motionTime[i] < 10) continue;
- this._motionTime[i] = ts;
- let motionIndex = arr[i];
- let motion = motionIndex.toString();
- this.emit(motion, ts);
- console.log("motion set " + motionIndex + " -> " + motion + " " + MOTION[motionIndex]);
- }
- }
- // if (v0 == 1) {
- // this._motionTime[0] = ts;
- // this.emit(MOTION.LEFT_DOWN.toString(), ts);
- // console.log("motion set left");
- // }
- // if (v1 == 1) {
- // this._motionTime[1] = ts;
- // this.emit(MOTION.RIGHT_DOWN.toString(), ts);
- // console.log("motion set right");
- // }
- // if (v2 > 0) {
- // this._motionTime[2] = ts;
- // let motion = MOTION[v2];
- // this.emit(motion, ts);
- // console.log("motion set " + motion);
- // }
- // if (v3 > 0) {
- // this._motionTime[3] = ts;
- // let motion = MOTION[v3];
- // this.emit(motion, ts);
- // console.log("motion set " + motion);
- // }
- }
- /**
- * 当前步频
- */
- getStepFreq(): number {
- return this._stepFreq;
- }
- /**
- * 开局后的步数
- */
- getStepCount(): number {
- return this._stepCount;
- }
- getVelocity(): number {
- return this._velocity;
- }
- stepFreq(freq: number) {
- this._stepFreq = freq;
- console.log("step freq " + freq);
- this.emit(STEP.FREQ.toString(), freq);
- }
- step(freq: number, count: number) {
- this.stepFreq(freq);
- this._stepCount = count;
- console.log("step count " + count);
- }
- stepData(freq: number, count: number, velocity: number) {
- this.stepFreq(freq);
- this._stepCount = count;
- this._velocity = velocity;
- }
- /**
- * 发布/ 触发
- * @param subscriber
- * @param args
- */
- emit(subscriber: string, ...args: any) {
- let callbackList = this._events[subscriber] || [];
- if(callbackList.length > 0){
- console.log("motion emit " + subscriber + ", " + callbackList.length);
- callbackList.forEach(item => item._fn.apply(item._target || item._fn, args));
- }
- return this;
- }
- /**
- * 订阅/监听
- * @param subscriber
- * @param callback
- * @param target
- */
- on(subscriber: string, callback?: Function, target?: Object) {
- if(!this._inited){
- this._inited = true;
- this.onSdkLoaded();
- }
- let callbackList = this._events[subscriber] || [];
- callback && callbackList.push(new Subscriber(target||this,callback));
- console.log("motion set on " + subscriber + ", " + callbackList.length);
- this._events[subscriber] = callbackList;
- return this;
- }
- /**
- * 只订阅一次/监听一次:
- * 思路:
- * 1. 重新包装一个回调函数(有名的),进行注册订阅/监听,
- * 2. 包装函数里面直接调用 once方法的第二个参数回调函数,然后调用off方法 卸载该包装函数
- * @param subscriber
- * @param callback
- * @param target
- */
- once(subscriber: string, callback?: Function, target?: Object) {
- let decor = (...args: any[]) => {
- callback && callback.apply(target || this, args);
- this.off(subscriber, decor);
- };
- this.on(subscriber, decor);
- return this;
- }
- /**
- * 卸载/取消 某一个回调监听(不是取消eventName的所有回调监听),主要配合once一起,实例单独调用,无意义
- * @param subscriber
- * @param callback
- */
- off(subscriber: string, callback: Function) {
- let callbackList = this._events[subscriber] || [];
- this._events[subscriber] = callbackList.filter(item => item._fn !== callback);
- return this;
- }
- /**
- * 卸载/取消 指定eventName 的所有订阅/监听
- * @param subscriber
- * @param callback
- */
- remove(subscriber: string, callback?: Function) {
- this._events[subscriber] = [];
- callback && callback();
- return this;
- }
- onSdkLoaded(){
- this._callsdk("onLoad", {ver: this._ver});
- }
- /**
- * 退出/后退事件
- */
- onBackPressed(foot:boolean = false){
- this._callsdk("onBackPressed", {foot: foot});
- }
- /**
- * 游戏开局调用事件
- */
- onGameStart(){
- this._callsdk("onGameStart", {});
- }
- /**
- * 游戏局结束调用事件
- * @param level 当局游戏级别
- * @param score 当局游戏评分
- * @param record 当局游戏战绩 0: 负, 1: 胜
- * @param mode 当局游戏模式 0: 单人, 1: 匹配, 2: 好友对战
- * @param opponentId 当局游戏对手ID
- * @param callback data
- * data = {
- * 'consume':$consume,
- * 'equivalent':$equivalent,
- * 'unit':'步'
- * }
- */
- onGameEnd(level: number = 0, score: number = 0.0, record: number = 0, mode: number = 0, opponentId: number = 0, callback?: Function) {
- if (callback) {
- let method = "onGameEnd_callback";
- this._responseCallMap[method] = callback;
- }
- this._callsdk("onGameEnd", {level: level, score:score, record:record,mode:mode, opponentId:opponentId});
- }
- /**
- * 获取当前用户信息
- * @param callback({"id": 1, "nickname":"nickname", "gender":1, "avatar":""})
- *
- * user用户信息结构
- * {
- * "id": 1,
- * "name":"nickname",
- * "gender":1,
- * "avatar":""
- * }
- */
- getUserInfo(callback:Function){
- let method = "getUserInfo_callback";
- this._responseCallMap[method] = callback;
- this._callsdk(method, {callback: method});
- }
- /**
- *
- * @param type, 0:世界,1:好友
- * @param callback(args)
- * args[0] = type
- * args[1] = {"list":[{"user":{"id": 1, "nickname":"nickname", "gender":1, "avatar":""}, "score":100, "rank":1}]}
- */
- getRank(type:number, callback:Function){
- let method = "getRank_callback";
- this._responseCallMap[method] = callback;
- this._callsdk(method, {callback: method, type: type});
- }
- /**
- * 震动反馈
- * @param duration 时长 ms [100 .. 1000]
- * @param leftOrRight [0:双,1:左,2:右]
- */
- vibrate(duration: number, leftOrRight:number = 0) {
- let method = "vibrate";
- this._callsdk(method, {duration:duration, leftOrRight: leftOrRight});
- }
- /**
- * 打开鞋子位移、加速度、姿态等数据传输
- * @param open
- * @param gameDataCallback
- */
- openGameData(open:boolean, gameDataCallback:Function){
- this._gameDataCallback = gameDataCallback;
- this._callsdk("openGameData", {open:open});
- }
- gameData(args:Object){
- if(this._gameDataCallback)this._gameDataCallback.call(this._gameDataCallback, args);
- }
- _callsdk(method:string, args:Object){
- console.log("sdk call " + method);
- if(window["SDKBridge"])window["SDKBridge"].postMessage(JSON.stringify({method:method, args:args}))
- }
- }
- const SDK = new _SDK();
- window.SHOES_SDK = SDK;
- export {SDK, MOTION, CMD}
|