YaoGan.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import AudioManager from "../AudioManager";
  2. import EventName from "../EventName/EventName";
  3. import { Global } from "../Global";
  4. import { SpineAnimationState } from "./Character";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class YaoGan extends cc.Component {
  8. @property(cc.Node)
  9. bg: cc.Node = null;//摇杆背景
  10. @property(cc.Node)
  11. joystick: cc.Node = null;//摇杆 也就是中心点
  12. @property(cc.Node)
  13. parent: cc.Node = null;//摇杆和背景的父节点
  14. max_R: number = 112//25;//摇杆移动的最大半径
  15. @property
  16. is_rotation: boolean = true;//角色是否根据摇杆的方向旋转
  17. @property
  18. is_forbidden: boolean = false;//是否禁用摇杆
  19. CurrentDir: cc.Vec2 = null
  20. CurrentPower: number = null
  21. //当前摇杆状态 false为摇杆取消 true为摇杆中
  22. CurrentYaoGanState: boolean = false
  23. onLoad() {
  24. //绑定事件
  25. //因为摇杆很小,如果给摇杆绑定事件玩家将很难控制,摇杆的背景比较大,所以把事件都绑定在背景上是不错的选择
  26. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.move, this);//当手指在背景上移动时触发move事件
  27. this.node.on(cc.Node.EventType.TOUCH_END, this.finish, this);//当手指在目标节点区域内离开屏幕时触发finish事件
  28. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.finish, this);//当手指在目标节点区域外离开屏幕时触发finish事件
  29. }
  30. protected start(): void {
  31. cc.Camera.main.node.on(cc.Node.EventType.TOUCH_START, this.CameraMove, this);//当手指在背景上移动时触发move事件
  32. cc.Camera.main.node.on(cc.Node.EventType.TOUCH_END, this.resetYaogan, this);//当手指在背景上移动时触发move事件
  33. }
  34. posss: cc.Vec2
  35. CameraMove(event: cc.Event.EventTouch) {
  36. const pos = event.getLocation()
  37. let pp = cc.Camera.main.getScreenToWorldPoint(pos)
  38. let pp1 = this.node.parent.convertToNodeSpaceAR(pp)
  39. this.node.setPosition(pp1)
  40. this.node.opacity = 255
  41. }
  42. move(event: cc.Event.EventTouch) {//负责移动摇杆 手指移动时调用
  43. if (this.is_forbidden == false) {//如果没有禁用摇杆
  44. // event.stopPropagation()
  45. let Camera = cc.Camera.main.node.position
  46. let pos = cc.v2(event.getLocationX() + Camera.x, event.getLocationY() + Camera.y);//获取触点的坐标
  47. let pos_0 = this.parent.convertToNodeSpaceAR(pos);//将一个点转换到节点 (局部) 空间坐标系,这个坐标系以锚点为原点。
  48. pos_0.x = pos_0.x / Global.YaoGanSpeed
  49. pos_0.y = pos_0.y / Global.YaoGanSpeed
  50. if (pos_0.mag() < this.max_R) {//如果触点长度小于我们规定好的最大半径
  51. this.joystick.x = pos_0.x;//摇杆的坐标为触点坐标
  52. this.joystick.y = pos_0.y;
  53. } else {//如果不
  54. let pos = pos_0.normalizeSelf();//将触点归一化
  55. // console.log('pos', pos.x, pos.y)
  56. let x = pos.x * this.max_R;//归一化的触点坐标 × 最大半径
  57. let y = pos.y * this.max_R;
  58. this.joystick.x = x;//给摇杆坐标赋值
  59. this.joystick.y = y;
  60. }
  61. if (pos_0.y > 0) {
  62. this.bg.color = cc.Color.RED
  63. cc.systemEvent.emit(EventName.YaoGanCancel);
  64. this.CurrentYaoGanState = false
  65. cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Idle);
  66. } else {
  67. if (this.PlayEffectOnce) {
  68. this.PlayEffectOnce = false
  69. AudioManager.instance.playEffect(AudioManager.预发射音效)
  70. }
  71. this.bg.color = cc.Color.WHITE
  72. this.CurrentYaoGanState = true
  73. //方向取反
  74. this.CurrentDir = pos_0.normalizeSelf()
  75. this.CurrentDir.x = -this.CurrentDir.x
  76. this.CurrentDir.y = -this.CurrentDir.y
  77. this.CurrentPower = (cc.v2(this.joystick.x, this.joystick.y).mag() / this.max_R)
  78. cc.systemEvent.emit(EventName.YaoGanMove, this.CurrentDir, this.CurrentPower);
  79. cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Xuli);
  80. }
  81. }
  82. }
  83. PlayEffectOnce: boolean = true
  84. finish() {//摇杆弹回原位置
  85. if (this.CurrentYaoGanState) {
  86. cc.systemEvent.emit(EventName.YaoGanEnd, this.CurrentDir, this.CurrentPower);
  87. cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Fly);
  88. }
  89. this.resetYaogan()
  90. }
  91. resetYaogan() {
  92. //恢复蓄力音效
  93. this.PlayEffectOnce = true
  94. //摇杆坐标和移动向量都为(0,0)
  95. this.joystick.position = cc.v3(0, 0);
  96. this.node.opacity = 0
  97. }
  98. }
  99. //////////////////////////////