Follow2DMove.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { Vec2 } from "cc";
  2. //跟随移动
  3. export class Follow2DMove {
  4. private _pos:Vec2;//起始点
  5. private _dir:Vec2;//起始方向
  6. private _rot:number=0;
  7. private _speed:number;//移动速度
  8. private _speed_add:number;//移动加速度
  9. private _ro_speed:number;//转向速度
  10. private _ro_speed_add:number;//转向加速度
  11. /***
  12. *
  13. */
  14. constructor(pos:Vec2,dir:Vec2|number,speed:number=410,speed_add:number=200,ro_speed:number=60,ro_speed_add:number=150){
  15. this._pos = new Vec2(pos.x,pos.y);
  16. if(typeof(dir)=='number'){
  17. this.setRot(dir);
  18. }else{
  19. this.setRotByDir(dir);
  20. }
  21. this._speed =speed;
  22. this._speed_add= speed_add;
  23. this._ro_speed = ro_speed;
  24. this._ro_speed_add = ro_speed_add;
  25. //
  26. }
  27. public get pos():Vec2{
  28. return this._pos;
  29. }
  30. public get dir():Vec2{
  31. return this._dir;
  32. }
  33. public get rot():number{
  34. return this._rot;
  35. }
  36. public setRotByDir(dir:Vec2){
  37. this._dir = dir.clone();
  38. this._rot = this.DirToRot(this._dir);
  39. }
  40. public setRot(an:number){
  41. this._rot = an;
  42. this._dir = this.RotToDir(this._rot);
  43. }
  44. private readonly _dr:number= 180/Math.PI;
  45. private readonly _rd:number = Math.PI / 180;
  46. private DirToRot(dir:Vec2):number{
  47. let radian = dir.signAngle(new Vec2(1,0));
  48. let angle =-this._dr* radian;
  49. return(angle);
  50. }
  51. private RotToDir(angle:number):Vec2{
  52. let radian = this._rd *angle;
  53. let cos = Math.cos(radian);
  54. let sin = Math.sin(radian);
  55. return new Vec2(cos, sin).normalize();
  56. }
  57. //
  58. /**本坐标到一个点的方向*/
  59. private GetDirXY(x:number,y:number):Vec2{
  60. return new Vec2(x-this._pos.x,y-this._pos.y).normalize();
  61. }
  62. private RotationDir(dir:Vec2,angle:number):Vec2
  63. {
  64. let radian = Math.PI / 180 *angle;
  65. let sin = Math.sin(radian);
  66. var cos = Math.cos(radian);
  67. var newX = dir.x * cos + dir.y * sin;
  68. var newY = dir.x * -sin + dir.y * cos;
  69. dir.x=newX;
  70. dir.y=newY;
  71. return dir;
  72. }
  73. /**追踪目标x,y*/
  74. public FollowXY(x:number,y:number,dt:number):void
  75. {
  76. let mbdir = this.GetDirXY(x,y);//目标方向
  77. let vdir = this._dir;//当前方向
  78. let jd = 180 / Math.PI * vdir.signAngle(mbdir);
  79. this._ro_speed += this._ro_speed_add*dt;
  80. let ro = this._ro_speed*dt;
  81. const jdabs= Math.abs(jd);
  82. if(ro>=jdabs){
  83. ro = jd>0 ? -jdabs:jdabs;
  84. }else{
  85. ro = jd>0 ? -ro:ro;
  86. }
  87. if(ro!=0)this.RotationDir(vdir,ro);
  88. this.setRotByDir(vdir);
  89. this.Move(dt);
  90. }
  91. private _fame_speed:number=0;
  92. public get fame_speed():number{
  93. return this._fame_speed;
  94. }
  95. /**向前移动不追踪*/
  96. public Move(dt:number):void{
  97. this._speed+=this._speed_add*dt;
  98. this._fame_speed=this._speed*dt;
  99. let v = this._dir.clone().multiplyScalar(this._fame_speed);
  100. this._pos.x+=v.x;
  101. this._pos.y+=v.y;
  102. }
  103. /**向前移动距离*/
  104. public MoveDis(dis:number):void{
  105. let v = this._dir.clone().multiplyScalar(dis);
  106. this._pos.x+=v.x;
  107. this._pos.y+=v.y;
  108. }
  109. }