| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- export default class WebView {
- static EVENT: WebViewEvent = {
- onGameInit: "onGameInit",
- onUploadInfo: "onUploadInfo",
- onAiRandomInfo: "onAiRandomInfo",
- onFruitInfo: "onFruitInfo",
- onDeviceUpdateData: "onDeviceUpdateData",
- onDeviceState: "onDeviceState",
- onDeviceClose: "onDeviceClose",
- onBoxingPostHit: "onBoxingPostHit",
- onQuit: "onQuit",
- onQuitModal: "onQuitModal"
- }
- static listeners: cc.Node[] = [];
- static addListener(listener: cc.Node) {
- if (this.listeners.indexOf(listener) == -1) {
- this.listeners.push(listener);
- }
- }
- static removeListener(listener: cc.Node) {
- let index = this.listeners.indexOf(listener);
- if (index > -1) {
- this.listeners.splice(index, 1);
- }
- }
- static dispatchEvent(eventName: string, gameData: any) {
- for (const listener of this.listeners) {
- if (listener.isValid) {
- listener.emit(eventName, gameData);
- }
- }
- }
- static loadSpriteFrame(base64Url: string, callback: Function) {
- let img = new Image();
- img.src = base64Url;
- img.onload = () => {
- let texture = new cc.Texture2D();
- texture.initWithElement(img);
- texture.handleLoadedTexture();
- if (callback instanceof Function) {
- callback(new cc.SpriteFrame(texture));
- }
- }
- }
- static initGame() {
- uni.postMessage({
- data:{
- funName: "gameInit",
- gameData: {}
- }
- });
- }
- static uploadInfo(gameScore: number, gameTime: number, calorieBurned: number) {
- uni.postMessage({
- data:{
- funName: "uploadInfo",
- gameData: {
- gameScore: gameScore,
- gameTime: gameTime,
- calorieBurned: calorieBurned
- }
- }
- });
- }
- static getAiInfo() {
- uni.postMessage({
- data:{
- funName: "aiRandomInfo",
- gameData: {}
- }
- });
- }
- static getFruitInfo(calorie: number) {
- uni.postMessage({
- data:{
- funName: "fruitInfo",
- gameData: {
- calorie: calorie
- }
- }
- });
- }
- static onBindBoxingPost(){
- uni.postMessage({
- data: {
- // funName: "bindBoxingPost",
- funName: "bindHitBoxingPost",
- gameData: {}
- }
- });
- }
-
- static onModify(){
- uni.postMessage({
- data: {
- funName: "setAngleRatio",
- gameData: {
- angleRatio:0.5 //z / x 值越小,代表直拳的判断方位越大
- }
- }
- })
- }
- static onBindHitBoxingPost(){
- uni.postMessage({
- data: {
- funName: "bindHitBoxingPost",
- gameData: {}
- }
- })
- }
- static onUnbindHitBoxingPost(){
- uni.postMessage({
- data: {
- funName: "unbindHitBoxingPost",
- gameData: {}
- }
- })
- }
- static closeGame(){
- uni.postMessage({
- data: {
- funName: "closeGame",
- gameData: {}
- }
- });
- }
- static onAddQuitModalListener(){
-
- uni.postMessage({
- data: {
- funName: "addQuitModal",
- gameData: {}
- }
- });
- }
- }
- window.onWebViewMessage = (data: any) => {
- console.log(data);
- WebView.dispatchEvent(data.funName, data.gameData);
- }
- export {}
- declare global{
- interface Window {
- onWebViewMessage: Function;
- }
- class uni {
- static postMessage: Function;
- }
- interface WebViewEvent {
- /**
- * onGameInit
- * 初始化游戏数据
- * gameData :
- * avatarBase64Url:"",//base64字符串头像,后面以base64为主
- * userName:"", //名字
- * gender: 0,//"0:男,1:女"
- * caloriUnit:10,//每次跳动消耗的卡路里
- * calorieParams:{runUnit:10,jumpUnit:20}, //蹦床时候的参数
- * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
- */
- onGameInit: string;
- /**
- * 上传分数后的返回,
- * gameData:
- * {}
- */
- onUploadInfo: string;
- /**
- * 获取ai信息的数据回调
- * aiId: 0, //ai的id
- * aiName: "",//ai 的名字
- * aiGender: 0,//"0:男,1:女"
- * aiType: "",//网名类型
- * aiAvatarBase64Url: ""//base64字符串,头像
- */
- onAiRandomInfo: string;
- /**
- * 获取水果图片信息
- * fruitBase64Url: "",// 水果雪碧图 base64
- * unitWidth: 100,// 雪碧图 单张图片宽
- * unitHeight: 100,// 雪碧图 单张图片高
- * unit: "px",// 雪碧图 单位
- * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
- * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
- * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
- */
- onFruitInfo: string;
- /**
- * 设备连接成功后的返回值
- * 蹦床的返回数值,有可能两种格式
- * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
- */
- onDeviceUpdateData: string;
- /**
- * 暂时只返回设备数据连接错误信息
- * gamedata = {state: -1, msg: "设备数据错误"}
- */
- onDeviceState: string;
- /**
- * 和设备断开连接时候回调
- * gamedata = {msg: '设备断开连接。'}
- */
- onDeviceClose: string;
-
- onBoxingPostHit: string;
- onQuit: string;
- onQuitModal: string;
- }
- }
|