PokerNode.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. let gfx = cc.gfx;
  2. var PokerMatType = {
  3. None : 0,
  4. DU : 1,
  5. DUAuto : 2,
  6. LR : 3,
  7. RL : 4,
  8. LRAuto : 5,
  9. }
  10. let MOVE_UP_DOWN = 1;
  11. let MOVE_RIGHT_LEFT = 1;
  12. cc.Class({
  13. extends: cc.BaseClass,
  14. editor: {
  15. executeInEditMode: true
  16. },
  17. properties: {
  18. pokerTextures: {
  19. default: [],
  20. type: cc.Texture2D,
  21. },
  22. },
  23. // LIFE-CYCLE CALLBACKS:
  24. // onLoad () {},
  25. start () {
  26. // var self = this;
  27. // cc.resources.load('p_front1', cc.Texture2D, function (err, texture) {
  28. // if (err) {
  29. // console.log('-----pc777----')
  30. // cc.error(err.message || err);
  31. // return;
  32. // }
  33. // self.frontTexture = texture;
  34. // self.startPoker();
  35. // });
  36. },
  37. SetHook:function(Hook) {
  38. this.m_Hook = Hook;
  39. },
  40. SetCardData:function(cbCardData){
  41. this._RealPoker = this.$('RealPoker@Sprite');
  42. this._RealPoker.node.active = false;
  43. var self = this;
  44. this.node.active = false;
  45. cc.gPreLoader.LoadRes(`Image_NdOpenCard2_2550`,'GamePublic_3',function(sf){
  46. self.backTexture = sf.getTexture();
  47. cc.gPreLoader.LoadRes(`Image_NdOpenCard2_c${cbCardData}`,'GamePublic_3',function(sf){
  48. self.node.active = true;
  49. self.frontTexture = sf.getTexture();
  50. self.startPoker();
  51. });
  52. });
  53. cc.gPreLoader.LoadRes(`Image_NdOpenCard2_r${cbCardData}`,'GamePublic_3',function(sf){
  54. self._RealPoker.spriteFrame = sf;
  55. });
  56. // cc.gPreLoader.LoadRes(`Image_NdOpenCard2_${cbCardData}`,'GamePublic_3',function(sf){
  57. // this.frontTexture = sf.getTexture();
  58. // this.startPoker();
  59. // }.bind(this));
  60. // this.frontTexture = this.pokerTextures[0];
  61. // this.startPoker();
  62. },
  63. startPoker() {
  64. this.RubCardLayer_Dir_du = 0;//上下搓牌
  65. this.RubCardLayer_Dir_lr = 1;//左右搓牌
  66. this.RubCardLayer_Dir_rl = 2;//右左搓牌
  67. this.RubCardLayer_Dir_No = 3;//不搓牌
  68. this.RubCardLayer_State_Move = 0;
  69. this.RubCardLayer_State_Smooth = 1;
  70. var RubCardLayer_Pai = 3.141592
  71. this.RubCardLayer_RotationFrame = 10
  72. this.RubCardLayer_RotationAnger = RubCardLayer_Pai/3
  73. this.RubCardLayer_SmoothFrame = 10
  74. this.RubCardLayer_SmoothAnger = RubCardLayer_Pai/6
  75. this.state = this.RubCardLayer_State_Move;
  76. this.smoothFrame = 1;
  77. this.isCreateNum = false;
  78. this.pokerRatio = 0;
  79. this.isChangedMat = false;
  80. this.isTextureVertical = true;
  81. this.initPos();
  82. this.udMesh = this.initMesh(false);
  83. this.lrMesh = this.initMesh(true);
  84. this.initMat();
  85. this.dirState = -1
  86. this.changeTouchMeshAndMat(this.RubCardLayer_Dir_du);
  87. this.registerTouch();
  88. },
  89. initPos() {
  90. var size = cc.winSize
  91. var zeye = size.height * 3/(2 *1.732050807568877);
  92. this.node.setPosition(cc.v3(0, 0, zeye))
  93. let camera = this.node.getComponent(cc.Camera);
  94. camera._fov = 30
  95. let renderer = this.node.getComponent(cc.MeshRenderer);
  96. if (!renderer) {
  97. renderer = this.node.addComponent(cc.MeshRenderer);
  98. }
  99. this.meshRenderer = renderer
  100. this.pokerPosX = cc.winSize.width/2;
  101. this.pokerPosY = cc.winSize.height/2;
  102. var pokerWidth = 680 //*1.5;680
  103. var pokerHeight = 472 //*1.5;
  104. // if(this.isTextureVertical){
  105. // pokerWidth = 457;
  106. // pokerHeight = 314;
  107. // }
  108. this.pokerWidth = pokerWidth;
  109. this.pokerHeight = pokerHeight;
  110. this.pokerOffX = this.pokerPosX - this.pokerWidth/2;
  111. this.pokerOffY = this.pokerPosY - this.pokerHeight/2;
  112. this.touchStartY = this.pokerOffY;
  113. this.touchStartLRX = this.pokerOffX;
  114. this.touchStartRLX = this.pokerOffX + this.pokerWidth;
  115. this.pokerRadius = pokerHeight/10;
  116. },
  117. initMat() {
  118. this.matMap = {};
  119. for(var i = 0; i< 6; i++)
  120. {
  121. let material = this.meshRenderer.getMaterial(i);
  122. material.setProperty('radius', this.pokerRadius);
  123. material.setProperty('width', this.pokerWidth);
  124. material.setProperty('height', this.pokerHeight);
  125. material.setProperty('offx', this.pokerOffX);
  126. material.setProperty('offy', this.pokerOffY);
  127. material.setProperty('rotation', 0);
  128. material.setProperty('textureBack', this.backTexture);
  129. material.setProperty('textureFront', this.frontTexture);
  130. this.matMap[i] = material;
  131. }
  132. },
  133. changeTouchMeshAndMat(newDir) {
  134. if(this.dirState == newDir)
  135. return;
  136. this.dirState = newDir;
  137. if(newDir == this.RubCardLayer_Dir_lr)
  138. {
  139. //console.log('-----changeTouchMeshAndMat LR----')
  140. var newMat = this.matMap[PokerMatType.LR];
  141. this.meshRenderer.setMaterial(0, newMat)
  142. this._material = newMat;
  143. this.mesh = this.lrMesh;
  144. this.meshRenderer.mesh = this.lrMesh;
  145. }
  146. else if(newDir == this.RubCardLayer_Dir_rl)
  147. {
  148. //console.log('-----changeTouchMeshAndMat RL----')
  149. var newMat = this.matMap[PokerMatType.RL];
  150. this.meshRenderer.setMaterial(0, newMat)
  151. this._material = newMat;
  152. this.mesh = this.lrMesh;
  153. this.meshRenderer.mesh = this.lrMesh;
  154. }else{
  155. //console.log('-----changeTouchMeshAndMat DU----')
  156. var newMat = this.matMap[PokerMatType.DU];
  157. this.meshRenderer.setMaterial(0, newMat)
  158. this._material = newMat;
  159. this.mesh = this.udMesh;
  160. this.meshRenderer.mesh = this.udMesh;
  161. }
  162. },
  163. changeToAutoMat(){
  164. if(this.isChangedMat)
  165. return;
  166. this.isChangedMat = true
  167. var dir = this.dirState
  168. if(dir == this.RubCardLayer_Dir_lr || dir == this.RubCardLayer_Dir_rl)
  169. {
  170. var newMat = this.matMap[PokerMatType.LRAuto];
  171. this.meshRenderer.setMaterial(0, newMat)
  172. this._material = newMat;
  173. }else
  174. {
  175. var newMat = this.matMap[PokerMatType.DUAuto];
  176. this.meshRenderer.setMaterial(0, newMat)
  177. this._material = newMat;
  178. }
  179. },
  180. registerTouch() {
  181. var self = this;
  182. self.node.setContentSize(2000, 2000);
  183. self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  184. if(self.isCreateNum) return;
  185. var touches = event.getTouches();
  186. var location = touches[0].getLocation();
  187. var newDir = null;
  188. if(MOVE_UP_DOWN>0 && location.x > self.touchStartLRX && location.x < self.touchStartRLX && location.y < (self.touchStartY + 100))
  189. {
  190. newDir = self.RubCardLayer_Dir_du
  191. }
  192. else if(MOVE_RIGHT_LEFT>0 && location.y > self.touchStartY && location.y < (self.touchStartY+self.pokerHeight) && location.x < (self.touchStartLRX + 100))
  193. {
  194. newDir = self.RubCardLayer_Dir_lr
  195. }
  196. else if(MOVE_RIGHT_LEFT>0 && location.y > self.touchStartY && location.y < (self.touchStartY+self.pokerHeight) && location.x > (self.touchStartRLX - 100))
  197. {
  198. newDir = self.RubCardLayer_Dir_rl
  199. }
  200. else
  201. {
  202. if(MOVE_UP_DOWN>0){
  203. newDir = self.RubCardLayer_Dir_du
  204. }
  205. }
  206. if(newDir== null) return;
  207. self.changeTouchMeshAndMat(newDir);
  208. }, self);
  209. self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  210. if(self.isCreateNum) return;
  211. var touches = event.getTouches();
  212. var location = touches[0].getLocation();
  213. //self.pokerRatio = (location.y-self.pokerOffY)/self.pokerHeight
  214. if(self.dirState == self.RubCardLayer_Dir_du)
  215. {
  216. self.pokerRatio = (location.y-self.touchStartY)/self.pokerHeight;
  217. }
  218. else if(self.dirState == self.RubCardLayer_Dir_lr)
  219. {
  220. self.pokerRatio = (location.x-self.touchStartLRX)/self.pokerWidth;
  221. }
  222. else if(self.dirState == self.RubCardLayer_Dir_rl)
  223. {
  224. self.pokerRatio = (self.touchStartRLX-location.x)/self.pokerWidth;
  225. }
  226. self.pokerRatio = Math.max(0, self.pokerRatio)
  227. self.pokerRatio = Math.min(1, self.pokerRatio)
  228. //console.log("TOUCH_MOVE pokerRatio="+self.pokerRatio);
  229. }, self);
  230. self.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  231. if(self.isCreateNum) return;
  232. if(self.pokerRatio >= 1)
  233. {
  234. self.state = self.RubCardLayer_State_Smooth;
  235. }
  236. else
  237. {
  238. self.pokerRatio = 0
  239. }
  240. }, self);
  241. self.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  242. if(self.isCreateNum) return;
  243. if(self.pokerRatio >= 1)
  244. {
  245. self.state = self.RubCardLayer_State_Smooth;
  246. }
  247. else
  248. {
  249. self.pokerRatio = 0
  250. }
  251. }, self);
  252. },
  253. initMesh(isLeftRight){
  254. var pokerWidth = this.pokerWidth;
  255. var pokerHeight = this.pokerHeight;
  256. var pokerMesh = new cc.Mesh();
  257. var vfmt = new gfx.VertexFormat([
  258. { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
  259. { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
  260. ]);
  261. var pokerDiv = 30
  262. var pointNum = pokerDiv*2 + 2;
  263. pokerMesh.init(vfmt, pointNum, true);
  264. var arrayNum = pointNum*2;
  265. var startPosX = 0;
  266. var startPosY = 0;
  267. var widthDis = pokerWidth/pokerDiv;
  268. var heightDis = pokerHeight/pokerDiv;
  269. var texHeightDis = 1/pokerDiv;
  270. var posArray = new Float32Array(arrayNum);
  271. var texArray = new Float32Array(arrayNum);
  272. for (var i = 0; i < pointNum; i++)
  273. {
  274. var posX = startPosX;
  275. var posY = startPosY;
  276. if(isLeftRight){
  277. if(i%2 == 0){
  278. posX = (i/2)*widthDis;
  279. }else{
  280. posY = startPosY+pokerHeight;
  281. posX = ((i-1)/2)*widthDis;
  282. }
  283. posX = posX + startPosX;
  284. }else{
  285. if(i%2 == 0){
  286. posY = (i/2)*heightDis;
  287. }else{
  288. posX = startPosX+pokerWidth;
  289. posY = ((i-1)/2)*heightDis;
  290. }
  291. posY = posY + startPosY;
  292. }
  293. posArray[i*2] = posX
  294. posArray[i*2+1] = posY
  295. if(isLeftRight){
  296. if(i%2 == 0){
  297. if(this.isTextureVertical){
  298. texArray[i*2] = 1
  299. texArray[i*2+1] = (i/2)*texHeightDis
  300. }else{
  301. texArray[i*2] = (i/2)*texHeightDis
  302. texArray[i*2+1] = 0
  303. }
  304. }else{
  305. if(this.isTextureVertical){
  306. texArray[i*2] = 0
  307. texArray[i*2+1] = ((i-1)/2)*texHeightDis
  308. }else{
  309. texArray[i*2] = ((i-1)/2)*texHeightDis
  310. texArray[i*2+1] = 1
  311. }
  312. }
  313. }else{
  314. if(i%2 == 0){
  315. if(this.isTextureVertical){
  316. texArray[i*2] = (i/2)*texHeightDis
  317. texArray[i*2+1] = 1
  318. }else{
  319. texArray[i*2] = 0
  320. texArray[i*2+1] = (i/2)*texHeightDis
  321. }
  322. }else{
  323. if(this.isTextureVertical){
  324. texArray[i*2] = ((i-1)/2)*texHeightDis
  325. texArray[i*2+1] = 0
  326. }else{
  327. texArray[i*2] = 1
  328. texArray[i*2+1] = ((i-1)/2)*texHeightDis
  329. }
  330. }
  331. }
  332. }
  333. pokerMesh.setVertices(gfx.ATTR_POSITION, posArray, 0)
  334. pokerMesh.setVertices(gfx.ATTR_UV0, texArray, 0)
  335. var triangleNum = (pointNum/2-1)*6;
  336. var indiceArray = new Uint16Array(triangleNum);
  337. var index = 0;
  338. if(isLeftRight){
  339. for (var i = 0; i < (pointNum-2); i=i+2){
  340. indiceArray[index++] = i;
  341. indiceArray[index++] = i+2;
  342. indiceArray[index++] = i+1;
  343. indiceArray[index++] = i+1;
  344. indiceArray[index++] = i+2;
  345. indiceArray[index++] = i+3;
  346. }
  347. }
  348. else{
  349. for (var i = 0; i < (pointNum-2); i=i+2){
  350. indiceArray[index++] = i;
  351. indiceArray[index++] = i+1;
  352. indiceArray[index++] = i+2;
  353. indiceArray[index++] = i+1;
  354. indiceArray[index++] = i+3;
  355. indiceArray[index++] = i+2;
  356. }
  357. }
  358. pokerMesh.setIndices(indiceArray, 0);
  359. return pokerMesh;
  360. },
  361. update (dt) {
  362. if(this.state == this.RubCardLayer_State_Move){
  363. if(this._material){
  364. this._material.setProperty('ratio', this.pokerRatio);
  365. }
  366. }else if(this.state == this.RubCardLayer_State_Smooth){
  367. if(this.smoothFrame <= this.RubCardLayer_RotationFrame){
  368. var rot = -this.RubCardLayer_RotationAnger*this.smoothFrame/this.RubCardLayer_RotationFrame;
  369. this._material.setProperty('rotation', rot);
  370. }
  371. else if(this.smoothFrame < (this.RubCardLayer_RotationFrame+this.RubCardLayer_SmoothFrame)){
  372. this.changeToAutoMat()
  373. var scale = (this.smoothFrame - this.RubCardLayer_RotationFrame)/this.RubCardLayer_SmoothFrame;
  374. var rot = Math.max(0.01,this.RubCardLayer_SmoothAnger*(1-scale));
  375. this._material.setProperty('rotation', rot);
  376. }
  377. else{
  378. //第一次到这里就铺平了
  379. if(this.isCreateNum == false){
  380. this.isCreateNum = true
  381. this._material.setProperty('rotation', 0.0);
  382. console.log("666666666666=");
  383. this._RealPoker.node.active = true;
  384. var self = this
  385. this.meshRenderer.scheduleOnce(function() {
  386. self._RealPoker.node.active = true;
  387. },0.15);
  388. this.meshRenderer.scheduleOnce(function() {
  389. self.node.parent.active = false
  390. self.m_Hook.m_GameClientView.OnBnClickedShowCard();
  391. //self.meshRenderer.onDisable()
  392. //self.destroy()
  393. //self.node.parent.OnDestroy();
  394. }, 2);
  395. }
  396. }
  397. this.smoothFrame = this.smoothFrame + 1
  398. }
  399. },
  400. });