gamePanel.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { _decorator, Component, Node, Label, geometry, find, Camera, PhysicsSystem, instantiate, Vec3, Button, debug } from 'cc';
  2. import { clientEvent } from '../../framework/clientEvent';
  3. import { playerData } from '../../framework/playerData';
  4. import { uiManager } from '../../framework/uiManager';
  5. import { gameConstants } from '../../game/gameConstants';
  6. import * as i18n from '../../../../extensions/i18n/assets/LanguageData'
  7. import { AudioManager } from '../../framework/audioManager';
  8. import { gameManager } from '../../game/gameManager';
  9. import { HttpHelp } from '../../game/HttpHelp';
  10. import { ResponseBase } from '../../game/pack/ResponseBase';
  11. import { CoinFlipVo } from '../../game/pack/CoinsBatchAwardVo';
  12. import { util } from '../../framework/util';
  13. import { GetGoodPanel } from './GetGoodPanel';
  14. import { GetAwardVo } from '../../game/pack/GetAwardVo';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('GamePanel')
  17. export class GamePanel extends Component {
  18. @property(Label)
  19. lbCountdownTime: Label = null!; //倒计时显示
  20. @property(Button)
  21. btnTuiBi: Button = null!;
  22. @property(GetGoodPanel)
  23. goodsPanel:GetGoodPanel = null!;
  24. @property(Node)
  25. TimeOverPanel:Node = null!;
  26. private _countdownTime: number = 0; //倒计时
  27. private _checkCanClick: Boolean = true; //可点击生成金币
  28. private _checkCanClickTime: number = 0; //可点击时间间隔计算
  29. private _time: number=0;//刷新闹钟时间间隔
  30. static Instance: GamePanel;
  31. onEnable() {
  32. // this.node.on(Node.EventType.TOUCH_START, this._onTouchStart, this);
  33. }
  34. onDisable() {
  35. // this.node.off(Node.EventType.TOUCH_START, this._onTouchStart, this);
  36. }
  37. onLoad() {
  38. // this._comCamera = find('Main Camera')?.getComponent(Camera)!;
  39. GamePanel.Instance=this;
  40. }
  41. // private _onTouchStart(e: any) {
  42. // if (!this._checkCanClick) return;
  43. // //若显示新手引导 隐藏
  44. // if (this._isFirstClick) {
  45. // this._isFirstClick = false;
  46. // clientEvent.dispatchEvent(gameConstants.EVENT_LIST.TOUCH_HIDE_TOUCHPLANE, false);
  47. // }
  48. // if (this._getPlayerGold() > 0) {
  49. // // const outRay = new geometry.Ray();
  50. // // const touchPoint = e.touch._point;
  51. // // this._comCamera.screenPointToRay(touchPoint.x, touchPoint.y, outRay);
  52. // // if (PhysicsSystem.instance.raycast(outRay, gameConstants.GROUP_MASK_LIST.PUSH_TOUCH), 50) {
  53. // // //点击到推动台的位置 永远只会有一个
  54. // // if (PhysicsSystem.instance.raycastResults.length <= 0) return;
  55. // // const hitPoint = PhysicsSystem.instance.raycastResults[0].hitPoint;
  56. // // this.createCoin(hitPoint);
  57. // // }
  58. // } else {
  59. // uiManager.instance.showTips(i18n.t('gamePanel.shortGold'));
  60. // }
  61. // }
  62. private createCoin(hitPoint: any) {
  63. this._checkCanClick = false;
  64. this._checkCanClickTime = 0;
  65. if (gameManager.Instance.golabMemory.Chagnci?.cost) {
  66. playerData.instance.updatePlayerInfo('gold', 0 - gameManager.Instance.golabMemory.Chagnci?.cost);
  67. }
  68. clientEvent.dispatchEvent(gameConstants.EVENT_LIST.GOLD_SHOW_UPDATE);
  69. clientEvent.dispatchEvent(gameConstants.EVENT_LIST.TOUCH_CREATE_GOLD,
  70. true,
  71. new Vec3(hitPoint.x, hitPoint.y, hitPoint.z - Math.random() * gameConstants.PUSH_RANGE_Z),
  72. new Vec3(0, 0, 0));//randomEul(), randomEul(), randomEul()
  73. }
  74. show() {
  75. clientEvent.dispatchEvent(gameConstants.EVENT_LIST.TOUCH_HIDE_TOUCHPLANE, true);
  76. if(gameManager.Instance.golabMemory.Chagnci?.endTime){
  77. const newEndTime = gameManager.Instance.golabMemory.Chagnci?.endTime.replace(/-/g, '/')
  78. const enTime:number = Date.parse(newEndTime);
  79. const xx=(enTime-Date.now()) / 1000;
  80. if(xx<=0){
  81. console.error("caonima:",xx);
  82. }
  83. console.log("time:", util.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss"));
  84. this._countdownTime =xx;
  85. this._updateTime();
  86. }
  87. // //存在离线时间
  88. // if (playerData.instance.settings.hideTime) {
  89. // const subTime = Date.now() - playerData.instance.settings.hideTime;
  90. // const hour = Math.floor(subTime / 1000 / 60);
  91. // let addGold;
  92. // if (hour >= gameConstants.OFFLINE_MAX_GOLD * (gameConstants.OFFLINE_ADD_GOLD_TIME / 60)) {
  93. // //大于等于最大上限时间
  94. // addGold = gameConstants.OFFLINE_MAX_GOLD;
  95. // } else {
  96. // addGold = Math.floor(hour / (gameConstants.OFFLINE_ADD_GOLD_TIME / 60));
  97. // }
  98. // if (addGold > 0) {
  99. // uiManager.instance.showDialog(gameConstants.PANEL_PATH_LIST.OFFLINEREWARD, [addGold]);
  100. // }
  101. // }
  102. }
  103. /**
  104. * 获取玩家金币数量
  105. */
  106. private _getPlayerGold() {
  107. return playerData.instance.playerInfo['gold'];
  108. }
  109. /**
  110. * 更新时间显示
  111. */
  112. private _updateTime() {
  113. if(this._countdownTime<=0){
  114. console.log("游戏结束");
  115. this.TimeOverPanel.active=true;
  116. return;
  117. }
  118. this.lbCountdownTime.string=util.formatTimeForSecond(this._countdownTime);
  119. }
  120. public onBtnSetting() {
  121. AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.CLICK);
  122. uiManager.instance.showDialog(gameConstants.PANEL_PATH_LIST.SETTING);
  123. }
  124. public onBtnVideoGetGold() {
  125. // AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.COUNTDOWNGETGOLD);
  126. // gameManager.scriptsBondsdk.showRewardVideo(() => {
  127. // AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.VIDEOGETGOLD);
  128. // playerData.instance.updatePlayerInfo('gold', gameConstants.GAMEPANEL_VIDEO_GET_GOLD);
  129. // clientEvent.dispatchEvent(gameConstants.EVENT_LIST.GOLD_SHOW_UPDATE);
  130. // });
  131. }
  132. update(dt: number) {
  133. if(this._countdownTime>0){
  134. this._countdownTime -= dt;
  135. this._time+=dt;
  136. if(this._time>=1||this._countdownTime<=0){
  137. this._time=0;
  138. this._updateTime();
  139. }
  140. }
  141. if (this._checkCanClick) return;
  142. this._checkCanClickTime += dt;
  143. if (this._checkCanClickTime >= gameConstants.GAMEPANEL_CAN_CLICK_INTERVAL) {
  144. this._checkCanClick = true;
  145. }
  146. }
  147. /**
  148. * 创建金币
  149. */
  150. public onCreatJinbi() {
  151. if (!this._checkCanClick) return;
  152. console.log("创建一个金币");
  153. if (this._getPlayerGold() > 0) {
  154. this.HttpSendGold();
  155. this.createCoin(new Vec3(-0.22, 1.33, -7.23));
  156. this.btnTuiBi.interactable = false;
  157. } else {
  158. uiManager.instance.showTips(i18n.t('gamePanel.shortGold'));
  159. }
  160. }
  161. /**
  162. * 请求投币
  163. */
  164. HttpSendGold() {
  165. let url = "coins/manage/coinFlip";
  166. HttpHelp.httpPost(url, null, this.HttpSendGoldBack, gameManager.Instance.golabMemory.Token, this);
  167. }
  168. HttpSendGoldBack(data: any) {
  169. try {
  170. if(data==-1){
  171. // this.btnTuiBi.interactable = false;
  172. uiManager.instance.showTips('网络异常,请重新加载!');
  173. // this.btnTuiBi.interactable = true;
  174. return;
  175. }
  176. // console.log('HttpSendGoldBack: ',data);
  177. var obj: ResponseBase = JSON.parse(data);
  178. if (obj.code != 1) {
  179. console.error("获取服务器信息出错", data);
  180. uiManager.instance.showTips('网络异常,请重新加载!');
  181. // this.btnTuiBi.interactable = true;
  182. return;
  183. }
  184. let coin: CoinFlipVo = obj.data;
  185. if (!coin) {
  186. console.error("推币返回的数据有误", obj);
  187. return;
  188. }
  189. playerData.instance.setPlayMoney(coin.gameCurrcy);
  190. clientEvent.dispatchEvent(gameConstants.EVENT_LIST.GOLD_SHOW_UPDATE);
  191. if (coin.awardList && coin.awardList.length > 0) {
  192. let len=coin.awardList.length;
  193. if(len>gameConstants.MaxAwardCount){
  194. len=gameConstants.MaxAwardCount;
  195. }
  196. for (let i = 0; i < len; i++) {
  197. let key = coin.awardList[i].uuid;
  198. if (!key) continue;
  199. gameManager.Instance._createPresent(coin.awardList[i]); //创建一个礼物
  200. gameManager.Instance.golabMemory.AwardInfos?.set(key, coin.awardList[i]);
  201. }
  202. }
  203. this.btnTuiBi.interactable = true;
  204. } catch (errorx) {
  205. console.error("投币回包err:",errorx);
  206. this.btnTuiBi.interactable = true;
  207. }
  208. }
  209. /**
  210. * 创建礼物
  211. */
  212. public onCreateLiwu() {
  213. console.log("创建一个礼物 ");
  214. gameManager.Instance._createPresent(); //创建一个礼物
  215. }
  216. public SetGoodsData(data:GetAwardVo){
  217. if(!data){
  218. console.error("打开领取界面数据有误,cao",data);
  219. return;
  220. }
  221. this.goodsPanel.node.active=true;
  222. this.goodsPanel.SetData(data);
  223. }
  224. public btnReLoad(){
  225. window.location.reload();
  226. }
  227. public btnZhongJiang(){
  228. if(gameManager.Instance.golabMemory.isReplse){
  229. window.location.href = "http://star.mstardance.com/starmini/winRecord.html";
  230. }else{
  231. window.location.href = "http://teststar.mstardance.com/starmini/winRecord.html";
  232. }
  233. }
  234. public btnZhuanYouxiBi(){
  235. if(gameManager.Instance.golabMemory.isReplse){
  236. window.location.href = "http://star.mstardance.com/starmini/mall.html";
  237. }else{
  238. window.location.href = "http://teststar.mstardance.com/starmini/mall.html";
  239. }
  240. }
  241. }