TrucoFuncBtnMgr.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import ScenceManager from 'HNScenceManager'
  2. import GameManagerBase from 'GameManagerBase'
  3. var Global = require("Global");
  4. var GameProtocol = require("TrucoGameProtocol");
  5. var UIHelper = require("UIHelper");
  6. var topTipMsg = require("topTipMsg");
  7. var HomeSoundFun = require("homeSoundFunc");
  8. var popScence = require("popScence");
  9. var LanguageKey = require("LanguageKey");
  10. import { CServerItem } from 'CServerItem'
  11. var SoundFun = require("TrucoSoundFun");
  12. var BaseDefine = require("BaseDefine");
  13. cc.Class({
  14. extends: cc.Component,
  15. editor: {
  16. menu: 'Game/Truco/FuncBtnMgr'
  17. },
  18. properties: {
  19. memeSpr: cc.SpriteFrame,
  20. _nodeChat:null, //聊天
  21. },
  22. // LIFE-CYCLE CALLBACKS:
  23. onLoad: function() {
  24. //功能按钮
  25. var funcBtnNode = this.node.getChildByName("FuncBtnNode");
  26. this.initButtonHandler(funcBtnNode.getChildByName("btn_rule"));
  27. this.initButtonHandler(funcBtnNode.getChildByName("btn_Exit"));
  28. this.initButtonHandler(funcBtnNode.getChildByName("btn_Menu"));
  29. this.initButtonHandler(funcBtnNode.getChildByName("btn_Emo"));
  30. this.initButtonHandler(funcBtnNode.getChildByName("btn_Ace"));
  31. this.initButtonHandler(funcBtnNode.getChildByName("btn_Cor"));
  32. this.initButtonHandler(funcBtnNode.getChildByName("btn_Truco"));
  33. this.forceExitBtn = funcBtnNode.getChildByName("btn_Exit");
  34. this.forceExitBtn.active = false;
  35. let menuNode = this.node.getChildByName("MenuNode").getChildByName("RootNode");
  36. this.musicBtn = menuNode.getChildByName("img_sz_yy").getChildByName("btn");
  37. this.effectBtn = menuNode.getChildByName("img_sz_sy").getChildByName("btn");
  38. this.shakeBtn = menuNode.getChildByName("img_sz_zd").getChildByName("btn");
  39. funcBtnNode.getChildByName("btn_Debug").active = false//BaseDefine.LOCALTEST;
  40. this.initButtonHandler(funcBtnNode.getChildByName("btn_Debug"));
  41. },
  42. start () {
  43. this.serverItem = GameManagerBase.Instance().getGameServerItem();
  44. },
  45. //按钮事件
  46. initButtonHandler: function (btn,opt) {
  47. if(btn){
  48. UIHelper.addButtonListener(btn, this.node, "TrucoFuncBtnMgr", "onBtnClicked",opt);
  49. }
  50. },
  51. onBtnClicked: function (event,opt) {
  52. var jsGameSence = ScenceManager.Instance().getGameComponent();
  53. if(event.target.name == "btn_Exit"){//强行退出
  54. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  55. this.onBtnForceExit();
  56. }else if(event.target.name == "btn_Truco"){
  57. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  58. jsGameSence.onClickTruco();
  59. }
  60. else if(event.target.name == "btn_Ace"){
  61. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  62. jsGameSence.onClickTrucoChoose(1)
  63. }else if(event.target.name == "btn_Cor"){
  64. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  65. jsGameSence.onClickTrucoChoose(2)
  66. }else if(event.target.name == "btn_Menu"){
  67. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  68. this.onBtnMenu();
  69. }else if(event.target.name == "btn_Emo"){
  70. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  71. this.onShowSelfBQ();
  72. }else if(event.target.name == "btn_rule"){
  73. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  74. this.onBtnRoomRules();
  75. }else if(event.target.name == "btn_Debug"){
  76. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  77. this.showDebugUi();
  78. }
  79. },
  80. onBtnCancelTrustee() {
  81. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  82. var jsGameSence = ScenceManager.Instance().getGameComponent();
  83. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_CANCEL_TRUSTEE, {});
  84. },
  85. onBtnChangeTable() {
  86. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  87. if(cc.__TrucoChangeTableTime) {
  88. let nowTime = new Date().getTime();
  89. if(nowTime - cc.__TrucoChangeTableTime < 5000) {
  90. topTipMsg.showTopTipMsg(LanguageKey.t("pubWords.change_table_frequently"));
  91. return;
  92. }
  93. }
  94. if(CServerItem.get()) {
  95. CServerItem.get().SendChangeChair(-1, -1);
  96. cc.__TrucoChangeTableTime = new Date().getTime();
  97. }
  98. },
  99. onBtnReady:function() {
  100. this.node.getChildByName("TimeNode").active = false;
  101. this.node.getChildByName("FuncBtnNode").getChildByName("btn_Ready").active = false;
  102. this.node.getChildByName("FuncBtnNode").getChildByName("btn_ChangeTable").x=(0);
  103. var jsGameSence = ScenceManager.Instance().getGameComponent();
  104. jsGameSence.sendReady();
  105. jsGameSence.stopCountdown();
  106. },
  107. onBtnCloseEnd:function() {
  108. this.node.getChildByName("GameEnd").active = false;
  109. var jsGameSence = ScenceManager.Instance().getGameComponent();
  110. var selfPlayer = jsGameSence.getSelfLocalPlayer();
  111. if (!selfPlayer) {
  112. if (this.serverItem){
  113. GameManagerBase.Instance().connectGameServer(this.serverItem);
  114. }else{
  115. jsGameSence.backHome();
  116. jsGameSence.exitGame();
  117. }
  118. }else{
  119. //jsGameSence.autoReady();
  120. }
  121. },
  122. onClickToggle:function(toggle){
  123. },
  124. onButtonPlayerHeadClick: function(node) {
  125. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  126. var player = ScenceManager.Instance().getGameComponent().getPlayerByUserID(node.target.userID);
  127. ScenceManager.Instance().getGameComponent().showHeadInfo(player);
  128. },
  129. //聊天
  130. onBtnChat: function() {
  131. var self = this;
  132. /*cc.resources.load("GamePrefabs/Public/Chat", cc.Prefab, function (err, prefab) {
  133. if (!self._nodeChat) {
  134. self._nodeChat = cc.instantiate(prefab);
  135. self._nodeChat.parent = self.node;
  136. }
  137. var jsChat = self._nodeChat.getComponent("LYCChat");
  138. if (!jsChat) {
  139. self._nodeChat.addComponent("LYCChat");
  140. }
  141. self._nodeChat.getComponent("LYCChat").setChatNodeVisible(true);
  142. }); */
  143. },
  144. onBtnGameMeme:function(){
  145. var self = this;
  146. cc.resources.load("GamePrefabs/Public/GameMeme", cc.Prefab, function (err, prefab) {
  147. if (!self._GameMeme) {
  148. self._GameMeme = cc.instantiate(prefab);
  149. self._GameMeme.parent = self.node;
  150. }
  151. self._GameMeme.getComponent("GameMeme").show(self.memeSpr);
  152. });
  153. },
  154. initAr: function() {
  155. this.audioRecord = new sdk.TMGInterface();
  156. //参数分别为:appid,authkey,userid
  157. //测试用appid=1400089356, authkey=1cfbfd2a1a03a53e
  158. //正式用appid=1400158484, authkey=UDZY34l7geTfY3nP
  159. this.audioRecord.init("1400158484", "UDZY34l7geTfY3nP", ""+cc.vv.globalUserInfo.getUserID());
  160. // setMaxMessageLength
  161. var ar = this.audioRecord;
  162. var self = this;
  163. this.audioRecord.setEventCallback(function(iCode, jsData){
  164. var data = JSON.parse(jsData);
  165. Global.print("录音::"+JSON.stringify(data));
  166. if (typeof data != "object") return;
  167. if (data.result == 0) {
  168. switch (iCode) {
  169. case 5001:
  170. self.audioRecord.uploadRecordedFile(data.file_path);
  171. break;
  172. case 5002:
  173. self.SendTableVoice(data.file_id);
  174. break;
  175. case 5003:
  176. self.audioRecord.playRecordedFile(data.file_path);
  177. cc.vv.audioMgr.setBGMVolume(0, false, false);
  178. break;
  179. case 5004:
  180. cc.vv.audioMgr.setBGMVolume(cc.vv.audioMgr.bgmVolume, true, true);
  181. ScenceManager.Instance().getGameComponent().hideVoice();
  182. break;
  183. }
  184. } else {
  185. if (data.result == 4103) {
  186. //提示录音时间太短
  187. topTipMsg.showTopTipMsg("录音时间太短!");
  188. } else if (data.result == 4102) {
  189. //topTipMsg.showTopTipMsg("麦克风未授权!");
  190. //提示开启授权
  191. //cc.vv.reflection.reqPermissionRecordAudio();
  192. } else {
  193. topTipMsg.showTopTipMsg("刚才没听清,请再试一次(" + data.result + ")");
  194. }
  195. }
  196. });
  197. },
  198. getAr: function() {
  199. if (!this.audioRecord) {
  200. this.initAr();
  201. }
  202. return this.audioRecord;
  203. },
  204. onBtnExitToLobby:function(){
  205. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  206. var jsGameSence = ScenceManager.Instance().getGameComponent();
  207. jsGameSence.exitGameWhenPlaying();
  208. },
  209. onCloseTipNode() {
  210. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  211. this.node.getChildByName("tipNode").active = false;
  212. },
  213. onConformLeave() {
  214. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  215. var jsGameSence = ScenceManager.Instance().getGameComponent();
  216. jsGameSence.backHome();
  217. jsGameSence.exitGame();
  218. },
  219. onBtnDebug:function(event){
  220. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  221. var jsGameSence = ScenceManager.Instance().getGameComponent();
  222. if(!jsGameSence.getSceneState()){
  223. topTipMsg.showTopTipMsg("玩家不在游戏中");
  224. return;
  225. }
  226. var data = {};
  227. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_TEST_GETCARD,data);
  228. },
  229. showDebugUi:function(data){
  230. var jsGameSence = ScenceManager.Instance().getGameComponent();
  231. let self = this;
  232. UIHelper.showWaitNode("loadResSingle", "");
  233. UIHelper.loadRes("resourcesPrefab/ControlNode", cc.Prefab, (prefab) => {
  234. UIHelper.hideWaitNode();
  235. if (!self._DebugUI) {
  236. self._DebugUI = cc.instantiate(prefab);
  237. self._DebugUI.parent = self.node;
  238. self._DebugUI.zIndex=(100);
  239. }
  240. self._DebugUI.getComponent("TrucoControl").show();
  241. });
  242. },
  243. onBtnMenu() {
  244. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  245. this.upSetting();
  246. let rootNode = this.node.getChildByName("MenuNode").getChildByName("RootNode");
  247. let desX = rootNode.width/2-cc.winSize.width/2;
  248. rootNode.x = desX-800;
  249. this.node.getChildByName("MenuNode").active = true;
  250. rootNode.stopAllActions();
  251. cc.tween(rootNode)
  252. .to(0.2, {x:desX+30})
  253. .to(0.05, {x:desX})
  254. .start()
  255. },
  256. onBtnCloseMenu: function() {
  257. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  258. this.node.getChildByName("MenuNode").active = false;
  259. },
  260. onBtnRoomRules:function(){
  261. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  262. var jsGameSence = ScenceManager.Instance().getGameComponent();
  263. let rootNode = this.node.getChildByName("RuleNode").getChildByName("RootNode");
  264. let desX = cc.winSize.width/2-rootNode.width/2;
  265. rootNode.x = desX+800;
  266. this.node.getChildByName("RuleNode").active = true;
  267. let centerNode = rootNode.getChildByName("CenterNode");
  268. centerNode.getChildByName("r1").getComponent(cc.Label).string = Global.formatString2Score(jsGameSence._baseScore);
  269. centerNode.getChildByName("r3").getComponent(cc.Label).string = Global.formatString2Score(jsGameSence._baseScore*8);
  270. rootNode.stopAllActions();
  271. cc.tween(rootNode)
  272. .to(0.2, {x:desX-30})
  273. .to(0.05, {x:desX})
  274. .start()
  275. },
  276. onBtnCloseRule: function() {
  277. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  278. this.node.getChildByName("RuleNode").active = false;
  279. },
  280. onBtnBangZhu() {
  281. var self = this;
  282. cc.resources.load("GamePrefabs/homescence/rule", cc.Prefab, function (err, prefab) {
  283. if (!self._rule) {
  284. self._rule = cc.instantiate(prefab);
  285. self._rule.parent = self.node;
  286. }
  287. self._rule.getComponent("homeRule").showGameRule(5);
  288. });
  289. },
  290. upSetting() {
  291. let bMusic = cc.vv.audioMgr.bgmVolume > 0;
  292. let bEffect = cc.vv.audioMgr.sfxVolume > 0;
  293. let bShake = cc.vv.audioMgr.shakeOpen > 0;
  294. this.musicBtn.getChildByName("on").active = bMusic;
  295. this.musicBtn.getChildByName("off").active = !bMusic;
  296. this.effectBtn.getChildByName("on").active = bEffect;
  297. this.effectBtn.getChildByName("off").active = !bEffect;
  298. this.shakeBtn.getChildByName("on").active = bShake;
  299. this.shakeBtn.getChildByName("off").active = !bShake;
  300. },
  301. onClickMusic(){
  302. // cc.vv.audioMgr.bgmVolume = cc.vv.audioMgr.bgmVolume>0?0:1;
  303. if(cc.vv.audioMgr.bgmVolume > 0) {
  304. cc.vv.audioMgr.setBGMVolume(0, false, true);
  305. }else{
  306. cc.vv.audioMgr.setBGMVolume(1, false, true);
  307. }
  308. this.upSetting();
  309. },
  310. onClickEffect(){
  311. // cc.vv.audioMgr.sfxVolume = cc.vv.audioMgr.sfxVolume>0?0:1;
  312. if(cc.vv.audioMgr.sfxVolume > 0) {
  313. cc.vv.audioMgr.setSFXVolume(0, false, true);
  314. }else{
  315. cc.vv.audioMgr.setSFXVolume(1, false, true);
  316. }
  317. this.upSetting();
  318. },
  319. onClickShake(){
  320. // cc.vv.audioMgr.shakeOpen = cc.vv.audioMgr.shakeOpen>0?0:1;
  321. if(cc.vv.audioMgr.shakeOpen > 0) {
  322. cc.vv.audioMgr.setShakeOpen(0);
  323. }else{
  324. cc.vv.audioMgr.setShakeOpen(1);
  325. }
  326. this.upSetting();
  327. },
  328. onBtnForceExit() {
  329. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  330. this._needSendCancel = false;
  331. this.node.getChildByName("exitNode").active = true;
  332. this.upForceExitLabel();
  333. },
  334. onCloseExitNode() {
  335. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  336. this.node.getChildByName("exitNode").active = false;
  337. },
  338. onShowSelfBQ() {
  339. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  340. this.node.getChildByName("selfNode").getComponent("TrucoEmoji").show();
  341. },
  342. showForceExit() {
  343. this._needSendCancel = true;
  344. this.node.getChildByName("exitNode").active = true;
  345. this.upForceExitLabel();
  346. },
  347. upForceExitLabel() {
  348. let s1 = "Você deseja ANULAR este jogo?";
  349. let s2 = "Caso seu colega de trabalho aceite,a partida ter-minará mais cedo. E sua equipe perderá fichas,prazer em confirmar";
  350. if(this._needSendCancel) {
  351. s1 = "Seu parceiro quer terminar o jogo,você concorda?";
  352. s2 = "Se acordado, o jogo terminará mais cedo e sua equipe perderá fichas.";
  353. }
  354. let root = this.node.getChildByName("exitNode").getChildByName("RootNode");
  355. root.getChildByName("title1").active = !this._needSendCancel;
  356. root.getChildByName("title2").active = this._needSendCancel;
  357. root.getChildByName("sure1").active = !this._needSendCancel;
  358. root.getChildByName("sure2").active = this._needSendCancel;
  359. root.getChildByName("tips1").getComponent(cc.Label).string = s1;
  360. root.getChildByName("tips2").getComponent(cc.Label).string = s2;
  361. },
  362. onClickForceSure() {
  363. ScenceManager.Instance().getGameComponent().SendGameSocketData(GameProtocol.SUB_C_FORCE_LEAVE,{cbChoose:1})
  364. this.node.getChildByName("exitNode").active = false;
  365. this.forceExitBtn.active = false;
  366. },
  367. onClickForceCancel() {
  368. if(this._needSendCancel) {
  369. ScenceManager.Instance().getGameComponent().SendGameSocketData(GameProtocol.SUB_C_FORCE_LEAVE,{cbChoose:2})
  370. this.forceExitBtn.active = false;
  371. }
  372. this.node.getChildByName("exitNode").active = false;
  373. },
  374. });