PlayerData.cs 3.6 KB

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