示例#1
0
        public void UpdateInput()
        {
            mouseState = Mouse.GetState();
            boardState = Keyboard.GetState();

            if (placingTower != null)
            {
                placingTower.Location = new Rectangle(mouseState.X - (mouseState.X % gridSize), mouseState.Y - (mouseState.Y % gridSize), placingTower.Location.Width, placingTower.Location.Height);

                if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
                {
                    for (int i = 0; i < map.Towers.Count; i++)
                    {
                    }
                    map.PlaceTower(new Tower(placingTower.Name, placingTower.Health, placingTower.AttackDamage, placingTower.Cost, placingTower.Range, placingTower.Location));

                    placingTower = null;
                }
            }
            else
            {
                if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
                {
                    foreach (Tower tower in menu.Towers)
                    {
                        if (tower.Location.Contains(mouseState.X, mouseState.Y))
                        {
                            placingTower = tower.Clone();
                        }
                    }
                }
            }

            if (mouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released)
            {
                for (int i = 0; i < map.Towers.Count; i++)
                {
                    if (map.Towers[i].Location.Contains(mouseState.X, mouseState.Y))
                    {
                        map.SellTower(map.Towers[i]);
                    }
                }
            }

            #region Selecting towers with Keyboard Input

            if (boardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
            {
                Tower temp = menu.Towers[0];
                placingTower = new Tower(temp.Name, temp.Health, temp.AttackDamage, temp.Cost, temp.Range, new Rectangle(mouseState.X - (mouseState.X % gridSize), mouseState.Y - (mouseState.Y % gridSize), 50, 50));
            }

            if (boardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
            {
                Tower temp = menu.Towers[1];
                placingTower = new Tower(temp.Name, temp.Health, temp.AttackDamage, temp.Cost, temp.Range, new Rectangle(mouseState.X - (mouseState.X % gridSize), mouseState.Y - (mouseState.Y % gridSize), 50, 50));
            }

            #endregion

            if (boardState.IsKeyUp(Keys.LeftAlt))
            {
                rewindingTime = false;
                map.Rewinding = false;
                map.SaveNextState();
            }
            if (boardState.IsKeyDown(Keys.LeftAlt))
            {
                map.Rewinding = true;
                rewindingTime = true;
                map.LoadPreviousState();
            }


            #region Menu interaction

            if (boardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
            {
                main.Visible = !main.Visible;
            }

            if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
            {
                foreach (Menu item in menus)
                {
                    if (item.Visible)
                    {
                        item.RegisterClick(new Point(mouseState.X, mouseState.Y));
                    }
                }
            }


            #endregion



            previousMouseState    = mouseState;
            previousKeyboardState = boardState;
        }