gamePanel.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 enTime:number = Date.parse(gameManager.Instance.golabMemory.Chagnci?.endTime);
  78. const xx=(enTime-Date.now())/1000;
  79. if(xx<=0){
  80. console.error("caonima:",xx);
  81. }
  82. console.log("time:", util.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss"));
  83. this._countdownTime =xx;
  84. this._updateTime();
  85. }
  86. // //存在离线时间
  87. // if (playerData.instance.settings.hideTime) {
  88. // const subTime = Date.now() - playerData.instance.settings.hideTime;
  89. // const hour = Math.floor(subTime / 1000 / 60);
  90. // let addGold;
  91. // if (hour >= gameConstants.OFFLINE_MAX_GOLD * (gameConstants.OFFLINE_ADD_GOLD_TIME / 60)) {
  92. // //大于等于最大上限时间
  93. // addGold = gameConstants.OFFLINE_MAX_GOLD;
  94. // } else {
  95. // addGold = Math.floor(hour / (gameConstants.OFFLINE_ADD_GOLD_TIME / 60));
  96. // }
  97. // if (addGold > 0) {
  98. // uiManager.instance.showDialog(gameConstants.PANEL_PATH_LIST.OFFLINEREWARD, [addGold]);
  99. // }
  100. // }
  101. }
  102. /**
  103. * 获取玩家金币数量
  104. */
  105. private _getPlayerGold() {
  106. return playerData.instance.playerInfo['gold'];
  107. }
  108. /**
  109. * 更新时间显示
  110. */
  111. private _updateTime() {
  112. if(this._countdownTime<=0){
  113. console.log("游戏结束");
  114. this.TimeOverPanel.active=true;
  115. return;
  116. }
  117. this.lbCountdownTime.string=util.formatTimeForSecond(this._countdownTime);
  118. }
  119. public onBtnSetting() {
  120. AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.CLICK);
  121. uiManager.instance.showDialog(gameConstants.PANEL_PATH_LIST.SETTING);
  122. }
  123. public onBtnVideoGetGold() {
  124. // AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.COUNTDOWNGETGOLD);
  125. // gameManager.scriptsBondsdk.showRewardVideo(() => {
  126. // AudioManager.instance.playSound(gameConstants.SOUND_NAME_LIST.VIDEOGETGOLD);
  127. // playerData.instance.updatePlayerInfo('gold', gameConstants.GAMEPANEL_VIDEO_GET_GOLD);
  128. // clientEvent.dispatchEvent(gameConstants.EVENT_LIST.GOLD_SHOW_UPDATE);
  129. // });
  130. }
  131. update(dt: number) {
  132. if(this._countdownTime>0){
  133. this._countdownTime -= dt;
  134. this._time+=dt;
  135. if(this._time>=1||this._countdownTime<=0){
  136. this._time=0;
  137. this._updateTime();
  138. }
  139. }
  140. if (this._checkCanClick) return;
  141. this._checkCanClickTime += dt;
  142. if (this._checkCanClickTime >= gameConstants.GAMEPANEL_CAN_CLICK_INTERVAL) {
  143. this._checkCanClick = true;
  144. }
  145. }
  146. /**
  147. * 创建金币
  148. */
  149. public onCreatJinbi() {
  150. if (!this._checkCanClick) return;
  151. console.log("创建一个金币");
  152. if (this._getPlayerGold() > 0) {
  153. this.HttpSendGold();
  154. this.createCoin(new Vec3(-0.22, 1.33, -7.23));
  155. this.btnTuiBi.interactable = false;
  156. } else {
  157. uiManager.instance.showTips(i18n.t('gamePanel.shortGold'));
  158. }
  159. }
  160. /**
  161. * 请求投币
  162. */
  163. HttpSendGold() {
  164. let url = "coins/manage/coinFlip";
  165. HttpHelp.httpPost(url, null, this.HttpSendGoldBack, gameManager.Instance.golabMemory.Token, this);
  166. }
  167. HttpSendGoldBack(data: any) {
  168. try {
  169. if(data==-1)return;
  170. // console.log('HttpSendGoldBack: ',data);
  171. var obj: ResponseBase = JSON.parse(data);
  172. if (obj.code != 1) {
  173. console.error("获取服务器信息出错", data);
  174. return;
  175. }
  176. let coin: CoinFlipVo = obj.data;
  177. if (!coin) {
  178. console.error("推币返回的数据有误", obj);
  179. return;
  180. }
  181. playerData.instance.setPlayMoney(coin.gameCurrcy);
  182. clientEvent.dispatchEvent(gameConstants.EVENT_LIST.GOLD_SHOW_UPDATE);
  183. if (coin.awardList && coin.awardList.length > 0) {
  184. let len=coin.awardList.length;
  185. if(len>gameConstants.MaxAwardCount){
  186. len=gameConstants.MaxAwardCount;
  187. }
  188. for (let i = 0; i < len; i++) {
  189. let key = coin.awardList[i].uuid;
  190. if (!key) continue;
  191. gameManager.Instance._createPresent(coin.awardList[i]); //创建一个礼物
  192. gameManager.Instance.golabMemory.AwardInfos?.set(key, coin.awardList[i]);
  193. }
  194. }
  195. this.btnTuiBi.interactable = true;
  196. } catch (errorx) {
  197. console.error("投币回包err:",errorx);
  198. this.btnTuiBi.interactable = true;
  199. }
  200. }
  201. /**
  202. * 创建礼物
  203. */
  204. public onCreateLiwu() {
  205. console.log("创建一个礼物 ");
  206. gameManager.Instance._createPresent(); //创建一个礼物
  207. }
  208. public SetGoodsData(data:GetAwardVo){
  209. if(!data){
  210. console.error("打开领取界面数据有误,cao",data);
  211. return;
  212. }
  213. this.goodsPanel.node.active=true;
  214. this.goodsPanel.SetData(data);
  215. }
  216. public btnReLoad(){
  217. window.location.reload();
  218. }
  219. public btnZhongJiang(){
  220. if(gameManager.Instance.golabMemory.isReplse){
  221. window.location.href = "http://star.mstardance.com/starmini/winRecord.html";
  222. }else{
  223. window.location.href = "http://teststar.mstardance.com/starmini/winRecord.html";
  224. }
  225. }
  226. public btnZhuanYouxiBi(){
  227. if(gameManager.Instance.golabMemory.isReplse){
  228. window.location.href = "http://star.mstardance.com/starmini/mall.html";
  229. }else{
  230. window.location.href = "http://teststar.mstardance.com/starmini/mall.html";
  231. }
  232. }
  233. }