CharacterMovement2D.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { _decorator, Component, Node, Vec2, v2, Prefab, Vec3 } from 'cc';
  2. import { EasyController, EasyControllerEvent } from './EasyController';
  3. const { ccclass, property } = _decorator;
  4. const tempV2 = v2();
  5. @ccclass('tgxCharacterMovement2D')
  6. export class CharacterMovement2D extends Component {
  7. @property
  8. moveSpeed: number = 100;
  9. @property
  10. needRotation: boolean = false;
  11. start() {
  12. EasyController.on(EasyControllerEvent.MOVEMENT, this.onMovement, this);
  13. EasyController.on(EasyControllerEvent.MOVEMENT_STOP, this.onMovementStop, this);
  14. }
  15. private _moveFactor: number = 0;
  16. private _moveDir: Vec2 = v2(1, 0);
  17. private _rotDegree:number = 0;
  18. private _realDegree:number = 0;
  19. private _precision:number = 1.0;
  20. public get moveDir(): Vec2 {
  21. return this._moveDir;
  22. }
  23. private _setDegree(degree:number){
  24. this._realDegree = degree;
  25. this._rotDegree = Math.floor(this._realDegree / this._precision) * this._precision;
  26. }
  27. public getRealSpeed(): number {
  28. return this.moveSpeed * this._moveFactor;
  29. }
  30. /**
  31. * @en
  32. * @zh 用于计算真实的移动方向精度。默认为 1.0,网络游戏建议设置为 5.0 到 10.0 之间
  33. */
  34. public setPrecision(p:number){
  35. if(p < 1){
  36. throw Error('precision must be greater than 1');
  37. }
  38. this._precision = p;
  39. this._rotDegree = Math.floor(this._realDegree / this._precision) * this._precision;
  40. }
  41. /**
  42. * @en current moving direction, used for calculating character movement
  43. * @zh 当前移动方向,用于计算角色移动
  44. */
  45. public get rotDegree():number{
  46. return this._rotDegree;
  47. }
  48. /**
  49. * @en current joystick direction, usually not used for calculating movement, used for setting character appearance
  50. * @zh 真实的摇杆方向,通常不用于逻辑,用于设置角色表现效果
  51. */
  52. public get realDegree():number{
  53. return this._realDegree;
  54. }
  55. onMovement(degree: number, strengthen: number) {
  56. this._setDegree(degree);
  57. if (this.needRotation) {
  58. this.node.setRotationFromEuler(0, 0, this._realDegree);
  59. }
  60. let angle = this._rotDegree / 180 * Math.PI;
  61. this._moveDir.set(Math.cos(angle), Math.sin(angle));
  62. this._moveDir.normalize();
  63. this._moveFactor = strengthen;
  64. }
  65. onMovementStop() {
  66. this._moveFactor = 0;
  67. }
  68. onDestroy() {
  69. EasyController.off(EasyControllerEvent.MOVEMENT, this.onMovement, this);
  70. EasyController.off(EasyControllerEvent.MOVEMENT_STOP, this.onMovementStop, this);
  71. }
  72. update(deltaTime: number) {
  73. if (this._moveFactor) {
  74. Vec2.multiplyScalar(tempV2, this._moveDir, this.getRealSpeed() * deltaTime);
  75. let pos = this.node.position;
  76. this.node.setPosition(pos.x + tempV2.x, pos.y + tempV2.y, pos.z);
  77. }
  78. }
  79. }