RotateAround.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /** 轴 */
  2. export enum Axis {
  3. /** 正 X 轴 */
  4. PositiveX,
  5. /** 正 Y 轴 */
  6. PositiveY,
  7. /** 负 X 轴 */
  8. NegativeX,
  9. /** 负 Y 轴 */
  10. NegativeY
  11. }
  12. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  13. /**
  14. * 围绕旋转组件
  15. * @see RotateAround.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/components/RotateAround.ts
  16. */
  17. @ccclass
  18. @executeInEditMode
  19. @playOnFocus
  20. export default class RotateAround extends cc.Component {
  21. @property({ type: cc.Node, tooltip: CC_DEV && '围绕旋转的目标' })
  22. public target: cc.Node = null;
  23. @property({ tooltip: CC_DEV && '顺时针旋转' })
  24. public clockwise: boolean = true;
  25. @property({ tooltip: CC_DEV && '旋转一圈花费的时间' })
  26. public timePerRound: number = 10;
  27. @property({ tooltip: CC_DEV && '是否始终面向目标节点' })
  28. public faceToTarget: boolean = false;
  29. @property({
  30. type: cc.Enum(Axis),
  31. tooltip: CC_DEV && '面向目标节点的轴:\n- PositiveX:正 X 轴\n- PositiveY:正 Y 轴\n- NegativeX:负 X 轴\n- NegativeY:负 Y 轴',
  32. visible() { return this.faceToTarget }
  33. })
  34. public faceAxis: Axis = Axis.NegativeY;
  35. @property({ tooltip: CC_DEV && '自动开始旋转' })
  36. public autoStart: boolean = false;
  37. public angle: number = 0; // 角度
  38. public radius: number = 0; // 半径
  39. @property({ displayName: CC_EDITOR && '编辑器测试' })
  40. private isRotating: boolean = false; // 标志位,是否正在旋转
  41. protected start() {
  42. cc.log('start')
  43. if (this.autoStart) this.run();
  44. }
  45. protected update(dt: number) {
  46. if (!this.isRotating || !this.target) return;
  47. // 将角度转换为弧度
  48. let radian = (Math.PI / 180) * this.angle;
  49. // 更新节点的位置
  50. this.node.x = this.target.x + this.radius * Math.cos(radian);
  51. this.node.y = this.target.y + this.radius * Math.sin(radian);
  52. // 更新节点的角度
  53. if (this.faceToTarget) {
  54. switch (this.faceAxis) {
  55. case Axis.PositiveX:
  56. this.node.angle = this.angle + 180;
  57. break;
  58. case Axis.PositiveY:
  59. this.node.angle = this.angle + 90;
  60. break;
  61. case Axis.NegativeX:
  62. this.node.angle = this.angle;
  63. break;
  64. case Axis.NegativeY:
  65. this.node.angle = this.angle - 90;
  66. break;
  67. }
  68. }
  69. // 计算下一帧的角度
  70. let anglePerFrame = dt * (360 / this.timePerRound);
  71. if (this.clockwise) this.angle -= anglePerFrame;
  72. else this.angle += anglePerFrame;
  73. // 重置角度,避免数值过大
  74. if (this.angle >= 360) this.angle %= 360;
  75. else if (this.angle <= -360) this.angle %= -360;
  76. }
  77. /**
  78. * 开始围绕目标节点旋转
  79. * @param target 目标节点
  80. * @param clockwise 是否顺时针旋转
  81. * @param timePerRound 旋转一圈的时间
  82. * @param faceToTarget 是否始终面向目标节点
  83. * @param faceAxis 面向目标节点的轴
  84. */
  85. public run(target?: cc.Node, clockwise?: boolean, timePerRound?: number, faceToTarget?: boolean, faceAxis?: Axis) {
  86. if (target) this.target = target;
  87. if (clockwise) this.clockwise = clockwise;
  88. if (timePerRound) this.timePerRound = timePerRound;
  89. if (faceToTarget) this.faceToTarget = faceToTarget;
  90. if (faceAxis) this.faceAxis = faceAxis;
  91. if (!this.target) return cc.log('No target!');
  92. // 计算初始角度和半径
  93. this.angle = this.getAngle(this.target.getPosition(), this.node.getPosition());
  94. this.radius = this.getDistance(this.target.getPosition(), this.node.getPosition());
  95. // 开始
  96. this.isRotating = true;
  97. }
  98. /**
  99. * 停止旋转
  100. */
  101. public stop() {
  102. this.isRotating = false;
  103. }
  104. /**
  105. * 获取两点间的角度
  106. * @param p1 点1
  107. * @param p2 点2
  108. * @see MathUtil.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/utils/MathUtil.ts
  109. */
  110. public getAngle(p1: cc.Vec2, p2: cc.Vec2): number {
  111. return Math.atan((p2.y - p1.y) / (p2.x - p1.x));
  112. }
  113. /**
  114. * 获取两点间的距离
  115. * @param p1 点1
  116. * @param p2 点2
  117. * @see MathUtil.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/utils/MathUtil.ts
  118. */
  119. public getDistance(p1: cc.Vec2, p2: cc.Vec2): number {
  120. return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
  121. }
  122. }