Example #1
0
 public void Update(MouseState MS, MouseState MSOld, KeyboardState KS, KeyboardState KSOld)
 {
     PlayerVeloctiy *= .75f;
     UpdateMovement(KS, KSOld);
     UpdateGravity(KS, KSOld);
     Rect.X    = (int)Pos.X;
     Rect.Y    = (int)Pos.Y;
     RectDet.X = (int)Pos.X;
     RectDet.Y = (int)Pos.Y;
     Pos      += PlayerVeloctiy;
     if (InvWindow.Shown == false)
     {
         InvBar.Update(MS, MSOld, KS, KSOld);
     }
     if (InvWindow.Shown == true)
     {
         InvWindow.Update(MS, MSOld, KS, KSOld);
     }
 }
Example #2
0
        protected override void Update(GameTime gameTime)
        {
            pastKeyboardState    = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            pastMouseState    = currentMouseState;
            currentMouseState = Mouse.GetState();

            HandleKeyInput();

            // Update player
            Player.Update(gameTime);

            // Update Map
            Map.Update(gameTime);

            // Update inventory
            inventoryBar.Update(gameTime);
            inventory.Update(gameTime);

            // Player collisions
            int collisionUpdateDistance = 50;
            int calls = 0;

            for (int x = (int)Player.TilePosition.X - collisionUpdateDistance; x < (int)Player.TilePosition.X + collisionUpdateDistance; x++)
            {
                for (int y = (int)Player.TilePosition.Y - collisionUpdateDistance; y < (int)Player.TilePosition.Y + collisionUpdateDistance; y++)
                {
                    if (x >= 0 && x < Map.MaxWidth && y >= 0 && y < Map.MaxHeight)
                    {
                        if (Map.Tiles[x, y].Type != TileType.Air)
                        {
                            Player.Collision(Map.Tiles[x, y].Rectangle, Map.MaxWidth, Map.MaxHeight);
                        }
                        calls++;

                        camera.Update(Player.Position, Map.MaxWidth, Map.MaxHeight);
                    }
                }
            }

            // Handle clicks
            if (currentMouseState.LeftButton == ButtonState.Pressed)
            {
                // Checks to make sure clicks are inside screen
                if (currentMouseState.X >= 0 && currentMouseState.X < camera.Bounds.Width && currentMouseState.Y > 0 && currentMouseState.Y < camera.Bounds.Height)
                {
                    float xClick = currentMouseState.ToAbsolute(camera).X;
                    float yClick = currentMouseState.ToAbsolute(camera).Y;

                    Vector2 tileClick = currentMouseState.ToAbsolute(camera).ToTile();

                    Tile tile = Map.Tiles[(int)xClick / Tile.Size, (int)yClick / Tile.Size];

                    if (tile.Type != TileType.Air)
                    {
                        Map.Drops.Add(new Drop(
                                          (int)tileClick.X,
                                          (int)tileClick.Y,
                                          Map.Tiles[(int)xClick / Tile.Size, (int)yClick / Tile.Size].Type));

                        Air air = new Air((int)xClick / Tile.Size, (int)yClick / Tile.Size, Map);
                        air.Load(Content);
                        Map.Tiles[(int)xClick / Tile.Size, (int)yClick / Tile.Size] = air;
                    }
                }
            }

            Debug.WriteLineIf(gameTime.IsRunningSlowly, "Update is running slowly: " + calls + " tiles updated");
            base.Update(gameTime);
        }