WebView.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. export default class WebView {
  2. static EVENT: WebViewEvent = {
  3. onGameInit: "onGameInit",
  4. onUploadInfo: "onUploadInfo",
  5. onAiRandomInfo: "onAiRandomInfo",
  6. onFruitInfo: "onFruitInfo",
  7. onDeviceUpdateData: "onDeviceUpdateData",
  8. onDeviceState: "onDeviceState",
  9. onDeviceClose: "onDeviceClose",
  10. onBoxingPostHit: "onBoxingPostHit",
  11. onQuit: "onQuit",
  12. onQuitModal: "onQuitModal"
  13. }
  14. static listeners: cc.Node[] = [];
  15. static addListener(listener: cc.Node) {
  16. if (this.listeners.indexOf(listener) == -1) {
  17. this.listeners.push(listener);
  18. }
  19. }
  20. static removeListener(listener: cc.Node) {
  21. let index = this.listeners.indexOf(listener);
  22. if (index > -1) {
  23. this.listeners.splice(index, 1);
  24. }
  25. }
  26. static dispatchEvent(eventName: string, gameData: any) {
  27. for (const listener of this.listeners) {
  28. if (listener.isValid) {
  29. listener.emit(eventName, gameData);
  30. }
  31. }
  32. }
  33. static loadSpriteFrame(base64Url: string, callback: Function) {
  34. let img = new Image();
  35. img.src = base64Url;
  36. img.onload = () => {
  37. let texture = new cc.Texture2D();
  38. texture.initWithElement(img);
  39. texture.handleLoadedTexture();
  40. if (callback instanceof Function) {
  41. callback(new cc.SpriteFrame(texture));
  42. }
  43. }
  44. }
  45. static initGame() {
  46. uni.postMessage({
  47. data:{
  48. funName: "gameInit",
  49. gameData: {}
  50. }
  51. });
  52. }
  53. static uploadInfo(gameScore: number, gameTime: number, calorieBurned: number) {
  54. uni.postMessage({
  55. data:{
  56. funName: "uploadInfo",
  57. gameData: {
  58. gameScore: gameScore,
  59. gameTime: gameTime,
  60. calorieBurned: calorieBurned
  61. }
  62. }
  63. });
  64. }
  65. static getAiInfo() {
  66. uni.postMessage({
  67. data:{
  68. funName: "aiRandomInfo",
  69. gameData: {}
  70. }
  71. });
  72. }
  73. static getFruitInfo(calorie: number) {
  74. uni.postMessage({
  75. data:{
  76. funName: "fruitInfo",
  77. gameData: {
  78. calorie: calorie
  79. }
  80. }
  81. });
  82. }
  83. static onBindBoxingPost(){
  84. uni.postMessage({
  85. data: {
  86. // funName: "bindBoxingPost",
  87. funName: "bindHitBoxingPost",
  88. gameData: {}
  89. }
  90. });
  91. }
  92. static onModify(){
  93. uni.postMessage({
  94. data: {
  95. funName: "setAngleRatio",
  96. gameData: {
  97. angleRatio:0.5 //z / x 值越小,代表直拳的判断方位越大
  98. }
  99. }
  100. })
  101. }
  102. static onBindHitBoxingPost(){
  103. uni.postMessage({
  104. data: {
  105. funName: "bindHitBoxingPost",
  106. gameData: {}
  107. }
  108. })
  109. }
  110. static onUnbindHitBoxingPost(){
  111. uni.postMessage({
  112. data: {
  113. funName: "unbindHitBoxingPost",
  114. gameData: {}
  115. }
  116. })
  117. }
  118. static closeGame(){
  119. uni.postMessage({
  120. data: {
  121. funName: "closeGame",
  122. gameData: {}
  123. }
  124. });
  125. }
  126. static onAddQuitModalListener(){
  127. uni.postMessage({
  128. data: {
  129. funName: "addQuitModal",
  130. gameData: {}
  131. }
  132. });
  133. }
  134. }
  135. window.onWebViewMessage = (data: any) => {
  136. console.log(data);
  137. WebView.dispatchEvent(data.funName, data.gameData);
  138. }
  139. export {}
  140. declare global{
  141. interface Window {
  142. onWebViewMessage: Function;
  143. }
  144. class uni {
  145. static postMessage: Function;
  146. }
  147. interface WebViewEvent {
  148. /**
  149. * onGameInit
  150. * 初始化游戏数据
  151. * gameData :
  152. * avatarBase64Url:"",//base64字符串头像,后面以base64为主
  153. * userName:"", //名字
  154. * gender: 0,//"0:男,1:女"
  155. * caloriUnit:10,//每次跳动消耗的卡路里
  156. * calorieParams:{runUnit:10,jumpUnit:20}, //蹦床时候的参数
  157. * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
  158. */
  159. onGameInit: string;
  160. /**
  161. * 上传分数后的返回,
  162. * gameData:
  163. * {}
  164. */
  165. onUploadInfo: string;
  166. /**
  167. * 获取ai信息的数据回调
  168. * aiId: 0, //ai的id
  169. * aiName: "",//ai 的名字
  170. * aiGender: 0,//"0:男,1:女"
  171. * aiType: "",//网名类型
  172. * aiAvatarBase64Url: ""//base64字符串,头像
  173. */
  174. onAiRandomInfo: string;
  175. /**
  176. * 获取水果图片信息
  177. * fruitBase64Url: "",// 水果雪碧图 base64
  178. * unitWidth: 100,// 雪碧图 单张图片宽
  179. * unitHeight: 100,// 雪碧图 单张图片高
  180. * unit: "px",// 雪碧图 单位
  181. * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
  182. * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
  183. * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
  184. */
  185. onFruitInfo: string;
  186. /**
  187. * 设备连接成功后的返回值
  188. * 蹦床的返回数值,有可能两种格式
  189. * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
  190. */
  191. onDeviceUpdateData: string;
  192. /**
  193. * 暂时只返回设备数据连接错误信息
  194. * gamedata = {state: -1, msg: "设备数据错误"}
  195. */
  196. onDeviceState: string;
  197. /**
  198. * 和设备断开连接时候回调
  199. * gamedata = {msg: '设备断开连接。'}
  200. */
  201. onDeviceClose: string;
  202. onBoxingPostHit: string;
  203. onQuit: string;
  204. onQuitModal: string;
  205. }
  206. }