Chest.cs 4.9 KB

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