public override void Update(float deltaSeconds) { base.Update(deltaSeconds); if (DepthReference != null) { SpatialData spatial = DepthReference.GetWorldSpatialData(); RenderDepth = Global.Game.CalcDepth(spatial, Owner.Layer); if (DebugDrawingEnabled) { Global.Game.DebugDrawCommands.Add(view => { view.DrawPoint(spatial.Position, Conversion.ToMeters(3), Color.Navy); }); } } }
public void PerformChase(float deltaSeconds) { Body body = MyBody; if (Target != null) { Vector2 worldPosition = this.GetWorldSpatialData().Position; Vector2 targetPosition = Target.GetWorldSpatialData().Position; Vector2 targetDelta = targetPosition - worldPosition; targetDelta.GetDirectionAndLength(out Vector2 targetDir, out float targetDistance); if (targetDistance > TargetRange) { // Note(manu): Without this value the target would always be out of range until it has _completely_ stopped. const float compensationForSlowMovement = 0.01f; Vector2 newPosition = targetPosition - (targetDir * (TargetRange - compensationForSlowMovement)); if (body != null) { Debug.Assert(body.BodyType == BodyType.Static); body.Position = newPosition; } else { Spatial.SetWorldPosition(newPosition); } } else if (targetDistance > TargetInnerRange) { float relevantTargetDistance = targetDistance - TargetInnerRange; float weightedDistance = relevantTargetDistance * SpeedFactor; Vector2 velocity = targetDir * weightedDistance; if (body != null) { if (body.BodyType == BodyType.Static) { Vector2 deltaPosition = velocity * deltaSeconds; body.Position += deltaPosition; } else { // TODO(manu): Setting the linear velocity is kind of bad. Can we do this with an impulse instead? // Note(manu): This code path is untested (2017-06-15). body.LinearVelocity = velocity; } } else { Vector2 deltaPosition = velocity * deltaSeconds; Vector2 newPosition = worldPosition + deltaPosition; Spatial.SetWorldPosition(newPosition); } } } if (DebugDrawingEnabled) { Global.Game.DebugDrawCommands.Add(view => { Vector2 p = this.GetWorldSpatialData().Position; view.DrawPoint(p, Conversion.ToMeters(3.0f), Color.Turquoise); view.DrawCircle(p, TargetInnerRange, Color.Yellow); view.DrawCircle(p, TargetRange, Color.Blue); }); } }