コード例 #1
0
        public void ManageMovement(IMovable movable)
        {
            movable.PreviousMoveDirection = movable.MoveDirection;

            #region new move-to command
            if (movable.CanGetMoveOrder && _mouseRightButtonClicked)
            {
                var rightClickLocation = GetMouseCellLocation();

                var path = PathFinder.FindPath( movable.CellLocation, rightClickLocation, _gameWorldRef.Map );
                movable.MovingPath = path;

                if (path != null && path.Count > 1)
                {
                    movable.CurrentPathSegment = 1;

                    // initially move to first segment..
                    SetMovableToMoveToCell(movable, movable.MovingPath[movable.CurrentPathSegment]); // or [1]
                }

            }
            #endregion

            #region update already moving objects in this frame
            if (movable.IsMoving)
            {

                if (MovableReachedCurrentDistination(movable))
                {
                    if (++movable.CurrentPathSegment == movable.MovingPath.Count)
                    {
                        // here movable reached its final distination
                        movable.Stop();
                        Console.WriteLine("distination location reached");

                    }
                    else
                    {
                        SetMovableToMoveToCell(movable, movable.MovingPath[movable.CurrentPathSegment]);
                    }
                }

                if (movable.MoveDirectionChanged())
                {
                    movable.UpdateMovingAnimation();
                }

                movable.MoveBy(movable.MoveDirection.X * movable.Speed, movable.MoveDirection.Y * movable.Speed);

                //commented
                #region Clamping
                //float visualX = MathHelper.Clamp(
                //    movable.Position.X, 100, Camera.WorldWidth - 100);
                //float visualY = MathHelper.Clamp(
                //    movable.Position.Y, 100 , Camera.WorldHeight - 100);

                //movable.Position = new Vector2(visualX, visualY);
                #endregion

                //commented
                #region Camera Follows the Character
                //// Camera follows the character if he comes closer to any of the edges of the screen by 100 pixels.
                //Vector2 testPosition = Camera.WorldToScreen(movable.Position);

                //if (testPosition.X < 100)
                //{
                //    Camera.Move(new Vector2(testPosition.X - 100, 0));
                //}

                //if (testPosition.X > (Camera.ViewWidth - 100))
                //{
                //    Camera.Move(new Vector2(testPosition.X - (Camera.ViewWidth - 100), 0));
                //}

                //if (testPosition.Y < 100)
                //{
                //    Camera.Move(new Vector2(0, testPosition.Y - 100));
                //}

                //if (testPosition.Y > (Camera.ViewHeight - 100))
                //{
                //    Camera.Move(new Vector2(0, testPosition.Y - (Camera.ViewHeight - 100)));
                //}
                #endregion
            }
            #endregion
        }