Exemplo n.º 1
0
        public void PerformChase(float deltaSeconds)
        {
            IsHoming = false;
            Body body = MyBody;

            if (Target != null)
            {
                SpatialData worldSpatial  = this.GetWorldSpatialData();
                SpatialData targetSpatial = Target.GetWorldSpatialData();
                Vector2     targetDelta   = targetSpatial.Position - worldSpatial.Position;
                targetDelta.GetDirectionAndLength(out Vector2 targetDir, out float targetDistance);

                if (targetDistance > TargetRange)
                {
                    // Don't do anything.
                }
                else if (targetDistance > TargetInnerRange)
                {
                    IsHoming = true;
                    Vector2 velocity = targetDir * Speed;

                    switch (HomingType)
                    {
                    case HomingType.ConstantSpeed:
                    {
                        if (body != null)
                        {
                            if (body.IsStatic)
                            {
                                Vector2 deltaPosition = velocity * deltaSeconds;
                                body.Position += deltaPosition;
                            }
                            else
                            {
                                body.LinearVelocity = velocity;
                            }
                        }
                        else
                        {
                            Vector2 deltaPosition = velocity * deltaSeconds;
                            Vector2 newPosition   = worldSpatial.Position + deltaPosition;
                            Spatial.SetWorldPosition(newPosition);
                        }
                    }
                    break;

                    case HomingType.ConstantAcceleration:
                    {
                        if (body != null)
                        {
                            Vector2 impulse = body.Mass * velocity;
                            body.ApplyLinearImpulse(ref impulse);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    break;

                    default: throw new ArgumentException(nameof(HomingType));
                    }
                }
            }

            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);
                });
            }
        }