PhysicsManager.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { _decorator, PhysicsSystem2D, EPhysics2DDrawFlags, Vec2 } from 'cc';
  2. import { BaseSingleton } from './BaseSingleton';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('PhysicsManager')
  5. export class PhysicsManager extends BaseSingleton {
  6. // 仅用于类型声明,实例由 BaseSingleton 维护
  7. public static _instance: PhysicsManager;
  8. /** 是否启用调试绘制 */
  9. @property
  10. public debugDraw: boolean = false;
  11. /**
  12. * BaseSingleton 首次实例化回调
  13. * 在此处完成物理系统初始化,确保全局仅执行一次。
  14. */
  15. protected init() {
  16. // 启用物理系统
  17. PhysicsSystem2D.instance.enable = true;
  18. // 设置物理系统的重力为零(因为是2D平面游戏)
  19. PhysicsSystem2D.instance.gravity = new Vec2(0, 0);
  20. // // 调试绘制
  21. // PhysicsSystem2D.instance.debugDrawFlags = this.debugDraw ?
  22. // (EPhysics2DDrawFlags.Aabb |
  23. // EPhysics2DDrawFlags.Pair |
  24. // EPhysics2DDrawFlags.CenterOfMass |
  25. // EPhysics2DDrawFlags.Joint |
  26. // EPhysics2DDrawFlags.Shape) :
  27. // EPhysics2DDrawFlags.None;
  28. }
  29. /**
  30. * 获取底层 PhysicsSystem2D 单例,避免脚本直接引用 PhysicsSystem2D.instance。
  31. */
  32. public getSystem(): PhysicsSystem2D {
  33. return PhysicsSystem2D.instance;
  34. }
  35. }