Exemplo n.º 1
0
        public void Update(GameTime gameTime)
        {
            KeyboardState kState = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (healthTimer > 0)
            {
                healthTimer -= dt;
            }

            anim = animations[(int)direction];

            if (isMoving)
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(1);
            }

            isMoving = false;

            if (kState.IsKeyDown(Keys.Right))
            {
                direction = Dir.Right;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Left))
            {
                direction = Dir.Left;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Up))
            {
                direction = Dir.Up;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Down))
            {
                direction = Dir.Down;
                isMoving  = true;
            }

            if (isMoving)
            {
                Vector2 tempPos = position;

                switch (direction)
                {
                case Dir.Right:
                    tempPos.X += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.X += speed * dt;
                    }
                    break;

                case Dir.Left:
                    tempPos.X -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.X -= speed * dt;
                    }
                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.Y -= speed * dt;
                    }
                    break;

                case Dir.Down:
                    tempPos.Y += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.Y += speed * dt;
                    }
                    break;

                default:
                    break;
                }
            }
            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                Projectile.projectiles.Add(new Projectile(Position, direction));
            }
            kStateOld = kState;
        }