Game settings

You have already created the simple implementation of game settings that allow to store the player name and key. In this part, you will finish the settings mechanism, as well as the Settings screen, where the user can load settings while navigating to the page and save them automatically while navigating away from it. The final version of the Settings screen is shown in the following figure (on the left). When the player enters incorrect username, a suitable information should be presented, as in the following screenshot (on the right):

Game settings

Implementation

The main logic regarding settings is placed in the Settings class, inside the model part. However, to finish implementation, you will also modify the view model.

Settings.cs

Currently, the static Settings class contains the code that makes it possible to retrieve and save the player name and key. Here, you will add support for the music volume (float), the vibration state (bool), as well as the consent to send the player location data (bool). All modifications required by adding the Volume setting are presented in the following code:

public static class Settings
{ (...)
  private const string KEY_VOLUME = "Volume"; (...)
  private const float DEFAULT_VOLUME = 50.0f; (...)
  public static float Volume
  {
    get { return GetValue<float>(KEY_VOLUME); }
    set { SetValue<float>(KEY_VOLUME, value); }
  } (...)
  static Settings()
  { (...)
    SetDefaultValue<float>(KEY_VOLUME, DEFAULT_VOLUME); (...)
  } (...)
}

You add two new constant values, representing the key and the default value (KEY_VOLUME and DEFAULT_VOLUME). Then, you create the static property (Volume) and call the SetDefaultValue method with suitable parameters in the static constructor. In a similar way, you should add settings regarding a consent to use vibrations (Vibrations property) and to send GPS data to the web service (Location property).

SettingsViewModel.cs

The last modifications are necessary in the view model for the Settings screen. Here, you need to automatically load current values while navigating to the page and save them while navigating from it, for example, when the player presses the back button.

To disable the possibility of storing an incorrect player name, you use the regular expression that has to be matched by every player name. It requires the name to contain only small (a-z) and capitals letters (A-Z), as well as digits, underscore, and dash. A length of the name has to be greater or equal to 3 and smaller or equal to 10. The regular expression is presented as follows:

private const string NAME_REGEX = "^[a-zA-Z0-9_-]{3,10}$";

When the user navigates to the Settings screen, you should present current values of all settings. To do it, you set values of Name, Volume, Vibrations, and UseLocation properties according to values obtained from the Settings class:

public void OnNavigatedTo(NavigationEventArgs e)
{
  this.Name = Settings.Name;
  this.Volume = Settings.Volume;
  this.Vibrations = Settings.Vibrations;
  this.UseLocation = Settings.Location;
}

To save the current version of settings, you implement the OnNavigatedFrom method. Here, you check whether the name matches the regular expression and only then you update the Name property (of the static Settings class). Otherwise, a suitable information is presented to the user. You can update other properties (Volume, Vibrations, and Location) without making any additional verification, as shown in the following block of code:

public void OnNavigatedFrom(NavigationEventArgs e)
{
  if (Regex.IsMatch(this.Name, NAME_REGEX))
  {
    Settings.Name = this.Name;
  }
  else
  {
    MessageBox.Show(AppResources.SettingsNameIncorrectMessage,
      AppResources.SettingsNameIncorrectTitle,
      MessageBoxButton.OK);
  }
  Settings.Volume = this.Volume;
  Settings.Vibrations = this.Vibrations;
  Settings.Location = this.UseLocation;
}

At the end, you need to override the OnNavigatedTo and OnNavigatedFrom methods inside the code-behind file (.xaml.cs), as well as call the OnNavigatedTo and OnNavigatedFrom methods from the view model. Thus, you can move the logic related to performing some operations when the player navigates to or from this page from the view to the view model part of the application.

MapViewModel.cs

In the previous chapter, you implemented a mechanism of sending your current location to the web service. However, you did not check whether the user really wants to do it. Thus, in this part you will modify this feature to use additional conditional expression to send GPS coordinates only when the player has a suitable option enabled in the Settings screen.

You need to make a small modification in the Geolocator_PositionChanged method inside the MapViewModel class. Here, you check whether the Location property of the Settings class is equal to TRUE. Only then, you send your location. Otherwise, no actions are taken, as shown as follows:

private void Geolocator_PositionChanged(Geolocator sender,
  PositionChangedEventArgs args)
{
  Deployment.Current.Dispatcher.BeginInvoke(() =>
  { (...)
    if (this.UserLocation == GeoCoordinate.Unknown)
    {
      this.Center = location;
      if (Settings.Location)
      {
        this.m_ws.SendLocationAsync(Settings.Key, 
          Settings.Name, (float)location.Latitude, 
          (float)location.Longitude);
      }
      this.m_ws.CloseAsync();
    } (...)
  });
}

You have finished developing the Settings screen. Thus, you can launch the game, navigate to this screen, update some values, and close it. After opening again, you should see exactly the same values as set earlier.

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

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