TrucoPlayer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. var GamePlayer = require("GamePlayer");
  2. var Define = require("Define");
  3. var Global = require("Global");
  4. var UIHelper = require("UIHelper");
  5. import { CServerItem } from 'CServerItem'
  6. import ScenceManager from 'HNScenceManager'
  7. var BaseDefine = require("BaseDefine");
  8. var SoundFun = require("TrucoSoundFun");
  9. var GameProtocol = require("TrucoGameProtocol");
  10. var ProtocolGameServer = require("ProtocolGameServer");
  11. const propConfig = require("propConfig");
  12. cc.Class({
  13. extends: GamePlayer,
  14. //extends: cc.Component,
  15. editor: {
  16. menu: 'Game/Truco/GamePlayer'
  17. },
  18. properties: {
  19. sprHeadIcon:cc.Sprite, //头像
  20. sprVipIcon:cc.Sprite, // VIP框
  21. btnHead:cc.Button,
  22. btnGift:cc.Button,
  23. sprEmptyHeadIcon:cc.Sprite,
  24. labScore:cc.Label, //分数
  25. labName:cc.Label, //昵称
  26. infoNode: cc.Node,
  27. cardsNode: cc.Node,
  28. OffineFlag:cc.Node, //离线标记
  29. TrusteeFlag: cc.Node,
  30. EmojiNode: cc.Node,
  31. trucoEmoNode: cc.Node,
  32. chatTxtNode: cc.Node,
  33. readyNode:cc.Node,
  34. trucoState:cc.Node,
  35. trucoNode: cc.Node,
  36. trucoChoose: cc.Node,
  37. outCardNode: cc.Node,
  38. _magicCardValue: -1,
  39. iIndex:-1,
  40. _CurGameScene:null,
  41. _myScore:0,
  42. },
  43. // LIFE-CYCLE CALLBACKS:
  44. onLoad: function() {
  45. this.handCardPos = [
  46. {x:[-104,6,115], y:[-303,-303,-303], angle:0, scale:0.55},
  47. {x:[-422,-422,-422], y:[22,-7,-36], angle:-90, scale:0.42},
  48. {x:[44,121,198], y:[295,295,295], angle:0, scale:0.4},
  49. {x:[418,418,418], y:[22,-7,-36], angle:90, scale:0.42},
  50. ];
  51. this.outCardStartPos = [
  52. {x:6, y:-303},
  53. {x:-422, y:-7},
  54. {x:121, y:295},
  55. {x:418, y:-7},
  56. ];
  57. this.outCardPos = [
  58. {x:6, y:-110, angle:0, scale:0.4},
  59. {x:-150, y:-7, angle:-70, scale:0.4},
  60. {x:6, y:110, angle:180, scale:0.4},
  61. {x:150, y:-7, angle:70, scale:0.4},
  62. ];
  63. this._isGameEnd = false;
  64. this._tenCardPosX = 724;
  65. this.needPlayScoreAni = false;
  66. this.trusteeState = 0;
  67. this._chatTxtTimes = 0;
  68. // this.sprVipIcon.node.width = 118;
  69. // this.sprVipIcon.node.height = 118;
  70. this.sprVipIcon.node.scale = 0.25;
  71. if(this.infoNode) {
  72. this.infoNode.active = false;
  73. this.loadMemeProp();
  74. }
  75. this.myHandCards = [];
  76. this.hideHandCard();
  77. this.hideTruco();
  78. this.hideTrucoChoose();
  79. UIHelper.addButtonListener(this.btnHead.node, this.node, "TrucoPlayer", "onClickeHead");
  80. UIHelper.addButtonListener(this.btnGift.node, this.node, "TrucoPlayer", "onClickeHead");
  81. },
  82. init: function(index) {
  83. },
  84. playerEnter: function(){
  85. Global.print("用户playerEnter")
  86. this.node.getChildByName("PlayerHead").active = true;
  87. // this.sprEmptyHeadIcon.node.active = false;
  88. this.sprHeadIcon.node.active = true;
  89. this.btnHead.node.userID = this.getUserID();
  90. this.btnHead.node.gameID = this.getGameID();
  91. this.btnGift.node.userID = this.getUserID();
  92. this.btnGift.node.gameID = this.getGameID();
  93. this.node.active = true;
  94. this.needPlayScoreAni = false;
  95. this._bMingCard = false;
  96. this.changeTrusteeState(0);
  97. this.setScore(this.getUserScore());
  98. this.hideTruco();
  99. this.hideTrucoChoose();
  100. this.hideTimeNode();
  101. this.node.getChildByName("AndroidTip").active = (this.getAndroidTip()==1 && BaseDefine.LOCALTEST);
  102. },
  103. playerLeave:function() {
  104. this.labName.string = "";
  105. this.sprEmptyHeadIcon.node.active = true;
  106. this.sprHeadIcon.node.active = false;
  107. this.node.getChildByName("AndroidTip").active = false;
  108. this.hideTimeNode();
  109. this.upPlayerInfo();
  110. this.upPlayerHead();
  111. this.upPlayerState();
  112. Global.print("用户离开了")
  113. this.sprVipIcon.node.active = false;
  114. this.node.active = false;
  115. },
  116. upPlayer:function(show)
  117. {
  118. this.upPlayerInfo();
  119. this.upPlayerHead();
  120. this.node.active = show;
  121. },
  122. upPlayerState: function() {
  123. this.setOffline(this.getUserStatus() == Define.US_OFFLINE);
  124. },
  125. upPlayerInfo: function() {
  126. if (this.getNickName() != "") {
  127. var str = this.getNickName();
  128. let len = 6;
  129. this.labName.string = Global.subStr(str,len);
  130. }
  131. this.setScore(this.getUserScore());
  132. },
  133. setNameLabelActive:function(show){
  134. },
  135. setVIPActive:function(show){
  136. this.sprVipIcon.node.active = show;
  137. },
  138. getnickName: function()
  139. {
  140. return this.labName.string
  141. },
  142. upPlayerHead: function() {
  143. if (this.getHeadHttp() != "") {
  144. // 设置头像
  145. this.sprHeadIcon.getComponent("urlSpriteFrame").loadAvatar(this.getHeadHttp(),null, {width:120, height:120});
  146. } else {
  147. this.sprHeadIcon.spriteFrame = this.spriteFrame;
  148. this.sprHeadIcon.node.width = 120;
  149. this.sprHeadIcon.node.height = 120;
  150. }
  151. this.sprHeadIcon.node.active = true;
  152. let vipID = this.getVIPLevel();
  153. if(vipID >= 4 && vipID <= 7) {
  154. this.sprVipIcon.spriteFrame = cc.vv.globalUserInfo.getVIPSpriteFrameByFaceID(vipID);
  155. this.sprVipIcon.node.active = true;
  156. }else{
  157. this.sprVipIcon.node.active = false;
  158. }
  159. },
  160. getHeadSpriteFrame() {
  161. return this.sprHeadIcon.spriteFrame;
  162. },
  163. getVIPSpriteFrame() {
  164. return this.sprVipIcon.spriteFrame;
  165. },
  166. //游戏结束,隐藏VIP框
  167. hideVIP() {
  168. this.sprVipIcon.node.active = false;
  169. },
  170. getHeadIcon: function(ret) {
  171. if(ret)
  172. {
  173. return this.sprHeadIcon.spriteFrame;
  174. }
  175. if (this.getHeadHttp() != "") {
  176. return this.sprHeadIcon.spriteFrame;
  177. }
  178. return null;
  179. },
  180. getIndex: function() {
  181. return this.iIndex;
  182. },
  183. setScore: function(score) {
  184. this._myScore = score;
  185. var strScore = this.changeScore(Global.formatString2Score(score));
  186. this.labScore.string = strScore;
  187. },
  188. getScore: function() {
  189. return this._myScore;
  190. },
  191. setOffline: function(isOffline) {
  192. this.OffineFlag.active = isOffline;
  193. if (isOffline) {
  194. this.TrusteeFlag.active = false;
  195. }else{
  196. this.changeTrusteeState(this.trusteeState);
  197. }
  198. },
  199. setEndGameState(bbb) {
  200. this.needPlayScoreAni = bbb;
  201. },
  202. getEndGameState() {
  203. return this.needPlayScoreAni;
  204. },
  205. setEndGameScore(score) {
  206. this.needPlayScore = score;
  207. },
  208. getEndGameScore() {
  209. return this.needPlayScore;
  210. },
  211. changeTrusteeState(state) {
  212. this.trusteeState = state;
  213. this.TrusteeFlag.active = state;
  214. },
  215. playTruco(score) {
  216. this.trucoNode.active = true;
  217. this.trucoNode.getChildByName("bg").getChildByName("label").getComponent(cc.Label).string = '+'+score;
  218. let ani = this.trucoNode.getChildByName("ani").getComponent(sp.Skeleton);
  219. ani.clearTracks();
  220. ani.setAnimation(0, "a1", false);
  221. },
  222. hideTruco() {
  223. this.trucoNode.active = false;
  224. },
  225. showTrucoChoose(type) {
  226. this.trucoChoose.active = true;
  227. let ani = this.trucoChoose.getComponent(sp.Skeleton);
  228. ani.clearTracks();
  229. if(type == 1) {
  230. ani.setAnimation(0, "b1", false);
  231. ani.addAnimation(0, "b2", true);
  232. }else{
  233. ani.setAnimation(0, "a1", false);
  234. ani.addAnimation(0, "a2", true);
  235. }
  236. this.trucoState.active = true;
  237. this.trucoState.getChildByName("ace").active = type == 1;
  238. this.trucoState.getChildByName("cor").active = type == 2;
  239. },
  240. hideTrucoChoose() {
  241. this.trucoChoose.active = false;
  242. this.trucoState.active = false;
  243. },
  244. showPe: function(isPe) {
  245. this.readyNode.active = isPe;
  246. },
  247. playTimeAction: function(UserLeftTime){
  248. this.node.getChildByName("TimeNode").active = true;
  249. this.node.getChildByName("TimeNode").getComponent("TrucoTimePointer").playTimeAction(UserLeftTime,this.iIndex==0);
  250. },
  251. hideTimeNode:function(){
  252. this.node.getChildByName("TimeNode").active = false;
  253. this.node.getChildByName("TimeNode").getComponent("TrucoTimePointer").stopTimeSchedule();
  254. },
  255. start () {
  256. },
  257. update(dt) {
  258. if(this._chatTxtTimes > 0) {
  259. this._chatTxtTimes-=dt;
  260. if(this._chatTxtTimes <= 0) {
  261. this.hideChatNode();
  262. }
  263. }
  264. },
  265. hideChatNode() {
  266. this.chatTxtNode.stopAllActions()
  267. cc.tween(this.chatTxtNode)
  268. .to(0.2, {scale: 0})
  269. .call(()=>{
  270. this.chatTxtNode.active = false;
  271. })
  272. .start()
  273. },
  274. showTalkState: function(data){
  275. if (data.Type == ProtocolGameServer.CMD_GR_C_TableTalk.TALK_TYPE){
  276. }else if (data.Type == ProtocolGameServer.CMD_GR_C_TableTalk.TYPE_WORD){
  277. if(data.String == ""){
  278. return;
  279. }
  280. this.chatTxtNode.stopAllActions()
  281. this.chatTxtNode.scale = 0;
  282. this.chatTxtNode.active = true;
  283. this.chatTxtNode.getChildByName("msg").getComponent(cc.Label).string = data.String;
  284. cc.tween(this.chatTxtNode)
  285. .to(0.2, {scale: 1})
  286. .start()
  287. this._chatTxtTimes = 3;
  288. }else if (data.Type == ProtocolGameServer.CMD_GR_C_TableTalk.TYPE_DEFINE){
  289. }else if (data.Type == ProtocolGameServer.CMD_GR_C_TableTalk.TYPE_BIAOQING)
  290. {
  291. if(data.String == ""){
  292. return;
  293. }
  294. let memeIdx = parseInt(data.String);
  295. if(memeIdx >= 100){
  296. this.trucoEmoNode.active = true;
  297. var Ani = this.trucoEmoNode.getComponent(sp.Skeleton);
  298. Ani.setAnimation(0,"a"+(memeIdx-100),false);
  299. return;
  300. }
  301. this.EmojiNode.active = true;
  302. var Ani = this.EmojiNode.getComponent(sp.Skeleton);
  303. Ani.setAnimation(0,"a"+memeIdx,false);
  304. }
  305. },
  306. rePlayHandCardShow() {
  307. if(this.iIndex <= 0) return;
  308. this.cardsNode.active = true;
  309. for(let i = 0; i < this.cardsNode.childrenCount; ++i) {
  310. this.cardsNode.children[i].position = this.handCardPos[this.iIndex];
  311. this.cardsNode.children[i].scale = 0.6;
  312. this.cardsNode.children[i].angle = 0;
  313. this.cardsNode.children[i].getComponent("TrucoCard").setId(0,0);
  314. this.cardsNode.children[i].active = true;
  315. }
  316. },
  317. getHandCardPos() {
  318. return this.handCardPos[this.iIndex];
  319. },
  320. hideHandCard() {
  321. if(!this.cardsNode) return;
  322. for(let i = 0; i < this.cardsNode.childrenCount; ++i) {
  323. this.cardsNode.children[i].stopAllActions();
  324. this.cardsNode.children[i].x = 429+9;
  325. this.cardsNode.children[i].y = 231+9;
  326. this.cardsNode.children[i].scale = 0.43;
  327. if(this.cardsNode.children[i].is3DNode) {
  328. this.cardsNode.children[i].eulerAngles = cc.v3(0,0,0);
  329. }else{
  330. this.cardsNode.children[i].angle = 0;
  331. }
  332. this.cardsNode.children[i].cardValue = 0;
  333. this.cardsNode.children[i].getComponent("TrucoCard").setId(0,0);
  334. this.cardsNode.children[i].active = false;
  335. }
  336. this.outCardNode.getChildByName("diAni").active = false;
  337. this.outCardNode.getChildByName("ani").active = false;
  338. this.outCardNode.active = false;
  339. this.cardsNode.active = true;
  340. // this.huAniNode.active = false;
  341. // this.myHandCards.splice(0);
  342. this.myHandCards = [];
  343. this.handCardCount = 0;
  344. this._bMingCard = false;
  345. },
  346. changeMingCard(bMing) {
  347. this._bMingCard = bMing;
  348. },
  349. getHandCardValue(idx) {
  350. if(this.handCards && this.handCards.length > idx) {
  351. return this.handCards[idx];
  352. }
  353. return 0;
  354. },
  355. resetHandCardPos() {
  356. if(!this.cardsNode) return;
  357. this.cardsNode.active = true;
  358. let config = this.handCardPos[this.iIndex];
  359. let cards = this.handCards;
  360. let count = this.handCardCount;
  361. for(let i = 0; i < this.cardsNode.childrenCount; ++i) {
  362. this.cardsNode.children[i].active = false;
  363. this.cardsNode.children[i].cardValue = cards?cards[i]:0;
  364. if(this.iIndex == 0)
  365. Global.print(this.cardsNode.children[i].cardValue)
  366. }
  367. if(this._bMingCard) {
  368. let cardIdx = 0;
  369. for(let i = 0; i < cards.length; ++i) {
  370. if(cards[i] > 0) {
  371. let card = this.cardsNode.children[cardIdx];
  372. if(card) {
  373. card.getComponent("TrucoCard").setId(cards[i], this._magicCardValue);
  374. card.scale = config.scale;
  375. card.x = config.x[cardIdx];
  376. card.y = config.y[cardIdx];
  377. card.angle = config.angle;
  378. card.active = true;
  379. cardIdx++;
  380. }
  381. }
  382. }
  383. }else{
  384. for(let i = 0; i < count; ++i) {
  385. let card = this.cardsNode.children[i];
  386. if(card) {
  387. card.getComponent("TrucoCard").setId(0,0);
  388. card.scale = config.scale;
  389. card.x = config.x[i];
  390. card.y = config.y[i];
  391. card.angle = config.angle;
  392. card.active = true;
  393. }
  394. }
  395. }
  396. },
  397. setMagicCardValue(val) {
  398. this._magicCardValue = val;
  399. },
  400. setHandCards(cards, count, needReset) {
  401. this.handCards = cards.slice(0);
  402. this.handCardCount = count;
  403. if(needReset)
  404. this.resetHandCardPos();
  405. return;
  406. let config = this.handCardPos[this.iIndex];
  407. if(this.iIndex == 0) {
  408. let cardIdx = 0;
  409. for(let i = 0; i < cards.length; ++i) {
  410. if(cards[i] > 0) {
  411. let card = this.cardsNode.children[cardIdx];
  412. if(card) {
  413. card.getComponent("TrucoCard").setId(cards[i],0);
  414. card.scale = config.scale;
  415. card.x = config.x[cardIdx];
  416. card.y = config.y[cardIdx];
  417. card.angle = config.angle;
  418. card.active = true;
  419. cardIdx++;
  420. }
  421. }
  422. }
  423. }else{
  424. for(let i = 0; i < count; ++i) {
  425. let card = this.cardsNode.children[i];
  426. if(card) {
  427. card.getComponent("TrucoCard").setId(0,0);
  428. card.scale = config.scale;
  429. card.x = config.x[i];
  430. card.y = config.y[i];
  431. card.angle = config.angle;
  432. card.active = true;
  433. }
  434. }
  435. }
  436. },
  437. oneCardMove(cardIdx) {
  438. let config = this.handCardPos[this.iIndex];
  439. if(!config) {return;}
  440. let card = this.cardsNode.children[cardIdx];
  441. this.handCardCount = 3;
  442. if(card) {
  443. card.active = true;
  444. cc.tween(card)
  445. .to(0.2, {x: config.x[cardIdx], y:config.y[cardIdx], scale: config.scale, angle: config.angle})
  446. .start()
  447. }
  448. },
  449. outOneCard(cardid, magicCard) {
  450. this._outCard = cardid;
  451. if(this.handCards) {
  452. for(let i = 0; i < this.handCards.length; ++i) {
  453. if(this.handCards[i] == cardid) {
  454. this.handCards.splice(i, 1);
  455. break;
  456. }
  457. }
  458. // }else{
  459. // this.handCards.splice(0, 1);
  460. }
  461. this.handCardCount--;
  462. this.resetHandCardPos();
  463. this.showOutCard(cardid, magicCard);
  464. // let card = this.cardsNode.children[2];
  465. // if(card) {
  466. // card.scale = outConfig.scale;
  467. // card.x = outConfig.x;
  468. // card.y = outConfig.y;
  469. // card.angle = outConfig.angle;
  470. // card.active = true;
  471. // card.getComponent("TrucoCard").setId(cardid, magicCard);
  472. // }
  473. },
  474. showOutCard(cardid, magicCard) {
  475. if(cardid <= 0) {return;}
  476. let outConfig = this.outCardPos[this.iIndex];
  477. let startPos = this.outCardStartPos[this.iIndex];
  478. if(!outConfig) {return;}
  479. this._outCard = cardid;
  480. let card = this.outCardNode;
  481. if(card) {
  482. card.scale = 0;
  483. card.x = startPos.x;
  484. card.y = startPos.y;
  485. card.angle = outConfig.angle;
  486. card.active = true;
  487. card.getChildByName("card0").getComponent("TrucoCard").setId(cardid, magicCard);
  488. card.getChildByName("diAni").active = false;
  489. let ani = card.getChildByName("ani");
  490. ani.active = false;
  491. cc.tween(card)
  492. .to(0.1, {x: outConfig.x, y:outConfig.y, scale:outConfig.scale})
  493. .call(()=>{
  494. ani.active = card.getChildByName("card0").getComponent("TrucoCard").isJoker();
  495. ani.getComponent(sp.Skeleton).setAnimation(0, "a1", false);
  496. })
  497. .start();
  498. // ani.active = card.getChildByName("card0").getComponent("TrucoCard").isJoker();
  499. // ani.getComponent(sp.Skeleton).setAnimation(0, "a1", false);
  500. }
  501. },
  502. getOutCard() {
  503. return this._outCard;
  504. },
  505. getHandCards() {
  506. return this.myHandCards;
  507. },
  508. playLightCardAni() {
  509. let card = this.outCardNode;
  510. if(card) {
  511. card.active = true;
  512. let ani = card.getChildByName("diAni");
  513. ani.active = true;
  514. ani.getComponent(sp.Skeleton).setAnimation(0, "a1", false);
  515. }
  516. },
  517. outCardMoveToTrush() {
  518. let card = this.outCardNode;
  519. if(card) {
  520. card.active = true;
  521. card.stopAllActions();
  522. cc.tween(card)
  523. .to(0.3, {x:438, y:240, scale:0})
  524. .start()
  525. }
  526. },
  527. loadMemeProp: function() {
  528. let memePropConfig = Array.from(propConfig.getMemePropConfig());
  529. cc.resources.loadDir("GameTextures/Public/MemeProp", cc.SpriteFrame, (error, assets) => {
  530. if(error) {
  531. Global.print("loadRes:: error:" + error.message);
  532. } else {
  533. for(let i = 0; i < memePropConfig.length; ++i) {
  534. let config = memePropConfig[i]
  535. let asset = assets.find(asset => asset.name == config.description);
  536. if(!asset) {continue;}
  537. let node = this.infoNode.children[i];
  538. node.name = config.id.toString();
  539. node.getChildByName("img").getComponent(cc.Sprite).spriteFrame = asset;
  540. UIHelper.addButtonListener(node, this.node, "TrucoPlayer", "onClickMemePropItem");
  541. }
  542. }
  543. })
  544. },
  545. onClickMemePropItem: function(event) {
  546. let item = event.target;
  547. let id = item.name;
  548. ScenceManager.Instance().getGameComponent().useProp(id, 1, this.getUserID());
  549. this.playTimePointerAni();
  550. Global.print("GameHeadInfo:: 使用互动表情:" + id);
  551. this.hideInfoNode();
  552. },
  553. onClickeHead() {
  554. if(!this.infoNode || this.iIndex == 0) {
  555. return;
  556. }
  557. ScenceManager.Instance().getGameComponent().hideAllMene();
  558. this.infoNode.active = true;
  559. },
  560. hideInfoNode() {
  561. if(!this.infoNode || this.iIndex == 0) {
  562. return;
  563. }
  564. this.infoNode.active = false;
  565. },
  566. playTimePointerAni: function() {
  567. const TIME_POINT = 5; // 时钟总时间
  568. for(let item of this.infoNode.children) {
  569. item.getComponent(cc.Button).interactable = false;
  570. let timeNode = item.getChildByName("timePointer");
  571. let timeLabel = timeNode.getChildByName("label").getComponent(cc.Label);
  572. let timeSprite = timeNode.getChildByName("sprite").getComponent(cc.Sprite);
  573. let time = TIME_POINT;
  574. timeLabel.string = TIME_POINT;
  575. timeSprite.fillRange = 1;
  576. if(timeNode.active) {return;}
  577. timeNode.active = true;
  578. let timePointFunc = (dt) => {
  579. time -= dt; // dt:与上次调用间隔时间
  580. if(time > 0) {
  581. timeLabel.string = Math.floor(time);
  582. timeSprite.fillRange = (time / TIME_POINT);
  583. } else {
  584. item.getComponent(cc.Button).interactable = true;
  585. timeNode.active = false;
  586. this.unschedule(timePointFunc);
  587. }
  588. }
  589. this.schedule(timePointFunc, 0.1);
  590. }
  591. },
  592. });