Exemple #1
0
        public Vector2 Move(float mx, float my)
        {
            Vector2 result    = new Vector2(posX, posY);
            string  oldSprite = curSprite;

            if (my > 0)
            {
                curSprite       = "down";
                facingDirection = Direction.Down;
            }
            else if (my < 0)
            {
                curSprite       = "up";
                facingDirection = Direction.Up;
            }
            else if (mx < 0)
            {
                curSprite       = "left";
                facingDirection = Direction.Left;
            }
            else if (mx > 0)
            {
                curSprite       = "right";
                facingDirection = Direction.Right;
            }
            else if (!curSprite.Contains("idle"))
            {
                curSprite += "_idle";
            }

            if (oldSprite != curSprite)
            {
                sprite = Game1.CreateAnimatedSprite(spriteFactory, curSprite); //TODO should we just store this?
            }
            velocityX += mx;
            velocityY += my;

            if (location != null)
            {
                var obj = location.GetCollidingObject(GetCollisionBox((int)velocityX, (int)velocityY), this);
                if (obj != null)
                {
                    obj.Collide(this);
                }
            }

            if ((location != null) && (location.IsColliding(GetCollisionBox((int)velocityX, 0), this)))
            {
                velocityX /= 16;
            }
            else if (Math.Abs(velocityX) < 1)
            {
                velocityX = 0;
            }
            else
            {
                posX       = posX + (int)velocityX;
                velocityX %= 1;
            }

            if ((location != null) && (location.IsColliding(GetCollisionBox(0, (int)velocityY), this)))
            {
                velocityY /= 16;
            }
            else if (Math.Abs(velocityY) < 1)
            {
                velocityY = 0;
            }
            else
            {
                posY       = posY + (int)velocityY;
                velocityY %= 1;
            }
            result.X -= posX;
            result.Y -= posY;
            return(result);
        }