dadishu.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var ObjectPool = require("ObjectPool");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. 我方: cc.Node,
  6. 敌方: cc.Node,
  7. 对象池: {
  8. default: null,
  9. type: ObjectPool
  10. },
  11. 敌方血条: cc.Node,
  12. 我方血条: cc.Node,
  13. 倒计时: cc.Label,
  14. 敌方分数: cc.Label,
  15. 我方分数: cc.Label,
  16. 结束界面: cc.Node,
  17. 连击动画: cc.Node,
  18. 拳套: cc.Node,
  19. 计时标志: false,
  20. },
  21. onLoad() {
  22. Games = this;
  23. this.glovesDragon = this.拳套.getComponent(dragonBones.ArmatureDisplay);
  24. console.log(this.时分(60));
  25. this.初始化();
  26. },
  27. 初始化() {
  28. this.随机生成(this.我方);
  29. this.随机生成(this.敌方);
  30. this.敌方分数.string = "10";
  31. this.我方分数.string = "10";
  32. this.敌方血条.getComponent(cc.ProgressBar).progress = 1;
  33. this.我方血条.getComponent(cc.ProgressBar).progress = 1;
  34. this.倒计 = 60;
  35. this.计时标志 = true;
  36. },
  37. start() {
  38. },
  39. 掉血(父级, 血量) {
  40. if (父级.name == this.我方.name) {
  41. var 分数 = parseInt(this.敌方分数.string);
  42. 分数 -= 血量;
  43. this.敌方分数.string = 分数.toString();
  44. this.敌方血条.getComponent(cc.ProgressBar).progress -= 0.1;
  45. if (分数 <= 0) {
  46. this.敌方分数.string = "0";
  47. this.敌方血条.getComponent(cc.ProgressBar).progress = 0;
  48. console.log("游戏结束,胜利");
  49. this.游戏成功();
  50. }
  51. } else {
  52. var 分数 = parseInt(this.我方分数.string);
  53. 分数 -= 血量;
  54. this.我方分数.string = 分数.toString();
  55. this.我方血条.getComponent(cc.ProgressBar).progress -= 0.1 * 血量;
  56. if (分数 <= 0) {
  57. this.我方分数.string = 分数.toString();
  58. this.我方血条.getComponent(cc.ProgressBar).progress = 0;
  59. console.log("游戏结束,失败");
  60. this.游戏失败();
  61. }
  62. }
  63. },
  64. 游戏成功() {
  65. this.游戏结束();
  66. },
  67. 游戏失败() {
  68. this.游戏结束();
  69. },
  70. 倒计时结束() {
  71. this.游戏结束();
  72. this.unscheduleAllCallbacks();
  73. if (parseInt(this.我方分数.string) > parseInt(this.敌方分数.string)) {
  74. console.log("胜利");
  75. } else if (parseInt(this.我方分数.string) == parseInt(this.敌方分数.string)) {
  76. console.log("平局");
  77. } else {
  78. console.log("失败");
  79. }
  80. },
  81. 游戏结束() {
  82. cc.director.pause();
  83. this.结束界面.active = true;
  84. this.glovesDragon.armature().animation.stop();
  85. for (var i = 0; i < this.连击动画.children.length; i++) {
  86. this.连击动画.children[i].active = false;
  87. }
  88. this.拳套.active = false;
  89. },
  90. 重新开始() {
  91. cc.director.resume();
  92. Games.拳套.active = true;
  93. this.glovesDragon.playAnimation("<None>", 1);
  94. for (var i = 0; i < this.我方.children.length; i++) {
  95. var childs = this.我方.children[i].children[0];
  96. if (childs.children.length == 1) {
  97. ObjectPools.地鼠回收(childs.children[0]);
  98. }
  99. }
  100. for (var i = 0; i < this.敌方.children.length; i++) {
  101. var childs = this.敌方.children[i].children[0];
  102. if (childs.children.length == 1) {
  103. ObjectPools.地鼠回收(childs.children[0]);
  104. }
  105. }
  106. this.初始化();
  107. this.结束界面.active = false;
  108. },
  109. 随机生成(父级) {
  110. var flag = false;
  111. for (var i = 0; i < 父级.children.length; i++) {
  112. var childs = 父级.children[i].children[0];
  113. if (childs.children.length == 0) {
  114. flag = true;
  115. }
  116. }
  117. if (flag == false)
  118. return;
  119. var randomIndex = parseInt(Math.random() * 3, 10);
  120. var parents = 父级.children[randomIndex].getChildByName("Mask");
  121. var randomShapeIndex = parseInt(Math.random() * 5, 10);
  122. // 炸弹
  123. if (randomShapeIndex == 4) {
  124. var item = this.对象池.生成炸弹();
  125. if (parents.children.length == 0) {
  126. item.parent = parents;
  127. } else {
  128. this.随机生成(父级);
  129. return;
  130. }
  131. item.getComponent("zhadan").init(父级, randomShapeIndex);
  132. } else {
  133. var item = this.对象池.生成地鼠();
  134. if (parents.children.length == 0) {
  135. item.parent = parents;
  136. } else {
  137. this.随机生成(父级);
  138. return;
  139. }
  140. item.getComponent("dishu").init(父级, randomShapeIndex);
  141. }
  142. },
  143. checkTouch: function (target, location) {
  144. let node = target;
  145. let pointInNode = node.convertToNodeSpaceAR(location);
  146. let i, j, c = false;
  147. if (!target.getComponent(cc.PolygonCollider))
  148. return;
  149. let nvert = target.getComponent(cc.PolygonCollider).points.length;
  150. let testx = pointInNode.x;
  151. let testy = pointInNode.y;
  152. let vert = target.getComponent(cc.PolygonCollider).points;
  153. for (i = 0, j = nvert - 1; i < nvert; j = i++) {
  154. if (((vert[i].y > testy) != (vert[j].y > testy)) &&
  155. (testx < (vert[j].x - vert[i].x) * (testy - vert[i].y) / (vert[j].y - vert[i].y) + vert[i].x))
  156. c = !c;
  157. }
  158. return c;
  159. },
  160. // 输出03:05:59 时分秒
  161. 时分(result) {
  162. var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  163. var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  164. var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  165. //return result = h + ":" + m + ":" + s;
  166. return result = m + ":" + s;
  167. },
  168. 连击播放(p, obj, str) {
  169. for (var i = 0; i < obj.children.length; i++) {
  170. var exp = obj.children[i];
  171. var labelstr = exp.getChildByName("New Label").getComponent(cc.Label);
  172. labelstr.string = str;
  173. if (exp.active == false) {
  174. exp.position = p;
  175. exp.active = true;
  176. break;
  177. }
  178. }
  179. },
  180. update(dt) {
  181. if (this.计时标志) {
  182. if (this.倒计 === 0) {
  183. this.倒计时结束();
  184. this.计时标志 = false;
  185. }
  186. this.倒计 -= dt;
  187. var 时间 = this.时分(this.倒计);
  188. this.倒计时.string = 时间.toString();
  189. }
  190. },
  191. });