PlayerData.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PlayerData.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Net;
  14. #endregion
  15. namespace NetRumble
  16. {
  17. /// <summary>
  18. /// Data for each player in a network session.
  19. /// </summary>
  20. public class PlayerData
  21. {
  22. #region Gameplay Data
  23. /// <summary>
  24. /// The color of the overlay portion of the ship.
  25. /// </summary>
  26. private byte shipColor = 0;
  27. public byte ShipColor
  28. {
  29. get { return shipColor; }
  30. set
  31. {
  32. if ((value < 0) || (value >= Ship.ShipColors.Length))
  33. {
  34. throw new ArgumentOutOfRangeException("value");
  35. }
  36. shipColor = value;
  37. // apply the change to the ship immediately
  38. if (ship != null)
  39. {
  40. ship.Color = Ship.ShipColors[shipColor];
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// The ship model to use.
  46. /// </summary>
  47. private byte shipVariation = 0;
  48. public byte ShipVariation
  49. {
  50. get { return shipVariation; }
  51. set
  52. {
  53. if ((value < 0) || (value >= Ship.Variations))
  54. {
  55. throw new ArgumentOutOfRangeException("value");
  56. }
  57. shipVariation = value;
  58. // apply the change to the ship immediately
  59. if (ship != null)
  60. {
  61. ship.Variation = shipVariation;
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// The ship used by this player.
  67. /// </summary>
  68. private Ship ship = null;
  69. public Ship Ship
  70. {
  71. get { return ship; }
  72. set
  73. {
  74. ship = value;
  75. // apply the other values to the ship immediately
  76. if (ship != null)
  77. {
  78. ship.Variation = shipVariation;
  79. ship.Color = Ship.ShipColors[shipColor];
  80. }
  81. }
  82. }
  83. #endregion
  84. #region Initialization Methods
  85. /// <summary>
  86. /// Constructs a PlayerData object.
  87. /// </summary>
  88. public PlayerData()
  89. {
  90. Ship = new Ship();
  91. }
  92. #endregion
  93. #region Networking Methods
  94. /// <summary>
  95. /// Deserialize from the packet into the current object.
  96. /// </summary>
  97. /// <param name="packetReader">The packet reader that has the data.</param>
  98. public void Deserialize(PacketReader packetReader)
  99. {
  100. // safety-check the parameter, as it must be valid.
  101. if (packetReader == null)
  102. {
  103. throw new ArgumentNullException("packetReader");
  104. }
  105. ShipColor = packetReader.ReadByte();
  106. ShipVariation = packetReader.ReadByte();
  107. }
  108. /// <summary>
  109. /// Serialize the current object to a packet.
  110. /// </summary>
  111. /// <param name="packetWriter">The packet writer that receives the data.</param>
  112. public void Serialize(PacketWriter packetWriter)
  113. {
  114. // safety-check the parameter, as it must be valid.
  115. if (packetWriter == null)
  116. {
  117. throw new ArgumentNullException("packetWriter");
  118. }
  119. packetWriter.Write(ShipColor);
  120. packetWriter.Write(ShipVariation);
  121. }
  122. #endregion
  123. }
  124. }