public void OnUpdate() { this.transform.MoveBy(-this.lastMovement); // Follow target if (this.Target != null) { Vector2 distance = Target.Pos.Xy - this.transform.Pos.Xy; this.transform.MoveBy(distance * this.TrailingDistance * Time.TimeMult); if (this.AlignWithTargetDirection) { this.transform.TurnTo(this.Target.Angle); } } // Add shake effect if (this.shakeGenerator != null) { this.lastMovement.Xy = this.shakeGenerator((float)Time.GameTimer.TotalSeconds * this.shakeSpeed) * this.shakeStrength; this.shakeStrength -= this.shakeDamping * Time.SPFMult * Time.TimeMult; if (this.shakeStrength <= 1f) { this.shakeGenerator = null; this.lastMovement.Xy = Vector2.Zero; } } this.lastMovement.Z = -(this.transform.Vel.Xy.Length * this.LeanBackRatio); this.transform.MoveBy(this.lastMovement); }
/// <summary> /// Adds a shake to the camera. If the previous shaking is not complete, the remaining strength /// will combine with the new one. /// This requires a custom implementation of the generator function. /// </summary> /// <param name="generator">The function that generates displacement based on time.</param> /// <param name="strength"> /// The strength of the shake, or how many pixels it will move at most in one direction (assuming /// the shake function stays in its default range of [-1, 1]). /// </param> /// <param name="time">How long, in seconds, the shake should last.</param> /// <param name="speed">The speed of the shake. The lower, the less the camera will shake.</param> public void Shake(ShakeGenerator generator, uint strength, float time, uint speed) { if (time < 0) { throw new ArgumentException("Time must be greater than 0"); } this.shakeGenerator = generator; this.shakeStrength += strength; this.shakeDamping = strength / time; this.shakeSpeed = speed; }