예제 #1
0
        public Player(IServiceProvider serviceProvider, SpriteBatch spriteBatch, Vector2 baseScreenSize, HelperStruct helperStruct)
        {
            _content = new ContentManager(serviceProvider, "Content");
            _spriteBatch = spriteBatch;
            _baseScreenWidth = (int)baseScreenSize.X;
            _baseScreenHeight = (int)baseScreenSize.Y;

            _helperStruct = helperStruct;

            if (_characterTexture == null)
                _characterTexture = _content.Load<Texture2D>("Sprites/Player/Player");

            _walkDown = new Animation();
            _walkDown.AddFrame(new Rectangle(0,   160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(80,  160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(160, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(240, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(320, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(400, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(480, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(560, 160, 80, 80), TimeSpan.FromSeconds(.25));
            _walkDown.AddFrame(new Rectangle(640, 160, 80, 80), TimeSpan.FromSeconds(.25));

            _walkUp = new Animation();
            _walkUp.AddFrame(new Rectangle(0, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(80, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(160, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(240, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(320, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(400, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(480, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(560, 0, 80, 80), TimeSpan.FromSeconds(.25));
            _walkUp.AddFrame(new Rectangle(640, 0, 80, 80), TimeSpan.FromSeconds(.25));

            _walkLeft = new Animation();
            _walkLeft.AddFrame(new Rectangle(0,   80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(80,  80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(160, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(240, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(320, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(400, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(480, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(560, 80, 80, 80), TimeSpan.FromSeconds(.25));
            _walkLeft.AddFrame(new Rectangle(640, 80, 80, 80), TimeSpan.FromSeconds(.25));

            _walkRight = new Animation();
            _walkRight.AddFrame(new Rectangle(0,   240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(80,  240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(160, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(240, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(320, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(400, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(480, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(560, 240, 80, 80), TimeSpan.FromSeconds(.25));
            _walkRight.AddFrame(new Rectangle(640, 240, 80, 80), TimeSpan.FromSeconds(.25));

            // Standing animations only have a single frame of animation:
            _standDown = new Animation ();
            _standDown.AddFrame(new Rectangle(0, 160, 80, 80), TimeSpan.FromSeconds(.25));

            _standUp = new Animation ();
            _standUp.AddFrame(new Rectangle(0, 0, 80, 80), TimeSpan.FromSeconds(.25));

            _standLeft = new Animation ();
            _standLeft.AddFrame(new Rectangle(0, 80, 80, 80), TimeSpan.FromSeconds(.25));

            _standRight = new Animation ();
            _standRight.AddFrame(new Rectangle(0, 240, 80, 80), TimeSpan.FromSeconds(.25));

            _currentAnimation = new Animation();
            _currentAnimation = _walkRight;
        }
예제 #2
0
        internal void WalkAlongPath(System.Collections.Generic.List<PlayerSteps> listSteps, int _numberAvaliableSteps)
        {
            //int aa = 0;
            //int bb = 0;

            _isPlayerInMove = true;
            _positionX = (int)Math.Floor((double)X);
            _positionY = (int)Math.Floor((double)Y);

            var howCloseDestinationTileX = _positionX - _positionDestinationX;
            howCloseDestinationTileX = (howCloseDestinationTileX > 0) ? howCloseDestinationTileX : howCloseDestinationTileX * -1;
            var howCloseDestinationTileY = _positionY - _positionDestinationY;
            howCloseDestinationTileY = (howCloseDestinationTileY > 0) ? howCloseDestinationTileY : howCloseDestinationTileY * -1;

            if (!_isAnimationContinue && _stepIndex <= (_numberAvaliableSteps))
            {
                _isAnimationContinue = true;
                var a = listSteps[_stepIndex];
                switch (a)
                {
                    case PlayerSteps.Right:
                        _currentAnimation = _walkRight;
                        _positionDestinationX = _positionX + Tile.Width;
                        _positionDestinationY = _positionY;
                        _playerPositionInMapX += Tile.Width;
                        //aa += Tile.Width;
                        break;
                    case PlayerSteps.Left:
                        _currentAnimation = _walkLeft;
                        _positionDestinationX = _positionX - Tile.Width;
                        _positionDestinationY = _positionY;
                        _playerPositionInMapX -= Tile.Width;
                        //aa -= Tile.Width;
                        break;
                    case PlayerSteps.Up:
                        _currentAnimation = _walkUp;
                        _positionDestinationX = _positionX;
                        _positionDestinationY = _positionY - Tile.Width;
                        _playerPositionInMapY -= Tile.Height;
                       // bb -= Tile.Height;
                        break;
                    case PlayerSteps.Down:
                        _currentAnimation = _walkDown;
                        _positionDestinationX = _positionX;
                        _positionDestinationY = _positionY + Tile.Width;
                        _playerPositionInMapY += Tile.Height;
                        //bb += Tile.Height;
                        break;
                    case PlayerSteps.CrossRightUp:
                        _currentAnimation = _walkRight;
                        _positionDestinationX = _positionX + Tile.Width;
                        _positionDestinationY = _positionY - Tile.Height;
                        _playerPositionInMapX += Tile.Width;
                        _playerPositionInMapY -= Tile.Height;
                        //aa += Tile.Width;
                       // bb -= Tile.Height;
                        break;
                    case PlayerSteps.CrossRightDown:
                        _currentAnimation = _walkRight;
                        _positionDestinationX = _positionX + Tile.Width;
                        _positionDestinationY = _positionY + Tile.Width;
                        _playerPositionInMapX += Tile.Width;
                        _playerPositionInMapY += Tile.Height;
                       // aa += Tile.Width;
                       // bb += Tile.Height;
                        break;
                    case PlayerSteps.CrossLeftUp:
                        _currentAnimation = _walkLeft;
                        _positionDestinationX = _positionX - Tile.Width;
                        _positionDestinationY = _positionY - Tile.Width;
                        _playerPositionInMapX -= Tile.Width;
                        _playerPositionInMapY -= Tile.Height;
                       // aa -= Tile.Width;
                       // bb -= Tile.Height;
                        break;
                    case PlayerSteps.CrossLeftDown:
                        _currentAnimation = _walkLeft;
                        _positionDestinationX = _positionX - Tile.Width;
                        _positionDestinationY = _positionY + Tile.Height;
                        _playerPositionInMapX -= Tile.Width;
                        _playerPositionInMapY += Tile.Height;
                        //aa -= Tile.Width;
                       // bb += Tile.Height;
                        break;
                    case PlayerSteps.None:
                        _currentAnimation = _standDown;
                        _isPlayerInMove = false;
                        _isAnimationContinue = false;
                        break;
                    default:
                        break;
                }
            }
            else
                if (howCloseDestinationTileX < 3 && howCloseDestinationTileY < 3)
                {
                    X = roundToTileSize((int)X, Tile.Width, 5);
                    Y = roundToTileSize((int)Y, Tile.Width, 5);

                    if (!_helperStruct.isPlayerStartX && !_helperStruct.isPlayerEndX)
                    {
                        X = (_helperStruct.NumberOfTilesWidth / 2) * Tile.Width;
                        _positionDestinationX = (int)X;
                        //_helperStruct.isPlayerMoveRight = true;
                    }

                    if (!_helperStruct.isPlayerStartY && !_helperStruct.isPlayerEndY)
                    {
                        Y = (_helperStruct.NumberOfTilesHeight / 2) * Tile.Height;
                        _positionDestinationY = (int)Y;
                        //_helperStruct.isPlayerMoveDown = true;
                    }

                    _playerPositionInMapPreX = _playerPositionInMapX;
                    _playerPositionInMapPreY = _playerPositionInMapY;

                    Draw();

                    _stepIndex++;
                    _isAnimationContinue = false;
                }

            if (_isPlayerInMove)
            {

                //_playerPositionInMapX += aa;
                //_playerPositionInMapY += bb;

                WalkTo(_gameTime, _positionDestinationX, _positionDestinationY);
            }
            else
            {
                _stepIndex = 0;
                listSteps.Clear();
            }
        }