Exemplo n.º 1
0
 public ActionMoveUnit2D(IUnit2D unit2D, FloatPoint2D destanationPoint, float speedPerSecond, IActions nextAction = null)
 {
     MovingUnit       = unit2D;
     DestanationPoint = destanationPoint;
     SpeedPerSecond   = speedPerSecond;
     NextAction       = nextAction;
     LastStepTime     = default;
 }
Exemplo n.º 2
0
        public FloatPoint2D MovingVector()
        {
            FloatPoint2D delta = new FloatPoint2D(DestanationPoint.X - MovingUnit.Position.X, DestanationPoint.Y - MovingUnit.Position.Y);
            float        len   = delta.VectorLength();

            if (len < 0.0001)
            {
                return(new FloatPoint2D(0, 0));
            }
            if (Math.Abs(delta.X) < 0.0001)
            {
                return(new FloatPoint2D(0, 1));
            }
            if (Math.Abs(delta.Y) < 0.0001)
            {
                return(new FloatPoint2D(1, 0));
            }
            return(new FloatPoint2D(delta.X / len, delta.Y / len));
        }
Exemplo n.º 3
0
        public override IActions Progress()
        {
            if (LastStepTime == default)
            {
                LastStepTime = DateTime.Now;
            }
            float StepLength  = (float)(DateTime.Now.Subtract(LastStepTime).TotalMilliseconds / 1000.0) * SpeedPerSecond;
            float sqrDistance = MovingUnit.Position.SquareDistanceF(DestanationPoint);

            if (sqrDistance < MathF.Pow(StepLength, 2))
            {
                MovingUnit.Position.X = DestanationPoint.X;
                MovingUnit.Position.Y = DestanationPoint.Y;
                return(NextAction?.Start());
            }
            FloatPoint2D vector = MovingVector();

            MovingUnit.Position.X += vector.X * StepLength;
            MovingUnit.Position.Y += vector.Y * StepLength;

            return(this);
        }
Exemplo n.º 4
0
 public float DistanceF(FloatPoint2D target) => MathF.Sqrt(SquareDistanceF(target));
Exemplo n.º 5
0
 public double Distance(FloatPoint2D target) => Math.Sqrt(SquareDistance(target));
Exemplo n.º 6
0
 public float SquareDistanceF(FloatPoint2D target) => (MathF.Pow(X - target.X, 2) + MathF.Pow(Y - target.Y, 2));
Exemplo n.º 7
0
 public double SquareDistance(FloatPoint2D target) => (Math.Pow(X - target.X, 2) + Math.Pow(Y - target.Y, 2));