RotateAround.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * @author 薛鸿潇
  16. */
  17. @ccclass
  18. @executeInEditMode()
  19. @playOnFocus()
  20. export default class RotateAround extends cc.Component {
  21. @property({ type: cc.Node, tooltip: '围绕旋转的目标' })
  22. public target: cc.Node = null;
  23. @property({ tooltip: '顺时针旋转' })
  24. public clockwise: boolean = true;
  25. @property({ tooltip: '旋转一圈花费的时间' })
  26. public timePerRound: number = 10;
  27. @property({ tooltip: '是否始终面向目标节点' })
  28. public faceToTarget: boolean = false;
  29. @property({
  30. type: cc.Enum(Axis),
  31. tooltip: '面向目标节点的轴:\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: '自动开始旋转' })
  36. public auto_start: boolean = false;
  37. /** 编辑器预览 */
  38. private _edit_play: boolean = false;
  39. @property({ displayName: '编辑器预览' })
  40. get edit_play(): boolean {
  41. if (this._edit_play && !this.radius) this.run();
  42. this.isRotating = this._edit_play;
  43. return this._edit_play;
  44. }
  45. set edit_play(v: boolean) {
  46. this._edit_play = v;
  47. }
  48. /** 角度 */
  49. public angle: number = 0;
  50. /** 半径 */
  51. public radius: number = 0;
  52. /** 标志位,是否正在旋转 */
  53. private isRotating: boolean = false;
  54. protected start() {
  55. if (this.auto_start) this.run();
  56. }
  57. protected update(dt: number) {
  58. if (!this.isRotating || !this.target) return;
  59. // 将角度转换为弧度
  60. let radian = (Math.PI / 180) * this.angle;
  61. // 更新节点的位置
  62. this.node.x = this.target.x + this.radius * Math.cos(radian);
  63. this.node.y = this.target.y + this.radius * Math.sin(radian);
  64. // 更新节点的角度
  65. if (this.faceToTarget) {
  66. switch (this.faceAxis) {
  67. case Axis.positiveX:
  68. this.node.angle = this.angle + 180;
  69. break;
  70. case Axis.positiveY:
  71. this.node.angle = this.angle + 90;
  72. break;
  73. case Axis.negativeX:
  74. this.node.angle = this.angle;
  75. break;
  76. case Axis.negativeY:
  77. this.node.angle = this.angle - 90;
  78. break;
  79. }
  80. }
  81. // 计算下一帧的角度
  82. let anglePerFrame = dt * (360 / this.timePerRound);
  83. if (this.clockwise) this.angle -= anglePerFrame;
  84. else this.angle += anglePerFrame;
  85. // 重置角度,避免数值过大
  86. if (this.angle >= 360) this.angle %= 360;
  87. else if (this.angle <= -360) this.angle %= -360;
  88. }
  89. /**
  90. * 开始围绕目标节点旋转
  91. * @param target 目标节点
  92. * @param clockwise 是否顺时针旋转
  93. * @param timePerRound 旋转一圈的时间
  94. * @param faceToTarget 是否始终面向目标节点
  95. * @param faceAxis 面向目标节点的轴
  96. */
  97. public run(target?: cc.Node, clockwise?: boolean, timePerRound?: number, faceToTarget?: boolean, faceAxis?: Axis) {
  98. if (target) this.target = target;
  99. if (clockwise) this.clockwise = clockwise;
  100. if (timePerRound) this.timePerRound = timePerRound;
  101. if (faceToTarget) this.faceToTarget = faceToTarget;
  102. if (faceAxis) this.faceAxis = faceAxis;
  103. if (!this.target) return cc.log('No target!');
  104. if (this.isRotating) return;
  105. // 计算初始角度和半径
  106. this.angle = this.getAngle(this.target.getPosition(), this.node.getPosition());
  107. this.radius = this.getDistance(this.target.getPosition(), this.node.getPosition());
  108. // 开始
  109. this.isRotating = true;
  110. }
  111. /**
  112. * 停止旋转
  113. */
  114. public stop() {
  115. this.isRotating = false;
  116. }
  117. /**
  118. * 获取两点间的角度
  119. * @param p1 点1
  120. * @param p2 点2
  121. */
  122. public getAngle(p1: cc.Vec2, p2: cc.Vec2): number {
  123. return Math.atan((p2.y - p1.y) / (p2.x - p1.x));
  124. }
  125. /**
  126. * 获取两点间的距离
  127. * @param p1 点1
  128. * @param p2 点2
  129. */
  130. public getDistance(p1: cc.Vec2, p2: cc.Vec2): number {
  131. return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
  132. }
  133. }