CameraUtils.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Component, Node, Vec3, director, CameraComponent } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('CameraUtils')
  4. export class CameraUtils extends Component {
  5. /* class member could be defined like this */
  6. // dummy = '';
  7. /* use `property` decorator if your want the member to be serializable */
  8. // @property
  9. // serializableDummy = 0;
  10. private min:Vec3;
  11. private max:Vec3;
  12. private out:Vec3=new Vec3();
  13. private startTime:number;
  14. private endTime:number;
  15. private startPos:Vec3=new Vec3();
  16. private shakeing:boolean;
  17. private camera:CameraComponent;
  18. /**
  19. * 抖动
  20. * @param time 持续时间
  21. * @param min 最小值
  22. * @param max 最大值
  23. */
  24. shake(time:number,min:Vec3,max:Vec3):void{
  25. this.min=min;
  26. this.max=max;
  27. this.startTime=director.getCurrentTime();
  28. this.endTime=this.startTime+time;
  29. this.shakeing=true;
  30. this.startPos.set(this.node.position.x,this.node.position.y,this.node.position.z);
  31. this.camera=this.node.getComponent(CameraComponent);
  32. }
  33. start () {
  34. // Your initialization goes here.
  35. }
  36. update (deltaTime: number) {
  37. if(this.shakeing){
  38. let currentTime:number=director.getCurrentTime();
  39. if(currentTime<this.endTime){
  40. this.randomV3(this.min,this.max,this.out);
  41. this.out.x+=this.startPos.x;
  42. this.out.y+=this.startPos.y;
  43. this.out.z+=this.startPos.z;
  44. this.node.position=this.out;
  45. }else{
  46. this.node.position=this.startPos;
  47. }
  48. }
  49. }
  50. private randomV3(min:Vec3,max:Vec3,out:Vec3):void{
  51. let dx:number=max.x-min.x;
  52. let dy:number=max.y-min.y;
  53. let dz:number=max.z-min.z;
  54. out.x=min.x+Math.random()*dx;
  55. out.y=min.y+Math.random()*dy;
  56. out.z=min.z+Math.random()*dz;
  57. }
  58. }