Chapter 20. Networking player location

 

This chapter covers

  • The updated Player model
  • Using an ItemsControl to display ships
  • Sending player updates across TCP

In the previous chapter you created the Ship user control and displayed it on the screen. In this chapter you’ll expand on that work. The primary purpose of this chapter is to show you how I filled out the glue code that makes the socket service and player display from the previous chapters respond to the user input from the next chapter. As part of this, you’ll learn some interesting techniques such as using a bound ItemsControl to display the players on the playing field and spinning up background threads to send test data across the wire.

You’ll start by updating the Player model class. If you want to send meaningful player information across the network, you’ll need more than just a name. Next, you’ll create a collection of Players in the game and expose that via the viewmodel. This collection will be maintained via network events and will be the source of data for an ItemsControl in the UI. The ItemsControl is an interesting use of a UI element because it won’t display a typical list of data but will instead render the ships onscreen at the correct positions.

Let’s start by expanding the Player model object.

20.1. Updating the Player model

20.2. The collection of players

20.3. Updating the TCP stream message service

20.4. Testing everything

20.5. Summary