JG0109.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.isPull = true;
  32. this.pullDir = pullDir
  33. this.showOptTouch(this.pullIcon,
  34. () => {
  35. this.pull()
  36. },()=>{
  37. this.isPull = false;
  38. })
  39. }
  40. }
  41. /**
  42. * 主角离开碰撞区域
  43. * @param tag 碰撞组件编号
  44. */
  45. public onEnd(tag: number) {
  46. this.closeOptTouch()
  47. this.isPull = false;
  48. }
  49. /**
  50. * 接触到石头后确定方向
  51. * @returns
  52. */
  53. public getPullDir():number{
  54. let mainSprite = this.ff.mainSprite
  55. let pbc = this.node.getComponent(cc.PhysicsBoxCollider)
  56. let x0,y0,x1,y1;
  57. x0 = this.node.x + pbc.offset.x - pbc.size.width/2 - 10
  58. x1 = this.node.x + pbc.offset.x + pbc.size.width/2 + 10
  59. y0 = this.node.y + pbc.offset.y - pbc.size.height/2 - 20//下
  60. y1 = this.node.y + pbc.offset.y + pbc.size.height/2 + 20//上
  61. let x = mainSprite.node.x
  62. let y = mainSprite.node.y
  63. if(x > x0 && x < x1){//当前角色在上下2个方向
  64. if(y > y1){
  65. return 1;
  66. }else{
  67. return 2;
  68. }
  69. }else if(y > y0 && y < y1){
  70. if(x < x0){
  71. return 3
  72. }else{
  73. return 4
  74. }
  75. }
  76. return -1
  77. }
  78. private pull(){
  79. let pos = cc.v2()
  80. if(this.pullDir == 1){
  81. pos.y = -64
  82. }else if(this.pullDir == 2){
  83. pos.y = 64
  84. }else if(this.pullDir == 3){
  85. pos.x = 64
  86. }else if(this.pullDir == 4){
  87. pos.x = -64
  88. }
  89. let tx = this.node.x + pos.x
  90. let ty = this.node.y + pos.y
  91. if(this.ff.mMap.checkCollision(tx,ty)){
  92. return
  93. }
  94. cc.tween(this.node).sequence(
  95. cc.delayTime(0.6),
  96. cc.callFunc(()=>{
  97. if(this.isPull){
  98. cc.tween(this.node).sequence(
  99. cc.moveBy(0.5,pos),
  100. cc.callFunc(()=>{})
  101. ).start()
  102. }
  103. }),
  104. cc.delayTime(0.5),
  105. cc.callFunc(()=>{
  106. if(this.isPull){
  107. this.pull()
  108. }
  109. })
  110. ).start()
  111. }
  112. }