Пример #1
0
        Vector2 speed; // Speed of player

        #endregion Fields

        #region Constructors

        public Player()
        {
            playerTexture = Ressources.player;
            playerHitboxTexture = Ressources.playerHitbox;
            playerHitbox = Collision.CollisionMapSprite(playerHitboxTexture);

            position.X = Settings.Current.StartPositionX;
            position.Y = Settings.Current.StartPositionY;
            playerSize.X = Settings.Current.SpriteWidth;
            playerSize.Y = Settings.Current.SpriteHeight;
            maxSpeed = Settings.Current.MaxSpeed;
            speed.X = 0;
            speed.Y = 0;
            maxAcceleration = Settings.Current.Acceleration;
            acceleration.X = 0;
            acceleration.Y = 0;
            drawingRectangle = new Rectangle((int)position.X, (int)position.Y, playerSize.X, playerSize.Y);

            frameLine = 0;
            frameRow = 0;
            direction = Direction.Down;
            animationTimer = 0;
            animationSpeed = Settings.Current.AnimationSpeed;
        }
Пример #2
0
        int animationSpeed;                             // Animation speed

        #endregion


        public Player()
        {
            playerTexture       = Ressources.player;
            playerHitboxTexture = Ressources.playerHitbox;
            playerHitbox        = Collision.CollisionMapSprite(playerHitboxTexture);

            position.X       = Settings.Current.StartPositionX;
            position.Y       = Settings.Current.StartPositionY;
            playerSize.X     = Settings.Current.SpriteWidth;
            playerSize.Y     = Settings.Current.SpriteHeight;
            maxSpeed         = Settings.Current.MaxSpeed;
            speed.X          = 0;
            speed.Y          = 0;
            maxAcceleration  = Settings.Current.Acceleration;
            acceleration.X   = 0;
            acceleration.Y   = 0;
            drawingRectangle = new Rectangle((int)position.X, (int)position.Y, playerSize.X, playerSize.Y);

            frameLine      = 0;
            frameRow       = 0;
            direction      = Direction.Down;
            animationTimer = 0;
            animationSpeed = Settings.Current.AnimationSpeed;
        }
Пример #3
0
        public static Vector2 MoveSpriteOnMap(Axis axis, Vector2 previousPosition, Vector2 position, Point spriteSize, Hitbox spriteHitbox)
        {
            Vector2 point = new Vector2();
            float   speed;
            float   X = position.X;
            float   Y = position.Y;
            int     point1Index, point2Index;

            switch (axis)
            {
            case Axis.YAxis:     //Up-Down
            {
                point.X = position.X;
                speed   = position.Y - previousPosition.Y;

                if (previousPosition.Y > position.Y)         //Up
                {
                    if (speed >= 1 || speed <= 1)
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            Y = previousPosition.Y;
                            for (int i = 0; i < Math.Abs(speed); i++)
                            {
                                Y--;
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    Y++;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Y = position.Y;
                        }
                        point.Y = Y;
                    }
                    else
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            point.Y = previousPosition.Y;
                        }
                        else
                        {
                            point.Y = Y;
                        }
                    }
                }
                else         //Down
                {
                    if (speed >= 1 || speed <= 1)
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            Y = previousPosition.Y;
                            for (int i = 0; i < Math.Abs(speed); i++)
                            {
                                Y++;
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    Y--;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Y = position.Y;
                        }
                        point.Y = Y;
                    }
                    else
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            point.Y = previousPosition.Y;
                        }
                        else
                        {
                            point.Y = Y;
                        }
                    }
                }
                break;
            }

            case Axis.XAxis:     //Left-Right
            {
                point.Y = position.Y;
                speed   = position.X - previousPosition.X;

                if (previousPosition.X > position.X)         //Left
                {
                    if (speed >= 1 || speed <= 1)
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            X = previousPosition.X;
                            for (int i = 0; i < Math.Abs(speed); i++)
                            {
                                X--;
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    X++;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            X = position.X;
                        }
                        point.X = X;
                    }
                    else
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            point.X = previousPosition.X;
                        }
                        else
                        {
                            point.X = X;
                        }
                    }
                }
                else         //Right
                {
                    if (speed >= 1 || speed <= 1)
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            X = previousPosition.X;
                            for (int i = 0; i < Math.Abs(speed); i++)
                            {
                                X++;
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    X--;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            X = position.X;
                        }
                        point.X = X;
                    }
                    else
                    {
                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                        {
                            point.X = previousPosition.X;
                        }
                        else
                        {
                            point.X = X;
                        }
                    }
                }
            }
            break;
            }
            return(point);
        }
Пример #4
0
        public static Vector2 MoveSpriteOnMap(Axis axis, Vector2 previousPosition, Vector2 position, Point spriteSize, Hitbox spriteHitbox)
        {
            Vector2 point = new Vector2();
            float speed;
            float X = position.X;
            float Y = position.Y;
            int point1Index, point2Index;

            switch (axis)
            {
                case Axis.YAxis: //Up-Down
                    {
                        point.X = position.X;
                        speed = position.Y - previousPosition.Y;

                        if (previousPosition.Y > position.Y) //Up
                        {
                            if (speed >= 1 || speed <= 1)
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    Y = previousPosition.Y;
                                    for (int i = 0; i < Math.Abs(speed); i++)
                                    {
                                        Y--;
                                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                        {
                                            Y++;
                                            break;
                                        }
                                    }
                                }
                                else
                                    Y = position.Y;
                                point.Y = Y;
                            }
                            else
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                    point.Y = previousPosition.Y;
                                else
                                    point.Y = Y;
                            }
                        }
                        else //Down
                        {
                            if (speed >= 1 || speed <= 1)
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    Y = previousPosition.Y;
                                    for (int i = 0; i < Math.Abs(speed); i++)
                                    {
                                        Y++;
                                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                        {
                                            Y--;
                                            break;
                                        }
                                    }
                                }
                                else
                                    Y = position.Y;
                                point.Y = Y;
                            }
                            else
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                    point.Y = previousPosition.Y;
                                else
                                    point.Y = Y;
                            }
                        }
                        break;
                    }

                case Axis.XAxis: //Left-Right
                    {
                        point.Y = position.Y;
                        speed = position.X - previousPosition.X;

                        if (previousPosition.X > position.X) //Left
                        {
                            if (speed >= 1 || speed <= 1)
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    X = previousPosition.X;
                                    for (int i = 0; i < Math.Abs(speed); i++)
                                    {
                                        X--;
                                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                        {
                                            X++;
                                            break;
                                        }
                                    }
                                }
                                else
                                    X = position.X;
                                point.X = X;
                            }
                            else
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x1, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                    point.X = previousPosition.X;
                                else
                                    point.X = X;
                            }
                        }
                        else //Right
                        {
                            if (speed >= 1 || speed <= 1)
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                {
                                    X = previousPosition.X;
                                    for (int i = 0; i < Math.Abs(speed); i++)
                                    {
                                        X++;
                                        point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                        point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                        if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                        {
                                            X--;
                                            break;
                                        }
                                    }
                                }
                                else
                                    X = position.X;
                                point.X = X;
                            }
                            else
                            {
                                point1Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y1);
                                point2Index = Position2MapIndex(X, Y, spriteHitbox.x2, spriteHitbox.y2);
                                if (TestPoints(point1Index, point2Index) || TestScreen(spriteSize, X, Y))
                                    point.X = previousPosition.X;
                                else
                                    point.X = X;
                            }
                        }

                    }
                    break;
            }
            return point;
        }