CharaCollision.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import GameManager from "./GameManager";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class CharaCollision extends cc.Component {
  11. CharaNumber: number = 0;//代表自身是那种类型的水果
  12. returnNumber: boolean = false;
  13. getNumberTime: number = 0;
  14. bianjieX: number = 0;// 防止水果位置超出屏幕外
  15. testEndDJS: number = 0;// 检测死亡倒计时
  16. endOne: number = 0; //只执行一次结束
  17. // LIFE-CYCLE CALLBACKS:
  18. // onLoad () {}
  19. start() {
  20. this.bianjieX = 360 - this.node.width / 2;
  21. }
  22. update(dt) {
  23. if (this.node.x < -this.bianjieX) {
  24. this.node.x = -this.bianjieX
  25. }
  26. if (this.node.x > this.bianjieX) {
  27. this.node.x = this.bianjieX
  28. }
  29. if (this.returnNumber) {
  30. this.scheduleOnce(() => {
  31. this.getNumberTime = 0
  32. }, 0.25)
  33. this.returnNumber = false;
  34. }
  35. if (this.node.parent.name == "CharaNode") {
  36. this.testEndDJS += dt;
  37. //cc.log(this.testEndDJS)
  38. }
  39. if (this.node.y + this.node.width / 2 > cc.find("Canvas/LineNode").children[0].y && this.endOne == 0 && this.testEndDJS > 3) {
  40. for (let i = 0; i < cc.find("Canvas/CharaNode").children.length; i++) {
  41. cc.find("Canvas/CharaNode").children[i].removeComponent(cc.PhysicsCircleCollider);
  42. cc.find("Canvas/CharaNode").children[i].removeComponent(cc.RigidBody);
  43. }
  44. this.node.color = cc.Color.RED;
  45. cc.tween(this.node)
  46. .to(0.3, { opacity: 0 })
  47. .to(0.3, { opacity: 255 })
  48. .union()
  49. .repeat(3)
  50. .call(() => {
  51. GameManager.Instance.end();
  52. cc.find("Canvas/LineNode").children[0].active = false;
  53. })
  54. .start()
  55. this.endOne++;
  56. }
  57. }
  58. // 防止多次碰撞
  59. getNumber() {
  60. let ad = this.getNumberTime;
  61. this.getNumberTime++;
  62. this.returnNumber = true;
  63. return ad;
  64. }
  65. onBeginContact(contact, selfCollider, otherCollider) {
  66. if (otherCollider.node.group == "Chara") {
  67. //this.endCtrl = true;
  68. // 只有下方的水果触发碰撞回调
  69. if (selfCollider.node.y < otherCollider.node.y) {
  70. return
  71. }
  72. // 水果一下落,放在CharaNode节点下
  73. selfCollider.node.parent = cc.find("Canvas/CharaNode");
  74. if (selfCollider.node.getComponent(cc.RigidBody) != null) {
  75. selfCollider.node.getComponent(cc.RigidBody).angularVelocity = 0;
  76. // 限制一下线速度
  77. }
  78. let selfNum = this.CharaNumber;
  79. let otherNum = otherCollider.node.getComponent("CharaCollision").CharaNumber;
  80. // 水果类型相同的合成
  81. if (selfNum == otherNum && selfNum < GameManager.Instance.AllChara.length-1 && otherNum < GameManager.Instance.AllChara.length-1) {
  82. if (selfCollider.node.getComponent("CharaCollision").getNumber() == 0) {
  83. otherCollider.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
  84. otherCollider.node.getComponent(cc.PhysicsCircleCollider).apply()
  85. this.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
  86. this.node.getComponent(cc.PhysicsCircleCollider).apply();
  87. cc.tween(selfCollider.node)
  88. .to(0.1, { position: otherCollider.node.position })
  89. .call(() => {
  90. try {
  91. //生成下一个等级的水果
  92. GameManager.Instance.CharaNumberRec[this.CharaNumber]=GameManager.Instance.CharaNumberRec[this.CharaNumber]-2;
  93. GameManager.Instance.CharaNumberRec[this.CharaNumber + 1]++;
  94. GameManager.Instance.score += this.CharaNumber + 1;
  95. GameManager.Instance.createLevelUpChara(this.CharaNumber + 1, otherCollider.node.position);
  96. otherCollider.node.active = false;
  97. selfCollider.node.active = false;
  98. otherCollider.node.destroy();
  99. selfCollider.node.destroy();
  100. GameManager.Instance.updateCharaNumber();
  101. } catch (error) {
  102. }
  103. })
  104. .start()
  105. }
  106. }
  107. // else if (selfNum == otherNum && selfNum == GameManager.Instance.AllChara.length && otherNum == GameManager.Instance.AllChara.length) {
  108. // if (selfCollider.node.getComponent("CharaCollision").getNumber() == 0) {
  109. // otherCollider.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
  110. // otherCollider.node.getComponent(cc.PhysicsCircleCollider).apply()
  111. // this.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
  112. // this.node.getComponent(cc.PhysicsCircleCollider).apply();
  113. // cc.tween(selfCollider.node)
  114. // .to(0.1, { position: otherCollider.node.position })
  115. // .call(() => {
  116. // GameManager.Instance.score += this.CharaNumber + 1;
  117. // GameManager.Instance.createLevelUpChara(this.CharaNumber + 1, otherCollider.node.position);
  118. // otherCollider.node.active = false;
  119. // selfCollider.node.active = false;
  120. // otherCollider.node.destroy();
  121. // selfCollider.node.destroy();
  122. // })
  123. // .start()
  124. // }
  125. // }
  126. }
  127. }
  128. }