JG0109.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import BaseEvent from "../fight/evnet/base/BaseEvent";
  2. /**
  3. * 推箱子、石头
  4. */
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class JG0109 extends BaseEvent {
  8. @property({
  9. displayName:'推图标',
  10. type:cc.SpriteFrame
  11. })
  12. pullIcon: cc.SpriteFrame = null;
  13. /**
  14. * 推的方向
  15. * 上下左右
  16. * 1,2,3,4
  17. */
  18. private pullDir = -1;
  19. /**
  20. * 当前是否推的状态
  21. */
  22. private isPull = false
  23. /**
  24. * 主角进入碰撞区域
  25. * @param tag 碰撞组件编号
  26. */
  27. public onBegin(tag: number) {
  28. let pullDir = this.getPullDir()
  29. // cc.log('pullDir : ',pullDir)
  30. if(pullDir > 0){
  31. this.pullDir = pullDir
  32. this.showOptTouch(this.pullIcon,
  33. () => {
  34. this.isPull = true;
  35. this.pull()
  36. },()=>{
  37. this.stopPull()
  38. })
  39. }
  40. }
  41. /**
  42. * 主角离开碰撞区域
  43. * @param tag 碰撞组件编号
  44. */
  45. public onEnd(tag: number) {
  46. if(!this.isPull){
  47. this.closeOptTouch()
  48. }
  49. }
  50. /**
  51. * 接触到石头后确定方向
  52. * @returns
  53. */
  54. public getPullDir():number{
  55. let mainSprite = this.ff.mainSprite
  56. let pbc = this.node.getComponent(cc.PhysicsBoxCollider)
  57. let x0,y0,x1,y1;
  58. x0 = this.node.x + pbc.offset.x - pbc.size.width/2 - 10
  59. x1 = this.node.x + pbc.offset.x + pbc.size.width/2 + 10
  60. y0 = this.node.y + pbc.offset.y - pbc.size.height/2 - 20//下
  61. y1 = this.node.y + pbc.offset.y + pbc.size.height/2 + 20//上
  62. let x = mainSprite.node.x
  63. let y = mainSprite.node.y
  64. if(x > x0 && x < x1){//当前角色在上下2个方向
  65. if(y > y1){
  66. return 1;
  67. }else{
  68. return 2;
  69. }
  70. }else if(y > y0 && y < y1){
  71. if(x < x0){
  72. return 3
  73. }else{
  74. return 4
  75. }
  76. }
  77. return -1
  78. }
  79. private pull(){
  80. let pos = cc.v2()
  81. let moveV2 = cc.v2()
  82. if(this.pullDir == 1){
  83. pos.y = -64
  84. moveV2.y = -1
  85. }else if(this.pullDir == 2){
  86. pos.y = 64
  87. moveV2.y = 1
  88. }else if(this.pullDir == 3){
  89. pos.x = 64
  90. moveV2.x = 1
  91. }else if(this.pullDir == 4){
  92. pos.x = -64
  93. moveV2.x = -1
  94. }
  95. let mainSprite = this.ff.mainSprite
  96. mainSprite.stopJoystick = true
  97. mainSprite.startMove(moveV2)
  98. let tx = this.node.x + pos.x
  99. let ty = this.node.y + pos.y
  100. if(this.ff.mMap.checkCollision(tx,ty)){
  101. return
  102. }
  103. cc.tween(this.node).sequence(
  104. cc.delayTime(0.6),
  105. cc.callFunc(()=>{
  106. if(this.isPull){
  107. cc.tween(this.node).sequence(
  108. cc.moveBy(0.5,pos),
  109. cc.callFunc(()=>{})
  110. ).start()
  111. // cc.tween(this.ff.mainSprite.node).sequence(
  112. // cc.moveBy(0.5,pos),
  113. // cc.callFunc(()=>{})
  114. // ).start()
  115. }
  116. }),
  117. cc.delayTime(0.5),
  118. cc.callFunc(()=>{
  119. if(this.ff.mainSprite.gamePause){
  120. this.closeOpt()
  121. this.stopPull()
  122. return
  123. }
  124. if(this.isPull){
  125. this.pull()
  126. }
  127. })
  128. ).start()
  129. }
  130. private stopPull(){
  131. this.isPull = false;
  132. let mainSprite = this.ff.mainSprite
  133. mainSprite.stopJoystick = false
  134. mainSprite.stopMove()
  135. }
  136. }