CachetaFuncBtnMgr.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import ScenceManager from 'HNScenceManager'
  2. import GameManagerBase from 'GameManagerBase'
  3. var Global = require("Global");
  4. var GameProtocol = require("CachetaGameProtocol");
  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("CachetaSoundFun");
  12. var BaseDefine = require("BaseDefine");
  13. cc.Class({
  14. extends: cc.Component,
  15. editor: {
  16. menu: 'Game/Cacheta/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_ChangeTable"));
  27. // this.initButtonHandler(funcBtnNode.getChildByName("btn_Ready"));
  28. this.initButtonHandler(funcBtnNode.getChildByName("btn_Listener").getChildByName("btn_Listener"));
  29. this.initButtonHandler(funcBtnNode.getChildByName("btn_Finish").getChildByName("btn_Finish"));
  30. this.initButtonHandler(funcBtnNode.getChildByName("btn_Menu"));
  31. this.initButtonHandler(funcBtnNode.getChildByName("btn_Tip"));
  32. this.initButtonHandler(funcBtnNode.getChildByName("btn_Monte"));
  33. this.initButtonHandler(funcBtnNode.getChildByName("btn_Lixo"));
  34. this.initButtonHandler(funcBtnNode.getChildByName("btn_Descartar"));
  35. let menuNode = funcBtnNode.getChildByName("MenuNode");
  36. this.initButtonHandler(menuNode.getChildByName("btn_Setting"));
  37. this.initButtonHandler(menuNode.getChildByName("btn_Rule"));
  38. this.initButtonHandler(menuNode.getChildByName("btn_Exit"));
  39. var EventNode = this.node.getChildByName("Cards").getChildByName("EventNode");
  40. this.initButtonHandler(EventNode.getChildByName("LeftNode"));
  41. this.initButtonHandler(EventNode.getChildByName("RightNode"));
  42. this.initButtonHandler(EventNode.getChildByName("RightNode").getChildByName("RightArrow"));
  43. // var GameEndNode = this.node.getChildByName("GameEnd").getChildByName("RootNode");
  44. // this.initButtonHandler(GameEndNode.getChildByName("btn_EndClose"));
  45. // this.initButtonHandler(GameEndNode.getChildByName("btn_EndExit"));
  46. // this.initButtonHandler(GameEndNode.getChildByName("btn_EndNext"));
  47. this.initButtonHandler(funcBtnNode.getChildByName("btn_Sort"));
  48. funcBtnNode.getChildByName("btn_Debug").active = false//BaseDefine.LOCALTEST;
  49. this.initButtonHandler(funcBtnNode.getChildByName("btn_Debug"));
  50. this._refreshing = false;
  51. },
  52. start () {
  53. this.serverItem = GameManagerBase.Instance().getGameServerItem();
  54. },
  55. //抓牌
  56. sendGetCard:function(isDarkCard){
  57. var jsGameSence = ScenceManager.Instance().getGameComponent();
  58. if(jsGameSence._isPlayingAni){
  59. return;
  60. }
  61. // if(!isDarkCard && jsGameSence.isRightCardMagic()) {
  62. // return;
  63. // }
  64. jsGameSence._isPlayingAni = true;
  65. var data = {};
  66. data.bGetTableCard = isDarkCard;//(true=暗牌堆,false=明牌堆)
  67. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_GET_CARD,data);
  68. },
  69. //出牌
  70. sendOutCard:function(cardData,groupData,bFinish){
  71. var jsGameSence = ScenceManager.Instance().getGameComponent();
  72. if(jsGameSence._isPlayingAni){
  73. return;
  74. }
  75. jsGameSence._isPlayingAni = true;
  76. var data = {};
  77. data.cbOutCardData = cardData;
  78. data.tagCardTeam = {};
  79. data.tagCardTeam.cbTeamCount = groupData.length;
  80. data.tagCardTeam.tagTeamDataArr = [];
  81. for(let i = 0;i<GameProtocol.HAND_CARD_COUNT+1;i++){
  82. data.tagCardTeam.tagTeamDataArr[i] = {};
  83. data.tagCardTeam.tagTeamDataArr[i].cbCardType = 0;
  84. data.tagCardTeam.tagTeamDataArr[i].lLostScore = 0;
  85. data.tagCardTeam.tagTeamDataArr[i].cbCardCount = 0;
  86. data.tagCardTeam.tagTeamDataArr[i].cbCardDataArr = [];
  87. if(i < groupData.length){
  88. data.tagCardTeam.tagTeamDataArr[i].cbCardCount = groupData[i].length;
  89. for(let j = 0;j<GameProtocol.HAND_CARD_COUNT+1;j++){
  90. if(j < groupData[i].length){
  91. data.tagCardTeam.tagTeamDataArr[i].cbCardDataArr.push(groupData[i][j]);
  92. }else{
  93. data.tagCardTeam.tagTeamDataArr[i].cbCardDataArr.push(0);
  94. }
  95. }
  96. }else{
  97. for(let j = 0;j<GameProtocol.HAND_CARD_COUNT+1;j++){
  98. data.tagCardTeam.tagTeamDataArr[i].cbCardDataArr.push(0);
  99. }
  100. }
  101. }
  102. data.bFinishSlotRequest = bFinish;
  103. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_OUT_CARD,data);
  104. },
  105. //按钮事件
  106. initButtonHandler: function (btn,opt) {
  107. if(btn){
  108. UIHelper.addButtonListener(btn, this.node, "CachetaFuncBtnMgr", "onBtnClicked",opt);
  109. }
  110. },
  111. onBtnClicked: function (event,opt) {
  112. var jsGameSence = ScenceManager.Instance().getGameComponent();
  113. if(event.target.name == "LeftNode"){//暗牌堆
  114. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  115. if(jsGameSence.isTurnSelf() && jsGameSence._canGetCard){
  116. this.sendGetCard(true);
  117. }
  118. }else if(event.target.name == "RightNode"){//明牌堆
  119. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  120. if(jsGameSence.isTurnSelf() && jsGameSence._canGetCard && !jsGameSence.isRightCardMagic()){
  121. this.sendGetCard(false);
  122. }else if(jsGameSence.isRightCardMagic()){
  123. topTipMsg.showTopTipMsg("Cacheta.not_pick_card");
  124. }
  125. }else if(event.target.name == "RightArrow"){
  126. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  127. jsGameSence.OnCardRecord();
  128. // var data = {};
  129. // jsGameSence.SendGameSocketData(GameProtocol.SUB_C_MING_CARD,data);
  130. }
  131. else if(event.target.name == "btn_Listener"){
  132. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  133. jsGameSence.OnListener();
  134. }else if(event.target.name == "btn_Finish"){
  135. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  136. jsGameSence.OnFinish();
  137. }
  138. else if(event.target.name == "btn_Monte"){//牌堆区
  139. SoundFun.playEffectEX("open");
  140. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_GET_CARD,{bGetTableCard: 1})
  141. }else if(event.target.name == "btn_Lixo"){//垃圾区
  142. SoundFun.playEffectEX("open");
  143. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_GET_CARD,{bGetTableCard: 0})
  144. }else if(event.target.name == "btn_Descartar"){//弃牌
  145. SoundFun.playEffectEX("open");
  146. // this.onBtnCloseEnd();
  147. jsGameSence.outCard();
  148. }
  149. else if(event.target.name == "btn_Menu"){
  150. SoundFun.playEffectEX("open");
  151. this.onBtnMenu();
  152. }
  153. else if(event.target.name == "btn_Sort"){
  154. SoundFun.playEffectEX(SoundFun.EX_BUTTON);
  155. this.onClickRefresh();
  156. // jsGameSence.refreshHandCardsGroup();
  157. }else if(event.target.name == "btn_Setting"){
  158. SoundFun.playEffectEX("open");
  159. this.onBtnGameSettings();
  160. }else if(event.target.name == "btn_Rule"){
  161. SoundFun.playEffectEX("open");
  162. this.onBtnRoomRules();
  163. }else if(event.target.name == "btn_Exit"){
  164. this.onBtnExitToLobby();
  165. // HomeSoundFun.playEffectEX("open");
  166. // this.node.getChildByName("RuleNode").active = true;
  167. }else if(event.target.name == "btn_Debug"){
  168. SoundFun.playEffectEX("open");
  169. this.showDebugUi();
  170. }
  171. },
  172. onClickRefresh() {
  173. if(this._refreshing) {
  174. return;
  175. }
  176. SoundFun.playEffectEX("open");
  177. this._refreshing = true;
  178. ScenceManager.Instance().getGameComponent().refreshHandCardsGroup(true);
  179. let node = this.node.getChildByName("FuncBtnNode").getChildByName("btn_Sort");
  180. node.stopAllActions();
  181. cc.tween(node)
  182. // .by(0.5, {angle: 360})
  183. .delay(0.5)
  184. .call(()=>{
  185. this._refreshing = false;
  186. })
  187. .start()
  188. },
  189. refreshSortState(bValue) {
  190. let node = this.node.getChildByName("FuncBtnNode").getChildByName("btn_Sort");
  191. node.getChildByName("value").active = bValue;
  192. node.getChildByName("color").active = !bValue;
  193. },
  194. onBtnCancelTrustee() {
  195. SoundFun.playEffectEX("open");
  196. var jsGameSence = ScenceManager.Instance().getGameComponent();
  197. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_CANCEL_TRUSTEE, {});
  198. },
  199. onBtnChangeTable() {
  200. SoundFun.playEffectEX("open");
  201. if(cc.__cachetaChangeTableTime) {
  202. let nowTime = new Date().getTime();
  203. if(nowTime - cc.__cachetaChangeTableTime < 5000) {
  204. topTipMsg.showTopTipMsg(LanguageKey.t("pubWords.change_table_frequently"));
  205. return;
  206. }
  207. }
  208. if(CServerItem.get()) {
  209. CServerItem.get().SendChangeChair(-1, -1);
  210. cc.__cachetaChangeTableTime = new Date().getTime();
  211. }
  212. },
  213. onBtnReady:function() {
  214. this.node.getChildByName("TimeNode").active = false;
  215. this.node.getChildByName("FuncBtnNode").getChildByName("btn_Ready").active = false;
  216. this.node.getChildByName("FuncBtnNode").getChildByName("btn_ChangeTable").x=(0);
  217. var jsGameSence = ScenceManager.Instance().getGameComponent();
  218. jsGameSence.sendReady();
  219. jsGameSence.stopCountdown();
  220. },
  221. onBtnCloseEnd:function() {
  222. this.node.getChildByName("GameEnd").active = false;
  223. var jsGameSence = ScenceManager.Instance().getGameComponent();
  224. var selfPlayer = jsGameSence.getSelfLocalPlayer();
  225. if (!selfPlayer) {
  226. if (this.serverItem){
  227. GameManagerBase.Instance().connectGameServer(this.serverItem);
  228. }else{
  229. jsGameSence.backHome();
  230. jsGameSence.exitGame();
  231. }
  232. }else{
  233. //jsGameSence.autoReady();
  234. }
  235. },
  236. onClickToggle:function(toggle){
  237. },
  238. onButtonPlayerHeadClick: function(node) {
  239. UIHelper.playButtonEffect("open");
  240. var player = ScenceManager.Instance().getGameComponent().getPlayerByUserID(node.target.userID);
  241. ScenceManager.Instance().getGameComponent().showHeadInfo(player);
  242. },
  243. //聊天
  244. onBtnChat: function() {
  245. var self = this;
  246. /*cc.resources.load("GamePrefabs/Public/Chat", cc.Prefab, function (err, prefab) {
  247. if (!self._nodeChat) {
  248. self._nodeChat = cc.instantiate(prefab);
  249. self._nodeChat.parent = self.node;
  250. }
  251. var jsChat = self._nodeChat.getComponent("LYCChat");
  252. if (!jsChat) {
  253. self._nodeChat.addComponent("LYCChat");
  254. }
  255. self._nodeChat.getComponent("LYCChat").setChatNodeVisible(true);
  256. }); */
  257. },
  258. onBtnGameMeme:function(){
  259. var self = this;
  260. cc.resources.load("GamePrefabs/Public/GameMeme", cc.Prefab, function (err, prefab) {
  261. if (!self._GameMeme) {
  262. self._GameMeme = cc.instantiate(prefab);
  263. self._GameMeme.parent = self.node;
  264. }
  265. self._GameMeme.getComponent("GameMeme").show(self.memeSpr);
  266. });
  267. },
  268. initAr: function() {
  269. this.audioRecord = new sdk.TMGInterface();
  270. //参数分别为:appid,authkey,userid
  271. //测试用appid=1400089356, authkey=1cfbfd2a1a03a53e
  272. //正式用appid=1400158484, authkey=UDZY34l7geTfY3nP
  273. this.audioRecord.init("1400158484", "UDZY34l7geTfY3nP", ""+cc.vv.globalUserInfo.getUserID());
  274. // setMaxMessageLength
  275. var ar = this.audioRecord;
  276. var self = this;
  277. this.audioRecord.setEventCallback(function(iCode, jsData){
  278. var data = JSON.parse(jsData);
  279. Global.print("录音::"+JSON.stringify(data));
  280. if (typeof data != "object") return;
  281. if (data.result == 0) {
  282. switch (iCode) {
  283. case 5001:
  284. self.audioRecord.uploadRecordedFile(data.file_path);
  285. break;
  286. case 5002:
  287. self.SendTableVoice(data.file_id);
  288. break;
  289. case 5003:
  290. self.audioRecord.playRecordedFile(data.file_path);
  291. cc.vv.audioMgr.setBGMVolume(0, false, false);
  292. break;
  293. case 5004:
  294. cc.vv.audioMgr.setBGMVolume(cc.vv.audioMgr.bgmVolume, true, true);
  295. ScenceManager.Instance().getGameComponent().hideVoice();
  296. break;
  297. }
  298. } else {
  299. if (data.result == 4103) {
  300. //提示录音时间太短
  301. topTipMsg.showTopTipMsg("录音时间太短!");
  302. } else if (data.result == 4102) {
  303. //topTipMsg.showTopTipMsg("麦克风未授权!");
  304. //提示开启授权
  305. //cc.vv.reflection.reqPermissionRecordAudio();
  306. } else {
  307. topTipMsg.showTopTipMsg("刚才没听清,请再试一次(" + data.result + ")");
  308. }
  309. }
  310. });
  311. },
  312. getAr: function() {
  313. if (!this.audioRecord) {
  314. this.initAr();
  315. }
  316. return this.audioRecord;
  317. },
  318. hideMenu() {
  319. let menuNode = this.node.getChildByName("FuncBtnNode").getChildByName("MenuNode");
  320. this.menuOpenState = false;
  321. let menuBtn = this.node.getChildByName("FuncBtnNode").getChildByName("btn_Menu");
  322. menuBtn.stopAllActions();
  323. cc.tween(menuBtn)
  324. .to(0.1, {angle: 0})
  325. .start()
  326. menuNode.stopAllActions();
  327. cc.tween(menuNode)
  328. .to(0.1, {scaleY: 0})
  329. .start()
  330. },
  331. //menuNode
  332. onBtnMenu: function(){
  333. SoundFun.playEffectEX("open");
  334. // let menuNode = this.node.getChildByName("FuncBtnNode").getChildByName("MenuNode");
  335. // this.menuOpenState = !this.menuOpenState;
  336. // let menuBtn = this.node.getChildByName("FuncBtnNode").getChildByName("btn_Menu");
  337. // menuBtn.stopAllActions();
  338. // cc.tween(menuBtn)
  339. // .to(0.1, {angle: this.menuOpenState?90:0})
  340. // .start()
  341. // menuNode.stopAllActions();
  342. // cc.tween(menuNode)
  343. // .to(0.1, {scaleY: this.menuOpenState?1:0})
  344. // .start()
  345. UIHelper.loadRes('GamePrefabs/Public/GameMenu', cc.Prefab, (prefab)=>{
  346. if (!this._ruleNode) {
  347. this._ruleNode = cc.instantiate(prefab);
  348. this._ruleNode.parent = this.node.getChildByName("MenuNode");
  349. }
  350. this._ruleNode.getComponent("GameMenu").show();
  351. this._ruleNode.getComponent("GameMenu").onBtnBangZhu = ()=>{
  352. this.onBtnHowToPlay();
  353. }
  354. })
  355. },
  356. onBtnExitToLobby:function(){
  357. SoundFun.playEffectEX("open");
  358. var jsGameSence = ScenceManager.Instance().getGameComponent();
  359. jsGameSence.exitGameWhenPlaying();
  360. },
  361. onBtnRoomRules:function(){
  362. SoundFun.playEffectEX("open");
  363. this.node.getChildByName("RuleNode").active = true;
  364. },
  365. onBtnGameSettings: function(){
  366. SoundFun.playEffectEX("open");
  367. var self = this;
  368. UIHelper.showWaitNode("loadResSingle", "");
  369. cc.resources.load("GamePrefabs/homescence/set", cc.Prefab, function (err, prefab) {
  370. UIHelper.hideWaitNode();
  371. if (!self._set) {
  372. self._set = cc.instantiate(prefab);
  373. self._set.parent = self.node;
  374. self._set.zIndex=(100);
  375. }
  376. self._set.getComponent("homeSet").showSet();
  377. self._set.getComponent("homeSet").hideNode();
  378. });
  379. },
  380. onBtnHowToPlay: function(){
  381. if(this._ruleNode) {
  382. this._ruleNode.active = false;
  383. }
  384. this.node.getChildByName("RuleNode").active = true;
  385. },
  386. onBtnDebug:function(event){
  387. SoundFun.playEffectEX("open");
  388. var jsGameSence = ScenceManager.Instance().getGameComponent();
  389. if(!jsGameSence.getSceneState()){
  390. topTipMsg.showTopTipMsg("玩家不在游戏中");
  391. return;
  392. }
  393. var data = {};
  394. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_TEST_GETCARD,data);
  395. },
  396. showDebugUi:function(data){
  397. var jsGameSence = ScenceManager.Instance().getGameComponent();
  398. let self = this;
  399. UIHelper.showWaitNode("loadResSingle", "");
  400. UIHelper.loadRes("resourcesPrefab/ControlNode", cc.Prefab, (prefab) => {
  401. UIHelper.hideWaitNode();
  402. if (!self._DebugUI) {
  403. self._DebugUI = cc.instantiate(prefab);
  404. self._DebugUI.parent = self.node;
  405. self._DebugUI.zIndex=(100);
  406. }
  407. self._DebugUI.getComponent("CachetaControl").show();
  408. });
  409. },
  410. onChangeCard:function(data) {
  411. var jsGameSence = ScenceManager.Instance().getGameComponent();
  412. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_TEST_SETCARD,data);
  413. setTimeout(() => {
  414. var _data = {};
  415. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_GET_GROUP,_data);
  416. }, 500);
  417. },
  418. onBtnCloseRule: function() {
  419. SoundFun.playEffectEX("open");
  420. this.node.getChildByName("RuleNode").active = false;
  421. },
  422. onBtnClickGoRealRoom:function(){
  423. HomeSoundFun.playEffectEX("close");
  424. this.node.getChildByName("GoRealRoom").active = false;
  425. },
  426. OnBtnNoThanks:function(){
  427. HomeSoundFun.playEffectEX("close");
  428. this.node.getChildByName("GoRealRoom").active = false;
  429. }
  430. });