MyPolarBear.GameObjects.PolarBear.Update C# (CSharp) Method

Update() public method

public Update ( GameTime gameTime ) : void
gameTime Microsoft.Xna.Framework.GameTime
return void
        public override void Update(GameTime gameTime)
        {
            if (!IsAlive)
                return;

            bMoving = false;

            if (bInvincible)
            {
                mInvincibleDeltaTime += gameTime.ElapsedGameTime.Milliseconds;

                if (mInvincibleDeltaTime >= mInvincibleDelay)
                {
                    mInvincibleDeltaTime = 0;
                    bInvincible = false;
                }
            }

            if (InputManager.Keyboard.IsKeyPressed(Keys.A) || InputManager.GamePad.IsButtonPressed(Buttons.LeftThumbstickLeft))
            {
                Velocity = new Vector2(-3, 0);
                bMoving = true;

                switch (power)
                {
                    case Power.Normal:
                        mAnimator.PlayAnimation("NormalWalkLeft", false); break;
                    case Power.Ice:
                        mAnimator.PlayAnimation("IceWalkLeft", false); break;
                    case Power.Fire:
                        mAnimator.PlayAnimation("FireWalkLeft", false); break;
                    case Power.Lighting:
                        break;
                }
            }
            if (InputManager.Keyboard.IsKeyPressed(Keys.D) || InputManager.GamePad.IsButtonPressed(Buttons.LeftThumbstickRight))
            {
                Velocity = new Vector2(3, 0);
                bMoving = true;

                switch (power)
                {
                    case Power.Normal:
                        mAnimator.PlayAnimation("NormalWalkRight", false); break;
                    case Power.Ice:
                        mAnimator.PlayAnimation("IceWalkRight", false); break;
                    case Power.Fire:
                        mAnimator.PlayAnimation("FireWalkRight", false); break;
                    case Power.Lighting:
                        break;
                }
            }
            if (InputManager.Keyboard.IsKeyPressed(Keys.W) || InputManager.GamePad.IsButtonPressed(Buttons.LeftThumbstickUp))
            {
                Velocity = new Vector2(0, -3);
                bMoving = true;

                switch (power)
                {
                    case Power.Normal:
                        mAnimator.PlayAnimation("NormalWalkBack", false); break;
                    case Power.Ice:
                        mAnimator.PlayAnimation("IceWalkBack", false); break;
                    case Power.Fire:
                        mAnimator.PlayAnimation("FireWalkBack", false); break;
                    case Power.Lighting:
                        break;
                }
            }
            if (InputManager.Keyboard.IsKeyPressed(Keys.S) || InputManager.GamePad.IsButtonPressed(Buttons.LeftThumbstickDown))
            {
                Velocity = new Vector2(0, 3);
                bMoving = true;

                switch (power)
                {
                    case Power.Normal:
                        mAnimator.PlayAnimation("NormalWalkFront", false); break;
                    case Power.Ice:
                        mAnimator.PlayAnimation("IceWalkFront", false); break;
                    case Power.Fire:
                        mAnimator.PlayAnimation("FireWalkFront", false); break;
                    case Power.Lighting:
                        break;
                }
            }

            if (!bMoving)
            {
                Velocity = new Vector2(0, 0);
            }

            if (InputManager.GamePad.IsButtonReleased(Buttons.DPadUp))
                power = Power.Normal;
            if (InputManager.GamePad.IsButtonReleased(Buttons.DPadLeft))
                power = Power.Ice;
            if (InputManager.GamePad.IsButtonReleased(Buttons.DPadRight))
                power = Power.Fire;

            if (InputManager.Keyboard.IsKeyReleased(Keys.Space))
                SwitchPowers();

            if (InputManager.GamePad.IsButtonReleased(Buttons.RightShoulder))
                SwitchPowers();

            if (InputManager.Mouse.IsButtonReleased(MouseComponent.MouseButton.Left))
            {
                Vector2 projectileDir = InputManager.Mouse.GetCurrentMousePosition() - ScreenManager.camera.ScreenCenter;
                projectileDir += ScreenManager.camera.Position;
                projectileDir -= Position;
                //Projectile projectile = ShootProjectile(InputManager.Mouse.GetCurrentMousePosition() - ScreenManager.camera.ScreenCenter);
                Projectile projectile = ShootProjectile(projectileDir);
                projectile.LoadContent();
                projectile.IsAlive = true;
                UpdateKeeper.getInstance().addEntity(projectile);
                DrawKeeper.getInstance().addEntity(projectile);
                SoundManager.PlaySound("Shoot");
            }

            if (InputManager.GamePad.GetThumbStickState(GamePadComponent.Thumbstick.Right).Length() >= .5)
            {
                Projectile projectile = ShootProjectile(InputManager.GamePad.GetThumbStickState(GamePadComponent.Thumbstick.Right));
                if (gameTime.TotalGameTime.TotalMilliseconds - timeProjectileFired >= 500)
                {
                    InputManager.GamePad.StartVibration();
                    projectile.LoadContent();
                    UpdateKeeper.getInstance().addEntity(projectile);
                    DrawKeeper.getInstance().addEntity(projectile);
                    projectile.IsAlive = true;
                    timeProjectileFired = (int)gameTime.TotalGameTime.TotalMilliseconds;
                    SoundManager.PlaySound("Shoot");
                }
            }
            else
            {
                InputManager.GamePad.StopVibration();
            }

            if (InputManager.Keyboard.IsKeyReleased(Keys.C))
            {
                //bGivingCommands = !bGivingCommands;

                //if (bGivingCommands)
                //{
                    foreach (Entity ene in UpdateKeeper.getInstance().getEntities())
                    {
                        if (ene is Enemy && ((Enemy)ene).CurrentState == Enemy.State.Following)
                        {
                            //((Enemy)ene).ListenForCommands();
                            ((Enemy)ene).Command();
                        }
                    }
                //}
                //else
                //{
                //    foreach (Entity ene in UpdateKeeper.getInstance().getEntities())
                //    {
                //        if (ene is Enemy && ((Enemy)ene).CurrentState == Enemy.State.Following)
                //        {
                //            //((Enemy)ene).StartCommands();
                //            //SoundManager.PlaySound("OK");
                //            ((Enemy)ene).Command();
                //        }
                //    }
                //}
            }

            // collide with level elements
            Rectangle travelRect = new Rectangle(CollisionBox.X + (int)Velocity.X, CollisionBox.Y + (int)Velocity.Y, CollisionBox.Width, CollisionBox.Height);

            foreach (LevelElement element in UpdateKeeper.getInstance().getLevelElements())
            {
                if (travelRect.Intersects(element.CollisionRect))
                {
                    if (!(element.Type.Equals("Grass") || element.Type.Equals("GrassBig") || element.Type.Equals("Ice")
                    || element.Type.Equals("SoftGround") || element.Type.Equals("BabyPlant")))
                    {
                        Velocity = Vector2.Zero;
                    }

                    if (element.Type.Equals("Bush"))
                    {
                        if (InputManager.Keyboard.IsKeyReleased(Keys.T) || InputManager.GamePad.IsButtonReleased(Buttons.A))
                        {
                            NumSeeds++;
                            if (NumSeeds <= 10)
                                SoundManager.PlaySound("PickSeed");

                            // teach follower
                            foreach (Entity ene in UpdateKeeper.getInstance().getEntities())
                            {
                                if (ene is Enemy && ((Enemy)ene).CurrentState == Enemy.State.Following)
                                {
                                    //((Enemy)ene).bHasSeenSeedGather = true;
                                    //if (bGivingCommands)
                                    //{
                                        ((Enemy)ene).AddCommand(new GetSeedAI(ene, ((Enemy)ene).Pouch));
                                    //}
                                }
                            }
                        }
                    }

                    if (element.Type.Equals("SoftGround") && NumSeeds > 0)
                    {
                        if (InputManager.Keyboard.IsKeyReleased(Keys.T) || InputManager.GamePad.IsButtonReleased(Buttons.A))
                        {
                            element.Type = "BabyPlant";
                            element.Tex = ContentManager.GetTexture("BabyPlant");
                            NumSeeds--;
                            AGrid.GetInstance().addResource(element);
                            GameScreen.CurWorldHealth++;
                            SoundManager.PlaySound("PlantSeed");

                            // teach follower
                            foreach (Entity ene in UpdateKeeper.getInstance().getEntities())
                            {
                                if (ene is Enemy && ((Enemy)ene).CurrentState == Enemy.State.Following)// && ((Enemy)ene).bHasSeenSeedGather)
                                {
                                    //((Enemy)ene).bHasSeenPlanting = true;
                                    //((Enemy)ene).CurrentState = Enemy.State.Planting;

                                    //if (bGivingCommands)
                                    //{
                                        ((Enemy)ene).AddCommand(new PlantSeedAI(ene, ((Enemy)ene).Pouch));
                                    //}
                                    //((Enemy)ene).CurrentState = Enemy.State.DoingCommands;

                                    //SoundManager.PlaySound("OK");
                                }
                            }
                        }
                    }
                }
                if (GameScreen.CurWorldHealth == GameScreen.MaxWorldHealth && element.Type.Equals("Boulder"))
                {
                    UpdateKeeper.getInstance().removeLevelElement(element);
                    DrawKeeper.getInstance().removeLevelElement(element);
                }
                if (CurHitPoints == 0)
                {
                    IsAlive = false;
                    InputManager.GamePad.StopVibration();
                }
            }

            // go to position test
            if (InputManager.Keyboard.IsKeyPressed(Keys.X) && path == null)
            {
                //path = AGrid.GetInstance().getPath(Position, new Vector2(500, 500));
                path = AGrid.GetInstance().getPath(Position, ANode.SEED_SOURCE);
                bPathfinding = true;
                pathPos = 0;
            }
            if (bPathfinding)
            {
                if (path != null && path.Count > 0)
                {
                    if ((int)Position.X < (int)path[pathPos].X + 10 && (int)Position.X > (int)path[pathPos].X - 10
                        && (int)Position.Y < (int)path[pathPos].Y + 10 && (int)Position.Y > (int)path[pathPos].Y - 10)
                    {
                        //path.RemoveAt(0);
                        pathPos++;
                    }

                    if (path.Count > pathPos)
                    {
                        Vector2 dir = path[pathPos] - Position;
                        dir.Normalize();
                        Velocity = dir * 10;
                    }
                    else
                    {
                        path = null;
                        pathPos = 0;
                        bPathfinding = false;
                    }
                }
                else
                {
                    bPathfinding = false;
                }
            }

            NumSeeds = (int)MathHelper.Clamp((float)NumSeeds, 0, 10);
            CurHitPoints = (int)MathHelper.Clamp((float)CurHitPoints, 0, 5);
            GameScreen.CurWorldHealth = (int)MathHelper.Clamp((float)GameScreen.CurWorldHealth, 0, 100);

            Position += Velocity;

            float horizontalBoundary = MathHelper.Clamp(Position.X, -GameScreen.WORLDWIDTH / 2, GameScreen.WORLDWIDTH / 2);
            float verticalBoundary = MathHelper.Clamp(Position.Y, -GameScreen.WORLDHEIGHT / 2, GameScreen.WORLDHEIGHT / 2);

            Position = new Vector2(horizontalBoundary, verticalBoundary);

            base.Update(gameTime);
        }