Time for action - the Game1 Update method

  1. Replace the current Update() method in the Game1 class with the following:
    protected override void Update(GameTime gameTime)
    {
    Camera.Position = new Vector2(hscroll.Value, vscroll.Value);
    MouseState ms = Mouse.GetState();
    if ((ms.X > 0) && (ms.Y > 0) &&
    (ms.X < Camera.ViewPortWidth) &&
    (ms.Y < Camera.ViewPortHeight))
    {
    Vector2 mouseLoc = Camera.ScreenToWorld(
    new Vector2(ms.X, ms.Y));
    if (Camera.WorldRectangle.Contains(
    (int)mouseLoc.X, (int)mouseLoc.Y))
    {
    if (ms.LeftButton == ButtonState.Pressed)
    {
    TileMap.SetTileAtCell(
    TileMap.GetCellByPixelX((int)mouseLoc.X),
    TileMap.GetCellByPixelY((int)mouseLoc.Y),
    DrawLayer,
    DrawTile);
    }
    if ((ms.RightButton == ButtonState.Pressed) &&
    (lastMouseState.RightButton ==
    ButtonState.Released))
    {
    if (EditingCode)
    {
    TileMap.GetMapSquareAtCell(
    TileMap.GetCellByPixelX((int)mouseLoc.X),
    TileMap.GetCellByPixelY((int)mouseLoc.Y)
    ).CodeValue = CurrentCodeValue;
    }
    else
    {
    TileMap.GetMapSquareAtCell(
    TileMap.GetCellByPixelX((int)mouseLoc.X),
    TileMap.GetCellByPixelY((int)mouseLoc.Y)
    ).TogglePassable();
    }
    }
    HoverCodeValue =
    TileMap.GetMapSquareAtCell(
    TileMap.GetCellByPixelX(
    (int)mouseLoc.X),
    TileMap.GetCellByPixelY(
    (int)mouseLoc.Y)).CodeValue;
    }
    }
    lastMouseState = ms;
    base.Update(gameTime);
    }
    
  2. Execute the application. Attempting to draw tiles at this point results in black holes being punched in the all-blue map:
Time for action - the Game1 Update method

What just happened?

The first thing our Update() method does is to set the game's camera position based on the current values of the horizontal and vertical scroll bars on the level editor form. When we scroll the scroll bars, the game map will scroll as well.

Next, we verify that the mouse coordinates are within the view port of the camera, and thus within the PictureBox control on the editor form. If they are, we determine the world-based coordinates of the mouse cursor.

If the left mouse button has been pressed, we update the tile under the cursor on the current DrawLayer with the current DrawTile. The right mouse button is a bit more complicated.

We will use the right mouse button to set two different types of information either toggling on and off the Passable property of the MapSquare, or setting its CodeValue property, depending on the mode selected on the MapEditor form.

In either case, we will only make a change to the underlying tile if the right mouse button is pressed during this frame and was not pressed during the previous frame. This eliminates updating the same square multiple times on a single button press (which would make toggling passability on and off difficult!) During the first frame after the button is pressed, the MapSquare will be updated. Until the button is released, no other updates will occur.

The HoverCodeValue member is set to the CodeValue of the MapSquare under the mouse cursor. This value will be used by the MapEditor form and displayed on a label on the screen to provide an alternative method of viewing the code for an individual square.

Finally, lastMouseState is updated to the current mouse state and the Update() method is completed.

Connecting the form to the game

As we saw at the end of the last section, attempting to draw tiles to the map at this point only leaves empty spaces. This is because the DrawLayer and DrawTile integers both default to zero, so we are drawing a fully-transparent tile onto the background layer.

Now that we have made the necessary updates to the XNA side of the level editor, we need to flesh out the event handlers for the controls we placed on the MapEditor form to update the control variables inside the Game1 class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.174.168