Shake.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Vec3 } from "cc";
  2. class Shake {
  3. private intensity:number;
  4. private duration:number;
  5. private shakePos:Vec3=new Vec3(0,0,0);
  6. constructor(public startPos:Vec3=new Vec3(0,0,0)) {
  7. this.duration=0;
  8. this.intensity = 0;
  9. }
  10. /**改变初起点*/
  11. setStartPos(x:number,y:number):void{
  12. this.startPos.x=x;
  13. this.startPos.y=y;
  14. }
  15. /**开始抖动
  16. * duration 时间
  17. * intensity 强度
  18. */
  19. start(duration:number=0.3,intensity:number=3.8):void{
  20. this.duration=duration;
  21. this.intensity = intensity;
  22. }
  23. /**停止抖动*/
  24. stop():Vec3 {
  25. this.shakePos.x = this.startPos.x;
  26. this.shakePos.y = this.startPos.y;
  27. this.duration=0;
  28. return this.shakePos;
  29. }
  30. getShakePos():Vec3{
  31. return this.shakePos;
  32. }
  33. action(dt: number):Vec3 {
  34. if (this.duration>0) {
  35. this.duration-=dt;
  36. if(this.duration<=0){
  37. return this.stop();
  38. }else{
  39. const randomX = Math.random() * this.intensity - this.intensity* 0.5;
  40. const randomY = Math.random() * this.intensity - this.intensity *0.5;
  41. this.shakePos.x = this.startPos.x+randomX;
  42. this.shakePos.y = this.startPos.y+randomY;
  43. return this.shakePos;
  44. }
  45. }
  46. return null;
  47. }
  48. }
  49. export default function get_new_shake(startPos:Vec3=new Vec3(0,0,0)):Shake { return new Shake(startPos);}