2 using System.Collections.Generic;
6 namespace Jypeli.GameObjects
11 internal abstract class Oscillator : Updatable, Destroyable
13 protected double t = 0;
15 public IGameObject Object;
16 public Waveform Waveform;
18 public bool IsUpdated {
get {
return true; } }
19 public bool IsDestroyed {
get;
private set; }
21 public event Action Destroyed;
23 public Oscillator( IGameObject obj, Waveform waveform )
26 this.Waveform = waveform;
29 public void Update( Time time )
31 double dt = time.SinceLastUpdate.TotalSeconds;
32 if ( dt == 0 )
return;
36 if ( Waveform.GetDampingMultiplier( t ) < 1e-12 )
45 protected abstract void Apply(
double dt );
50 if ( Destroyed != null ) Destroyed();
53 public abstract void Stop();
59 internal class LinearOscillator : Oscillator
64 public LinearOscillator( IGameObject obj, Vector axis, Waveform waveform )
65 : base( obj, waveform )
67 this.Axis = axis.Normalize();
68 this.Center = obj.Position - Axis * waveform.Amplitude * Math.Sin( waveform.Phase );
69 this.t = -Game.Time.SinceLastUpdate.TotalSeconds;
77 public Vector GetOffset()
79 return Axis * Waveform.GetValue( t );
82 protected override void Apply(
double dt )
84 if ( Object is PhysicsObject )
86 PhysicsObject physObj = (PhysicsObject)Object;
87 double otherVel = physObj.Velocity.ScalarProjection( Axis.LeftNormal );
88 physObj.Velocity = otherVel * Axis.LeftNormal + Axis * Waveform.GetDerivative( t, dt );
92 Object.Position = Center + GetOffset();
96 public override void Stop()
98 if ( Object is PhysicsObject )
100 ( (PhysicsObject)Object ).StopAxial( Axis );
108 internal class AngularOscillator : Oscillator
110 public double Direction;
112 public Waveform Waveform;
114 public AngularOscillator( IGameObject obj,
double dir, Waveform waveform )
115 : base( obj, waveform )
117 this.Direction = Math.Sign( dir );
118 this.Waveform = waveform;
119 this.Center = obj.Angle.Radians;
122 protected override void Apply(
double dt )
124 if ( Object is PhysicsObject )
126 PhysicsObject physObj = (PhysicsObject)Object;
127 physObj.AngularVelocity = Direction * Waveform.GetDerivative( t, dt );
131 Object.Angle = Angle.FromRadians( Center + Direction * Waveform.GetValue( t ) );
135 public override void Stop()
137 if ( Object is PhysicsObject )
139 ( (PhysicsObject)Object ).StopAngular();