Chest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Chest.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 System.Collections.Generic;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace RolePlayingGameData
  16. {
  17. /// <summary>
  18. /// A treasure chest in the game world.
  19. /// </summary>
  20. public class Chest : WorldObject
  21. #if WINDOWS
  22. , ICloneable
  23. #endif
  24. {
  25. #region Chest Contents
  26. /// <summary>
  27. /// The amount of gold in the chest.
  28. /// </summary>
  29. private int gold = 0;
  30. /// <summary>
  31. /// The amount of gold in the chest.
  32. /// </summary>
  33. [ContentSerializer(Optional = true)]
  34. public int Gold
  35. {
  36. get { return gold; }
  37. set { gold = value; }
  38. }
  39. /// <summary>
  40. /// The gear in the chest, along with quantities.
  41. /// </summary>
  42. private List<ContentEntry<Gear>> entries = new List<ContentEntry<Gear>>();
  43. /// <summary>
  44. /// The gear in the chest, along with quantities.
  45. /// </summary>
  46. public List<ContentEntry<Gear>> Entries
  47. {
  48. get { return entries; }
  49. set { entries = value; }
  50. }
  51. /// <summary>
  52. /// Array accessor for the chest's contents.
  53. /// </summary>
  54. public ContentEntry<Gear> this[int index]
  55. {
  56. get { return entries[index]; }
  57. }
  58. /// <summary>
  59. /// Returns true if the chest is empty.
  60. /// </summary>
  61. public bool IsEmpty
  62. {
  63. get { return ((gold <= 0) && (entries.Count <= 0)); }
  64. }
  65. #endregion
  66. #region Graphics Data
  67. /// <summary>
  68. /// The content name of the texture for this chest.
  69. /// </summary>
  70. private string textureName;
  71. /// <summary>
  72. /// The content name of the texture for this chest.
  73. /// </summary>
  74. public string TextureName
  75. {
  76. get { return textureName; }
  77. set { textureName = value; }
  78. }
  79. /// <summary>
  80. /// The texture for this chest.
  81. /// </summary>
  82. private Texture2D texture;
  83. /// <summary>
  84. /// The texture for this chest.
  85. /// </summary>
  86. [ContentSerializerIgnore]
  87. public Texture2D Texture
  88. {
  89. get { return texture; }
  90. set { texture = value; }
  91. }
  92. #endregion
  93. #region Content Type Reader
  94. /// <summary>
  95. /// Reads a Chest object from the content pipeline.
  96. /// </summary>
  97. public class ChestReader : ContentTypeReader<Chest>
  98. {
  99. protected override Chest Read(ContentReader input,
  100. Chest existingInstance)
  101. {
  102. Chest chest = existingInstance;
  103. if (chest == null)
  104. {
  105. chest = new Chest();
  106. }
  107. input.ReadRawObject<WorldObject>(chest as WorldObject);
  108. chest.Gold = input.ReadInt32();
  109. chest.Entries.AddRange(
  110. input.ReadObject<List<ContentEntry<Gear>>>());
  111. foreach (ContentEntry<Gear> contentEntry in chest.Entries)
  112. {
  113. contentEntry.Content = input.ContentManager.Load<Gear>(
  114. System.IO.Path.Combine(@"Gear",
  115. contentEntry.ContentName));
  116. }
  117. chest.TextureName = input.ReadString();
  118. chest.Texture = input.ContentManager.Load<Texture2D>(
  119. System.IO.Path.Combine(@"Textures\Chests", chest.TextureName));
  120. return chest;
  121. }
  122. }
  123. #endregion
  124. #region ICloneable Members
  125. /// <summary>
  126. /// Clone implementation for chest copies.
  127. /// </summary>
  128. /// <remarks>
  129. /// The game has to handle chests that have had some contents removed
  130. /// without modifying the original chest (and all chests that come after).
  131. /// </remarks>
  132. public object Clone()
  133. {
  134. // create the new chest
  135. Chest chest = new Chest();
  136. // copy the data
  137. chest.Gold = Gold;
  138. chest.Name = Name;
  139. chest.Texture = Texture;
  140. chest.TextureName = TextureName;
  141. // recreate the list and entries, as counts may have changed
  142. chest.entries = new List<ContentEntry<Gear>>();
  143. foreach (ContentEntry<Gear> originalEntry in Entries)
  144. {
  145. ContentEntry<Gear> newEntry = new ContentEntry<Gear>();
  146. newEntry.Count = originalEntry.Count;
  147. newEntry.ContentName = originalEntry.ContentName;
  148. newEntry.Content = originalEntry.Content;
  149. chest.Entries.Add(newEntry);
  150. }
  151. return chest;
  152. }
  153. #endregion
  154. }
  155. }