Beispiel #1
0
        public override void Update(FrameTick elapsedTime)
        {
            Loc movediff = Destination - CurPos;
            int movelen  = movediff.Length();

            //Get the difference between the current position and destination
            double x = movediff.X / Math.Sqrt(movediff.DistSquared());
            double y = movediff.Y / Math.Sqrt(movediff.DistSquared());

            //Ignore translation on a given axis if its already at the correct position on that axis.
            // Mainly because we're rounding the values to the nearest integer, and might overshoot otherwise.
            if (movediff.X == 0)
            {
                x = 0;
            }
            if (movediff.Y == 0)
            {
                y = 0;
            }

            //Convert the floating point number to the biggest nearby integer value, and keep its sign
            Loc movevec = new Loc();

            movevec.X = (int)(Math.Ceiling(Math.Abs(x)) * Math.Sign(x));
            movevec.Y = (int)(Math.Ceiling(Math.Abs(y)) * Math.Sign(y));


            Loc checkedmove = new Loc();

            //Constrain the move vector components to the components of the position difference vector to
            // ensure we don't overshoot because of the move rate
            checkedmove.X = Math.Min(Math.Abs(movediff.X), Math.Abs(movevec.X * moveRate)) * Math.Sign(movevec.X);
            checkedmove.Y = Math.Min(Math.Abs(movediff.Y), Math.Abs(movevec.Y * moveRate)) * Math.Sign(movevec.Y);

            //Update facing direction. Ignore none, since it crashes the game.
            Dir8 newdir = movevec.ApproximateDir8();

            if (newdir != Dir8.None)
            {
                CharDir = newdir;
            }

            Move    = checkedmove;
            CurPos += Move; //Increment our internal current position, since we have no ways of knowing where we are otherwise..
        }