using System; using FarseerPhysics.Dynamics; using Microsoft.Xna.Framework; namespace FarseerPhysics.Controllers { public abstract class AbstractForceController : Controller { #region DecayModes enum /// /// Modes for Decay. Actual Decay must be implemented in inheriting /// classes /// public enum DecayModes { None, Step, Linear, InverseSquare, Curve } #endregion #region ForceTypes enum /// /// Forcetypes are used in the decay math to properly get the distance. /// They are also used to draw a representation in DebugView /// public enum ForceTypes { Point, Line, Area } #endregion #region TimingModes enum /// /// Timing Modes /// Switched: Standard on/off mode using the baseclass enabled property /// Triggered: When the Trigger() method is called the force is active /// for a specified Impulse Length /// Curve: Still to be defined. The basic idea is having a Trigger /// combined with a curve for the strength /// public enum TimingModes { Switched, Triggered, Curve } #endregion /// /// Curve to be used for Decay in Curve mode /// public Curve DecayCurve; /// /// The Forcetype of the instance /// public ForceTypes ForceType; /// /// Provided for reuse to provide Variation functionality in /// inheriting classes /// protected Random Randomize; /// /// Curve used by Curve Mode as an animated multiplier for the force /// strength. /// Only positions between 0 and 1 are considered as that range is /// stretched to have ImpulseLength. /// public Curve StrengthCurve; /// /// Constructor /// public AbstractForceController() : base(ControllerType.AbstractForceController) { Enabled = true; Strength = 1.0f; Position = new Vector2(0, 0); MaximumSpeed = 100.0f; TimingMode = TimingModes.Switched; ImpulseTime = 0.0f; ImpulseLength = 1.0f; Triggered = false; StrengthCurve = new Curve(); Variation = 0.0f; Randomize = new Random(1234); DecayMode = DecayModes.None; DecayCurve = new Curve(); DecayStart = 0.0f; DecayEnd = 0.0f; StrengthCurve.Keys.Add(new CurveKey(0, 5)); StrengthCurve.Keys.Add(new CurveKey(0.1f, 5)); StrengthCurve.Keys.Add(new CurveKey(0.2f, -4)); StrengthCurve.Keys.Add(new CurveKey(1f, 0)); } /// /// Overloaded Contstructor with supplying Timing Mode /// /// public AbstractForceController(TimingModes mode) : base(ControllerType.AbstractForceController) { TimingMode = mode; switch (mode) { case TimingModes.Switched: Enabled = true; break; case TimingModes.Triggered: Enabled = false; break; case TimingModes.Curve: Enabled = false; break; } } /// /// Global Strength of the force to be applied /// public float Strength { get; set; } /// /// Position of the Force. Can be ignored (left at (0,0) for forces /// that are not position-dependent /// public Vector2 Position { get; set; } /// /// Maximum speed of the bodies. Bodies that are travelling faster are /// supposed to be ignored /// public float MaximumSpeed { get; set; } /// /// Maximum Force to be applied. As opposed to Maximum Speed this is /// independent of the velocity of /// the affected body /// public float MaximumForce { get; set; } /// /// Timing Mode of the force instance /// public TimingModes TimingMode { get; set; } /// /// Time of the current impulse. Incremented in update till /// ImpulseLength is reached /// public float ImpulseTime { get; private set; } /// /// Length of a triggered impulse. Used in both Triggered and Curve Mode /// public float ImpulseLength { get; set; } /// /// Indicating if we are currently during an Impulse /// (Triggered and Curve Mode) /// public bool Triggered { get; private set; } /// /// Variation of the force applied to each body affected /// !! Must be used in inheriting classes properly !! /// public float Variation { get; set; } /// /// See DecayModes /// public DecayModes DecayMode { get; set; } /// /// Start of the distance based Decay. To set a non decaying area /// public float DecayStart { get; set; } /// /// Maximum distance a force should be applied /// public float DecayEnd { get; set; } /// /// Calculate the Decay for a given body. Meant to ease force /// development and stick to the DRY principle and provide unified and /// predictable decay math. /// /// The body to calculate decay for /// A multiplier to multiply the force with to add decay /// support in inheriting classes protected float GetDecayMultiplier(Body body) { //TODO: Consider ForceType in distance calculation! float distance = (body.Position - Position).Length(); switch (DecayMode) { case DecayModes.None: { return 1.0f; } case DecayModes.Step: { if (distance < DecayEnd) return 1.0f; else return 0.0f; } case DecayModes.Linear: { if (distance < DecayStart) return 1.0f; if (distance > DecayEnd) return 0.0f; return (DecayEnd - DecayStart / distance - DecayStart); } case DecayModes.InverseSquare: { if (distance < DecayStart) return 1.0f; else return 1.0f / ((distance - DecayStart) * (distance - DecayStart)); } case DecayModes.Curve: { if (distance < DecayStart) return 1.0f; else return DecayCurve.Evaluate(distance - DecayStart); } default: return 1.0f; } } /// /// Triggers the trigger modes (Trigger and Curve) /// public void Trigger() { Triggered = true; ImpulseTime = 0; } /// /// Inherited from Controller /// Depending on the TimingMode perform timing logic and call ApplyForce() /// /// public override void Update(float dt) { switch (TimingMode) { case TimingModes.Switched: { if (Enabled) { ApplyForce(dt, Strength); } break; } case TimingModes.Triggered: { if (Enabled && Triggered) { if (ImpulseTime < ImpulseLength) { ApplyForce(dt, Strength); ImpulseTime += dt; } else { Triggered = false; } } break; } case TimingModes.Curve: { if (Enabled && Triggered) { if (ImpulseTime < ImpulseLength) { ApplyForce(dt, Strength * StrengthCurve.Evaluate(ImpulseTime)); ImpulseTime += dt; } else { Triggered = false; } } break; } } } /// /// Apply the force supplying strength (wich is modified in Update() /// according to the TimingMode /// /// /// The strength public abstract void ApplyForce(float dt, float strength); } }