Time for action - supporting map transitions

  1. Add a new helper method to the Helper Methods region of the Player class:
    private void checkLevelTransition()
    {
    Vector2 centerCell = TileMap.GetCellByPixel(WorldCenter);
    if (TileMap.CellCodeValue(centerCell).StartsWith("T_"))
    {
    string[] code = TileMap.CellCodeValue(centerCell). Split('_'),
    if (code.Length != 4)
    return;
    LevelManager.LoadLevel(int.Parse(code[1]));
    WorldLocation = new Vector2(
    int.Parse(code[2]) * TileMap.TileWidth,
    int.Parse(code[3]) * TileMap.TileHeight);
    LevelManager.RespawnLocation = WorldLocation;
    velocity = Vector2.Zero;
    }
    }
    
  2. Modify the Update() method of the Player class to add a check for pressing the Up key or pressing Up on the gamepad to check the current square for an available transition. Place this after the check for pressing the Space bar or A button to jump:
    if (keyState.IsKeyDown(Keys.Up) ||
    gamePad.ThumbSticks.Left.Y > 0.3f)
    {
    checkLevelTransition();
    }
    
  3. Execute the Gemstone Hunter application and move right on the map until you reach the cave door. Stand on the door and press up on the keyboard or gamepad to transition to the next map. Try not to get killed by the zombies on the way! Once you reach the door and walk through it, the second map (MAP001.MAP), containing an underground level, will be loaded:
Time for action - supporting map transitions

What just happened?

The checkLevelTransition() method examines the code value in the cell containing the center of the player's sprite, looking for an entry starting with T_ that indicates a transition square. If found, the text is split into pieces separated by an underscore using the Split() method. If the result is not four pieces (T_, the map number, an X coordinate, and a Y Coordinate), the method simply returns and does nothing.

If the code is in the proper format, the LevelManager is asked to load the desired map. After the map has been loaded (which may contain START codes that position the player) the player is moved to the position indicated by the transition code value.

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

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