2
0

Controller.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using FarseerPhysics.Dynamics;
  3. namespace FarseerPhysics.Controllers
  4. {
  5. [Flags]
  6. public enum ControllerType
  7. {
  8. GravityController = (1 << 0),
  9. VelocityLimitController = (1 << 1),
  10. AbstractForceController = (1 << 2),
  11. BuoyancyController = (1 << 3),
  12. }
  13. public struct ControllerFilter
  14. {
  15. public ControllerType ControllerFlags;
  16. /// <summary>
  17. /// Ignores the controller. The controller has no effect on this body.
  18. /// </summary>
  19. /// <param name="controller">The controller type.</param>
  20. public void IgnoreController(ControllerType controller)
  21. {
  22. ControllerFlags |= controller;
  23. }
  24. /// <summary>
  25. /// Restore the controller. The controller affects this body.
  26. /// </summary>
  27. /// <param name="controller">The controller type.</param>
  28. public void RestoreController(ControllerType controller)
  29. {
  30. ControllerFlags &= ~controller;
  31. }
  32. /// <summary>
  33. /// Determines whether this body ignores the the specified controller.
  34. /// </summary>
  35. /// <param name="controller">The controller type.</param>
  36. /// <returns>
  37. /// <c>true</c> if the body has the specified flag; otherwise, <c>false</c>.
  38. /// </returns>
  39. public bool IsControllerIgnored(ControllerType controller)
  40. {
  41. return (ControllerFlags & controller) == controller;
  42. }
  43. }
  44. public abstract class Controller : FilterData
  45. {
  46. public bool Enabled;
  47. public World World;
  48. private ControllerType _type;
  49. public Controller(ControllerType controllerType)
  50. {
  51. _type = controllerType;
  52. }
  53. public override bool IsActiveOn(Body body)
  54. {
  55. if (body.ControllerFilter.IsControllerIgnored(_type))
  56. return false;
  57. return base.IsActiveOn(body);
  58. }
  59. public abstract void Update(float dt);
  60. }
  61. }