Esempio n. 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            this.gameTime = gameTime;

            //FPS
            if (gameTime.TotalGameTime - lastFpsCounterReset > fpsCounterResetFreq)
            {
                fps                 = fpsCounter;
                fpsCounter          = 0;
                lastFpsCounterReset = gameTime.TotalGameTime;
            }

            //Player Run
            if (gameTime.TotalGameTime - lastRunUpdate > runFreq)
            {
                if (isMoving)
                {
                    if (runStepDirection)
                    {
                        if (playerRunStep < 2)
                        {
                            playerRunStep++;
                        }
                        else
                        {
                            runStepDirection = !runStepDirection;
                            playerRunStep--;
                        }
                    }
                    else
                    {
                        if (playerRunStep > 0)
                        {
                            playerRunStep--;
                        }
                        else
                        {
                            runStepDirection = !runStepDirection;
                            playerRunStep++;
                        }
                    }
                }
                else
                {
                    playerRunStep = 1;
                }
                lastRunUpdate = gameTime.TotalGameTime;
            }
            isMoving = false;
            UpdateFireballs(gameTime);
            lightSources = GetLightSources();

            #region keyboard_input
            lastKeyboardState = keyboardState;
            keyboardState     = Keyboard.GetState();
            if (firstRun)
            {
                firstRun = false;
                return;
            }

            if (!player.IsInInventory)
            {
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    PlayerMove(3, gameTime);
                    isMoving = true;
                }
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    PlayerMove(2, gameTime);
                    isMoving = true;
                }
                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    PlayerMove(1, gameTime);
                    isMoving = true;
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    PlayerMove(0, gameTime);
                    isMoving = true;
                }

                if (KeyPress(Keys.Q))
                {
                    if (player.Type == 1 && player.UseSkill(0, gameTime))
                    {
                        fireballs.Add(new Fireball()
                        {
                            X = (int)player.Position.X, Y = (int)player.Position.Y, Direction = player.RunDirection, Speed = 3, Step = 0
                        });
                    }
                    if (player.CanUseSwords && player.UseSkill(0, gameTime))
                    {
                        player.CastSword(48, 0.3F, player.RunDirection);
                    }
                }
            }

            if (keyboardState.IsKeyDown(Keys.OemTilde))
            {
                //RegenPole();
            }

            Rectangle playerRect = new Rectangle((int)player.Position.X, (int)player.Position.Y, playerTextureWidth, playerTextureHeight);
            if (KeyPress(Keys.E))
            {
                foreach (var obj in currentLocation.GetObjectsIntersectsRect(playerRect))
                {
                    obj.Interact(player);
                }
            }

            for (int i = 0; i < player.Buffs.Count; i++)
            {
                var buff = player.Buffs[i];
                if (buff.LastedTime <= TimeSpan.Zero)
                {
                    player.Buffs.Remove(buff);
                    i--;
                    continue;
                }
                buff.LastedTime -= gameTime.ElapsedGameTime;
            }

            if (KeyPress(Keys.T))
            {
                if (player.Type == 1 && player.UseSkill(1, gameTime))
                {
                    player.AddBuff(new Buff(BuffType.Torch, 10));
                }
            }

            if (player.HP < 0)
            {
                player.Kill();
            }

            if (player.SkillUsePoint < player.MaxSkillUsePoint)
            {
                player.SkillUsePoint += player.SkillUsePointPerSec * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            if (player.SkillUsePoint > player.MaxSkillUsePoint)
            {
                player.SkillUsePoint = player.MaxSkillUsePoint;
            }

            if (KeyPress(Keys.OemComma))
            {
                enableSmoothLightning = !enableSmoothLightning;
            }

            if (player.Buffs.Where((Buff f) => f.Type == BuffType.Poison).Count() > 0)
            {
                player.HP -= (float)(Buff.PoisonDmgPS * gameTime.ElapsedGameTime.TotalSeconds);
            }

            if (enableDevCheats && KeyPress(Keys.Z))
            {
                //Healing Potion
                player.HP += 50;
                if (player.HP > player.MaxHP)
                {
                    player.HP = player.MaxHP;
                }
            }

            if (enableDevCheats && KeyPress(Keys.X))
            {
                //Healing Potion
                player.SkillUsePoint += 50;
                if (player.SkillUsePoint > player.MaxSkillUsePoint)
                {
                    player.SkillUsePoint = player.MaxSkillUsePoint;
                }
            }

            if (enableDevCheats && KeyPress(Keys.C))
            {
                cheatFullBright       = !cheatFullBright;
                enableSmoothLightning = !cheatFullBright;
            }

            if (enableDevCheats && KeyPress(Keys.F3))
            {
                debugInfo = !debugInfo;
            }

            if (enableDevCheats && KeyPress(Keys.V))
            {
                player.isSpeedHack = !player.isSpeedHack;
            }

            if (enableDevCheats && KeyPress(Keys.B))
            {
                MouseState s = Mouse.GetState();
                player.Position += new Vector2(s.X, s.Y) - ScreenCenter;
            }

            if (enableDevCheats && KeyPress(Keys.N))
            {
                player.CastSword(10000000, 1F, player.RunDirection);
            }

            if (enableDevCheats && KeyPress(Keys.M))
            {
                player.UnCastSword();
            }

            if (KeyPress(Keys.Escape))
            {
                player.IsInInventory = !player.IsInInventory;
            }

            if (KeyPress(Keys.F4))
            {
                isFullScreen = !isFullScreen;
                //TODO: Fix fullscreen bug
            }

            if (!player.IsInInventory)
            {
                if (holdingItem != null)
                {
                    player.AddItem(holdingItem);
                    holdingItem = null;
                }
                if (KeyPress(Keys.D1))
                {
                    player.SelectedSlot = 0;
                }
                if (KeyPress(Keys.D2))
                {
                    player.SelectedSlot = 1;
                }
                if (KeyPress(Keys.D3))
                {
                    player.SelectedSlot = 2;
                }
                if (KeyPress(Keys.D4))
                {
                    player.SelectedSlot = 3;
                }
                if (KeyPress(Keys.D5))
                {
                    player.SelectedSlot = 4;
                }
                if (KeyPress(Keys.D6))
                {
                    player.SelectedSlot = 5;
                }
                if (KeyPress(Keys.D7))
                {
                    player.SelectedSlot = 6;
                }
                if (KeyPress(Keys.D8))
                {
                    player.SelectedSlot = 7;
                }
                if (KeyPress(Keys.D9))
                {
                    player.SelectedSlot = 8;
                }

                if (KeyPress(Keys.D0))
                {
                    player.SelectedSlot = 9;
                }

                if (KeyPress(Keys.A))
                {
                    player.SelectedSlot--;
                    if (player.SelectedSlot < 0)
                    {
                        player.SelectedSlot += 10;
                    }
                }

                if (KeyPress(Keys.D))
                {
                    player.SelectedSlot++;
                    player.SelectedSlot %= 10;
                }
            }
            else
            {
                if (KeyPress(Keys.Up))
                {
                    player.SelectedSlot -= 10;
                    if (player.SelectedSlot < 0)
                    {
                        player.SelectedSlot += 40;
                    }
                    player.SelectedSlot %= 40;
                }
                if (KeyPress(Keys.Down))
                {
                    player.SelectedSlot += 10;
                    if (player.SelectedSlot < 0)
                    {
                        player.SelectedSlot += 40;
                    }
                    player.SelectedSlot %= 40;
                }
                if (KeyPress(Keys.Left))
                {
                    player.SelectedSlot -= 1;
                    if (player.SelectedSlot < 0)
                    {
                        player.SelectedSlot += 40;
                    }
                    player.SelectedSlot %= 40;
                }
                if (KeyPress(Keys.Right))
                {
                    player.SelectedSlot += 1;
                    if (player.SelectedSlot < 0)
                    {
                        player.SelectedSlot += 40;
                    }
                    player.SelectedSlot %= 40;
                }
                if (KeyPress(Keys.W))
                {
                    ItemStack s = player.Inventory[player.SelectedSlot];
                    if (holdingItem == null || s == null || s.Type != holdingItem.Type)
                    {
                        player.Inventory[player.SelectedSlot] = holdingItem;
                        holdingItem = s;
                    }
                    else
                    {
                        int add = holdingItem.Count;
                        if (add + player.Inventory[player.SelectedSlot].Count > holdingItem.Type.GetMaxStackSize())
                        {
                            add = holdingItem.Type.GetMaxStackSize() - player.Inventory[player.SelectedSlot].Count;
                        }
                        player.Inventory[player.SelectedSlot].Count += add;
                        holdingItem.Count -= add;
                        if (holdingItem.Count <= 0)
                        {
                            holdingItem = null;
                        }
                    }
                }
                if (KeyPress(Keys.Q))
                {
                    ItemStack s = player.Inventory[player.SelectedSlot];
                    if (s != null)
                    {
                        if (holdingItem == null)
                        {
                            holdingItem = new ItemStack(s.Type, 1);
                            s.Count--;
                        }
                        else if (holdingItem.Type == s.Type && holdingItem.Count + 1 <= holdingItem.Type.GetMaxStackSize())
                        {
                            holdingItem.Count++;
                            s.Count--;
                        }
                        if (s.Count <= 0)
                        {
                            player.Inventory[player.SelectedSlot] = null;
                        }
                    }
                }
            }

            if (KeyPress(Keys.S))
            {
                ItemStack cs = player.Inventory[player.SelectedSlot];
                if (cs != null)
                {
                    cs.Type.OnUse(player, cs);
                    player.ValidateInventory();
                }
            }
            #endregion


            particleController.Update(gameTime);
            if (player.Type == 1)
            {
                particleController.PlayerWizardManaSparks(player.Position);
            }
            base.Update(gameTime);
        }