1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import { Vec3 } from "cc";
- class Shake {
- private intensity:number;
- private duration:number;
- private shakePos:Vec3=new Vec3(0,0,0);
- constructor(public startPos:Vec3=new Vec3(0,0,0)) {
- this.duration=0;
- this.intensity = 0;
- }
- /**改变初起点*/
- setStartPos(x:number,y:number):void{
- this.startPos.x=x;
- this.startPos.y=y;
- }
- /**开始抖动
- * duration 时间
- * intensity 强度
- */
- start(duration:number=0.3,intensity:number=3.8):void{
- this.duration=duration;
- this.intensity = intensity;
- }
- /**停止抖动*/
- stop():Vec3 {
- this.shakePos.x = this.startPos.x;
- this.shakePos.y = this.startPos.y;
- this.duration=0;
- return this.shakePos;
- }
- getShakePos():Vec3{
- return this.shakePos;
- }
- action(dt: number):Vec3 {
- if (this.duration>0) {
- this.duration-=dt;
- if(this.duration<=0){
- return this.stop();
- }else{
- const randomX = Math.random() * this.intensity - this.intensity* 0.5;
- const randomY = Math.random() * this.intensity - this.intensity *0.5;
- this.shakePos.x = this.startPos.x+randomX;
- this.shakePos.y = this.startPos.y+randomY;
- return this.shakePos;
- }
- }
- return null;
- }
- }
- export default function get_new_shake(startPos:Vec3=new Vec3(0,0,0)):Shake { return new Shake(startPos);}
|