Shooter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. item: cc.Node,
  5. shooterFrames: [cc.SpriteFrame],
  6. },
  7. // 初始化组件
  8. init (game) {
  9. this.game = game;
  10. this.wrap = game.wrap;
  11. this.modeHit = game.modeHit;
  12. // this.shooterMng = game.shooterMng;
  13. this.audioMng = game.audioMng;
  14. this.vSize = cc.director.getVisibleSize();
  15. this.posY = this.vSize.height / 2 + 50;
  16. // this.weldJoint = this.getComponent(cc.WeldJoint);
  17. this.shootBody = this.getComponent(cc.RigidBody)
  18. this.node.x = 0;
  19. this.node.y = this.vSize.height / 2 - 60;
  20. this.status = 0;
  21. // 设置箭矢皮肤
  22. let arrIndex = window.Global.arrowSkinIndex || 0
  23. this.item.getComponent(cc.Sprite).spriteFrame = this.shooterFrames[arrIndex]
  24. // 监听触摸事件
  25. this.scheduleOnce(() => {
  26. this.wrap.once(cc.Node.EventType.TOUCH_START, this.shotEvent, this)
  27. }, 0.09)
  28. },
  29. shotEvent () {
  30. if(this.game.gameMode != 'hit') {
  31. //必中瓶模式结束后1.5秒内暂停发射箭矢
  32. if(this.modeHit.status == 2) {
  33. this.wrap.once(cc.Node.EventType.TOUCH_START, this.shotEvent, this)
  34. } else {
  35. this.runAnim()
  36. }
  37. } else {
  38. this.runHit()
  39. }
  40. },
  41. runHit () {
  42. let spawn = cc.spawn(cc.moveTo(0.1, cc.p(this.node.x, this.node.y - this.vSize.height)), cc.fadeOut(0.2));
  43. this.node.runAction(spawn);
  44. this.scheduleOnce(this.reset, 0.1)
  45. },
  46. runAnim () {
  47. // this.status = 1;
  48. this.audioMng.playShoot()
  49. // let spawn = cc.spawn(cc.moveTo(0.3, cc.p(this.node.x, this.node.y - this.vSize.height)), cc.fadeOut(0.5));
  50. // this.node.runAction(spawn);
  51. let vec = cc.v2(this.vSize / 2, 0).sub(this.node.position)
  52. let distance = vec.mag();
  53. let velocity = vec.normalize().mulSelf(2500)
  54. this.shootBody.type = cc.RigidBodyType.Dynamic;
  55. this.shootBody.linearVelocity = velocity;
  56. },
  57. /**
  58. * 碰撞检测
  59. */
  60. // onCollisionEnter: function (other, self) {
  61. // //击中必中瓶模式
  62. // if(this.game.gameMode == 'hit') {
  63. // this.game.inGameUI.addScore(1);
  64. // this.reset()
  65. // return;
  66. // }
  67. // if(other.node.group == 'wall') {
  68. // if(this.game.gameMode != 'undead') {
  69. // this.scheduleOnce(() => {
  70. // this.game.gameOver();
  71. // }, 0.5)
  72. // }
  73. // } else if(other.node.group == 'bottle') {
  74. // let score = 1;
  75. // this.game.inGameUI.addScore(score);
  76. // this.reset()
  77. // }
  78. // },
  79. // 只在两个碰撞体开始接触时被调用一次
  80. onBeginContact (contact, selfCollider, otherCollider) {
  81. if(otherCollider.node.group == 'wall') {
  82. this.game.delShooter(this.node);
  83. if(this.game.gameMode != 'hit') {
  84. this.scheduleOnce(() => {
  85. this.game.gameOver();
  86. }, 0.5)
  87. }
  88. } else if(otherCollider.node.group == 'bottle') {
  89. this.game.delShooter(this.node);
  90. let score = 1;
  91. this.game.inGameUI.addScore(score);
  92. this.game.readyShooter();
  93. }
  94. },
  95. /**
  96. * 物理碰撞回调
  97. */
  98. // onPostSolve (contact, selfCollider, otherCollider) {
  99. // let impulse = contact.getImpulse();
  100. // if (Math.abs(impulse.normalImpulses[0]) < cc.PhysicsManager.PTM_RATIO) {
  101. // return;
  102. // }
  103. // let joint = this.weldJoint;
  104. // if (joint.enabled) {
  105. // joint.enabled = false;
  106. // return
  107. // }
  108. // // let arrowBody = selfCollider.body;
  109. // // let targetBody = otherCollider.body;
  110. // // let worldCoordsAnchorPoint = arrowBody.getWorldPoint(cc.v2(0.6, 0));
  111. // // joint.connectedBody = targetBody;
  112. // // joint.anchor = arrowBody.getLocalPoint( worldCoordsAnchorPoint );
  113. // // joint.connectedAnchor = targetBody.getLocalPoint( worldCoordsAnchorPoint );
  114. // // joint.referenceAngle = targetBody.node.rotation - arrowBody.node.rotation;
  115. // // joint.enabled = true;
  116. // },
  117. /**
  118. * 重置箭的位置和状态
  119. */
  120. reset () {
  121. this.game.resetShooter(this.node);
  122. // this.node.x = 0;
  123. // this.node.y = this.vSize.height / 2 - 40;
  124. // this.node.opacity = 255;
  125. // this.status = 0;
  126. // this.shootBody.type = cc.RigidBodyType.Static;
  127. },
  128. start () {
  129. },
  130. update (dt) {
  131. },
  132. });