Move.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { App } from "../Manager/App";
  2. import player from "../player";
  3. export enum LoopType {
  4. Loop = 0,
  5. Once = 1,
  6. PingPong = 2
  7. }
  8. export enum LineType {
  9. any,
  10. Vertical,
  11. Horizontal
  12. }
  13. export enum ShapeType {
  14. StraightLine,
  15. PathLine,
  16. CircleLine
  17. }
  18. const { ccclass, property } = cc._decorator;
  19. @ccclass
  20. export default class NewClass extends cc.Component {
  21. /**移动速度 */
  22. @property
  23. public speed: number = 1;
  24. /**移动pingpong返回速度 */
  25. @property
  26. public backSpeed: number = 1;
  27. private currentSpeed: number = 1;
  28. /**循环类型 */
  29. @property({ type: cc.Enum(LoopType) })
  30. loopType: LoopType = LoopType.Loop;
  31. /**移动形状类型 */
  32. @property({ type: cc.Enum(ShapeType) })
  33. shapeType: ShapeType = ShapeType.StraightLine;
  34. /**直线类型 */
  35. @property({ type: cc.Enum(LineType) })
  36. lineType: LineType = LineType.any;
  37. /**路径个数 */
  38. public pathNum: number = 1;
  39. /** 是否延时开始*/
  40. @property
  41. public isDelayStart: boolean = false;
  42. /** 延时时间*/
  43. @property
  44. public delayTime: number = 0;
  45. /**圆标志 */
  46. private _circleFlag: boolean = false;
  47. /**加标志 */
  48. private _addFlag: boolean = true;
  49. /** 目标位置组*/
  50. private posArr: any = [];
  51. /** 当前位置下标*/
  52. private currentPosIndex = 0;
  53. /** 初始位置*/
  54. private startPos: any = null;
  55. /**目标位置 */
  56. private targetPos: any = null;
  57. /**半径 */
  58. private radius: number = 0;
  59. /**角度 */
  60. @property
  61. private angle: number = 0;
  62. @property
  63. private isPlaySound: boolean = false;
  64. /** 坐标组 */
  65. @property([cc.Vec2])
  66. public positionArray: cc.Vec2[] = [];
  67. private startMove: boolean = false;
  68. @property(cc.ParticleSystem)
  69. public particle: cc.ParticleSystem = null;
  70. // onLoad () {}
  71. start() {
  72. this.startPos = this.positionArray[0];
  73. this.currentSpeed = this.speed;
  74. if (this.isDelayStart) this.scheduleOnce(() => { this.SetStutas(this.positionArray); }, this.delayTime);
  75. else this.SetStutas(this.positionArray);
  76. }
  77. /**设置状态 */
  78. private SetStutas(tempPosArr) {
  79. this._circleFlag = false;
  80. this.currentPosIndex = 1;
  81. if (this.shapeType == ShapeType.PathLine) {
  82. this.posArr = tempPosArr;
  83. // this.posArr.unshift(this.startPos);
  84. } else {
  85. this.targetPos = tempPosArr[1];
  86. if (this.shapeType == ShapeType.StraightLine) {
  87. if (this.lineType == LineType.Vertical) {
  88. this.targetPos.x = this.node.x;
  89. } else if (this.lineType == LineType.Horizontal) {
  90. this.targetPos.y = this.node.y;
  91. }
  92. this.posArr = [this.startPos, this.targetPos];
  93. // console.log('this.posArr', this.posArr);
  94. } else if (this.shapeType == ShapeType.CircleLine) {
  95. this._circleFlag = true;
  96. this.radius = this.startPos.sub(this.targetPos).mag();
  97. }
  98. }
  99. this.startMove = true;
  100. };
  101. update(dt) {
  102. if (!this.startMove) return;
  103. if (this._circleFlag) this.CircleMove();
  104. else this.PathMove();
  105. };
  106. /**圆形运动 */
  107. private CircleMove() {
  108. this.node.position = this.AroundMove(this.targetPos, this.radius, this.angle);
  109. // console.log(' this.node.position ', this.node.position, 'this._角度 ', this._角度);
  110. if (this.angle >= 360) {
  111. if (this.loopType == LoopType.Once) return;
  112. if (this.loopType == LoopType.Loop) {
  113. this.angle = 0;
  114. } else if (this.loopType == LoopType.PingPong) {
  115. this.currentSpeed = -Math.abs(this.currentSpeed);
  116. }
  117. } else if (this.angle <= 0) {
  118. if (this.loopType == LoopType.PingPong) this.currentSpeed = Math.abs(this.currentSpeed);
  119. }
  120. this.angle += this.currentSpeed;
  121. };
  122. /**路径移动 */
  123. private PathMove() {
  124. let _targetPos = this.posArr[this.currentPosIndex];
  125. let _distance = this.node.position.sub(_targetPos).mag();
  126. if (_distance < this.currentSpeed) {
  127. if (this.loopType == LoopType.Once) return;
  128. if (this.loopType == LoopType.Loop) {
  129. if (this.currentPosIndex < this.posArr.length - 1) {
  130. this.currentPosIndex++;
  131. } else {
  132. this.currentPosIndex = 1;
  133. this.node.position = this.posArr[0];
  134. return;
  135. }
  136. } else if (this.loopType == LoopType.PingPong) {
  137. this.node.position = _targetPos;
  138. if (this._addFlag) {
  139. if (this.currentPosIndex < this.posArr.length - 1) {
  140. this.currentPosIndex++;
  141. this.currentSpeed = this.speed;
  142. } else {
  143. this._addFlag = false;
  144. }
  145. } else {
  146. if (this.currentPosIndex > 0) {
  147. this.currentPosIndex--;
  148. this.currentSpeed = this.backSpeed;
  149. this.PlayAudio();
  150. } else {
  151. this._addFlag = true;
  152. }
  153. }
  154. }
  155. }
  156. var returnPos = this.TowardsMove(_targetPos, this.node.position, this.currentSpeed);//, dt
  157. this.node.position = returnPos;
  158. };
  159. private PlayAudio() {
  160. if (!this.isPlaySound || !App.SoundManager.allowPlayEffect) return;
  161. var pos1 = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
  162. var newVec2 = cc.find('Canvas').convertToNodeSpaceAR(pos1);
  163. if (this.IsCameraSaw(newVec2)) {
  164. this.node.getComponent(cc.AudioSource).play();
  165. if (this.particle) this.particle.resetSystem();
  166. }
  167. }
  168. private IsCameraSaw(targetPos: any): boolean {
  169. var heights = this.node.height;
  170. var posY = player.getInstance().node.y + cc.winSize.height + heights;
  171. var posY1 = player.getInstance().node.y - cc.winSize.height - heights + player.getInstance().cameraOffsetY;
  172. if (targetPos.y < posY && targetPos.y > posY1) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. private AroundMove(axis: any, radius: any, angle: any) {
  179. var x1 = axis.x + radius * Math.sin(angle * Math.PI / 180);
  180. var y1 = axis.y + radius * Math.cos(angle * Math.PI / 180);
  181. return new cc.Vec3(x1, y1, 0);
  182. }
  183. private TowardsMove(endPos: any, movePos: any, speed: any) { //旋转角度没求//, dt
  184. var moveDir = endPos.sub(movePos).normalize();
  185. var vx = moveDir.x * speed;
  186. var vy = moveDir.y * speed;
  187. var returnX = movePos.x + vx;//* dt
  188. var returnY = movePos.y + vy;//* dt
  189. return new cc.Vec3(returnX, returnY, 0);
  190. };
  191. }