TrucoControl.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var UIHelper = require("UIHelper");
  2. var Global = require("Global");
  3. var topTipMsg = require("topTipMsg");
  4. var GameProtocol = require("TrucoGameProtocol");
  5. var GameLogic = require("TrucoGameLogic");
  6. import ScenceManager from 'HNScenceManager';
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. cardNode: cc.Node,
  11. cardClone: cc.Node,
  12. },
  13. onLoad() {
  14. this.cardsInfo = [
  15. 0x32,0x22,0x12,0x02,0x32,0x22,0x12,0x02,
  16. 0x31,0x21,0x11,0x01,0x31,0x21,0x11,0x01,
  17. 0x3D,0x2D,0x1D,0x0D,0x3D,0x2D,0x1D,0x0D,
  18. 0x3C,0x2C,0x1C,0x0C,0x3C,0x2C,0x1C,0x0C,
  19. 0x3B,0x2B,0x1B,0x0B,0x3B,0x2B,0x1B,0x0B,
  20. 0x3A,0x2A,0x1A,0x0A,0x3A,0x2A,0x1A,0x0A,
  21. 0x39,0x29,0x19,0x09,0x39,0x29,0x19,0x09,
  22. 0x38,0x28,0x18,0x08,0x38,0x28,0x18,0x08,
  23. 0x37,0x27,0x17,0x07,0x37,0x27,0x17,0x07,
  24. 0x36,0x26,0x16,0x06,0x36,0x26,0x16,0x06,
  25. 0x35,0x25,0x15,0x05,0x35,0x25,0x15,0x05,
  26. 0x34,0x24,0x14,0x04,0x34,0x24,0x14,0x04,
  27. 0x33,0x23,0x13,0x03,0x33,0x23,0x13,0x03,
  28. ];
  29. this.cardNode.removeAllChildren();
  30. for(let i = 0; i < this.cardsInfo.length; ++i) {
  31. const element = this.cardsInfo[i];
  32. let card = cc.instantiate(this.cardClone);
  33. card.parent = this.cardNode;
  34. card.active = true;
  35. card.belong = -1;
  36. card.beMagic = -1;
  37. let js = card.getComponent("TrucoCard");
  38. if(js) {
  39. js.setId(element, 0);
  40. }
  41. // UIHelper.addButtonListener(card, this.node, "TrucoControl", "clickCard");
  42. }
  43. this.selectedCards = [];
  44. this.selectedMagicCard = null;
  45. // this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
  46. this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseDown, this);
  47. },
  48. onMouseDown(event) {
  49. let mouseType = event.getButton();
  50. let mousePoint = event.getLocation();
  51. let localPoint = this.cardNode.convertToNodeSpaceAR(mousePoint);
  52. var touchCard = null;
  53. for(let i = 0; i < this.cardNode.childrenCount; ++i) {
  54. var cardNode = this.cardNode.children[i];
  55. var cardSize = cardNode.getBoundingBox().size;
  56. var cardPos = cardNode.getPosition();
  57. var rect = cc.rect(cardPos.x-cardSize.width/2,cardPos.y-cardSize.height/2,cardSize.width,cardSize.height);
  58. if (rect.contains(localPoint)){
  59. touchCard = cardNode;
  60. break;
  61. }
  62. }
  63. if(!touchCard) return;
  64. Global.print(touchCard);
  65. if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
  66. this.clickCard({target: touchCard});
  67. }else if(mouseType === cc.Event.EventMouse.BUTTON_RIGHT) {
  68. this.selectMagicCard({target: touchCard});
  69. }
  70. },
  71. show () {
  72. this.node.active = true;
  73. },
  74. onClickClose() {
  75. this.node.active = false;
  76. },
  77. onClickZiCe() {
  78. // if(this.selectedCards.length < 9) {
  79. // topTipMsg.showTopTipMsg("选中的牌少于9张");
  80. // return;
  81. // }
  82. let jsGameSence = ScenceManager.Instance().getGameComponent();
  83. if(jsGameSence._GameLogic.isSet([0,1,2,3],this.selectedCards)) {
  84. Global.print(this.selectedCards);
  85. }
  86. return;
  87. let magicCard = this.selectedMagicCard?this.selectedMagicCard.getComponent("TrucoCard").cardId():0;
  88. let huase = magicCard&0xF0;
  89. let value = (magicCard&0x0F) % 13;
  90. var magicCards = [];
  91. if((huase >> 4)%2 == 0) {
  92. magicCards.push(0x00|(value));
  93. magicCards.push(0x20|(value));
  94. }else{
  95. magicCards.push(0x10|(value));
  96. magicCards.push(0x30|(value));
  97. }
  98. jsGameSence._GameLogic.setMagicCard(magicCards);
  99. jsGameSence._GameLogic.analyseCardsGroupWithSort(this.selectedCards);
  100. },
  101. onClickSend() {
  102. if(this.selectedCards.length < 9) {
  103. topTipMsg.showTopTipMsg("选中的牌少于9张");
  104. return;
  105. }
  106. let jsGameSence = ScenceManager.Instance().getGameComponent();
  107. jsGameSence.SendGameSocketData(GameProtocol.SUB_C_TEST_CARD,{
  108. cbHandCard: this.selectedCards,
  109. cbMagicCard: this.selectedMagicCard?this.selectedMagicCard.getComponent("TrucoCard").cardId():0,
  110. });
  111. },
  112. selectMagicCard(event) {
  113. let card = event.target;
  114. if(this.selectedMagicCard) {
  115. if(this.selectedMagicCard == card) {
  116. card.color = cc.color(255,255,255);
  117. if(card.belong == 1) {
  118. card.color = cc.color(255,0,0);
  119. }
  120. card.beMagic = -1;
  121. this.selectedMagicCard = null;
  122. }else{
  123. this.selectedMagicCard.color = cc.color(255,255,255);
  124. this.selectedMagicCard.beMagic = -1;
  125. if(this.selectedMagicCard.belong == 1) {
  126. this.selectedMagicCard.color = cc.color(255,0,0);
  127. }
  128. card.color = cc.color(0,255,0);
  129. if(card.belong == 1) {
  130. card.color = cc.color(255,255,0);
  131. }
  132. card.beMagic = 1;
  133. this.selectedMagicCard = card;
  134. }
  135. }else{
  136. card.color = cc.color(0,255,0);
  137. if(card.belong == 1) {
  138. card.color = cc.color(255,255,0);
  139. }
  140. card.beMagic = 1;
  141. this.selectedMagicCard = card;
  142. }
  143. // let cardid = card.getComponent("TrucoCard").cardId();
  144. // if(card.beMagic == 1) {
  145. // card.color = cc.color(255,255,255);
  146. // if(card.belong == 1) {
  147. // card.color = cc.color(255,0,0);
  148. // }
  149. // card.beMagic = -1;
  150. // for(let i = 0; i < this.cardNode.childrenCount; ++i) {
  151. // let cardd = this.cardNode.children[i];
  152. // if(cardd.getComponent("TrucoCard").cardId() == cardid) {
  153. // cardd.color = cc.color(255,255,255);
  154. // if(card.belong == 1) {
  155. // card.color = cc.color(255,0,0);
  156. // }
  157. // this.selectedMagicCard = -1;
  158. // return;
  159. // }
  160. // }
  161. // return;
  162. // }
  163. // if(card.beMagic < 0) {
  164. // card.beMagic = 1;
  165. // card.color = cc.color(0,255,0);
  166. // if(card.belong == 1) {
  167. // card.color = cc.color(255,255,0);
  168. // }
  169. // this.selectedMagicCard = cardid;
  170. // }
  171. },
  172. clickCard(event) {
  173. let card = event.target;
  174. let cardid = card.getComponent("TrucoCard").cardId();
  175. if(card.belong == 1) {
  176. card.color = cc.color(255,255,255);
  177. if(card.beMagic == 1) {
  178. card.color = cc.color(0,255,0);
  179. }
  180. card.belong = -1;
  181. for(let i = 0; i < this.selectedCards.length; ++i) {
  182. if(this.selectedCards[i] == cardid) {
  183. this.selectedCards.splice(i, 1);
  184. return;
  185. }
  186. }
  187. return;
  188. }
  189. if(card.belong < 0) {
  190. if(this.selectedCards.length >= 10) {
  191. topTipMsg.showTopTipMsg("已选择10张牌");
  192. return;
  193. }
  194. card.belong = 1;
  195. card.color = cc.color(255,0,0);
  196. if(card.beMagic == 1) {
  197. card.color = cc.color(255,255,0);
  198. }
  199. this.selectedCards.push(cardid);
  200. }
  201. },
  202. });