CameraUtils.ts 1.8 KB

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