#region File Description //----------------------------------------------------------------------------- // Chest.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; #endregion namespace RolePlayingGameData { /// /// A treasure chest in the game world. /// public class Chest : WorldObject #if WINDOWS , ICloneable #endif { #region Chest Contents /// /// The amount of gold in the chest. /// private int gold = 0; /// /// The amount of gold in the chest. /// [ContentSerializer(Optional = true)] public int Gold { get { return gold; } set { gold = value; } } /// /// The gear in the chest, along with quantities. /// private List> entries = new List>(); /// /// The gear in the chest, along with quantities. /// public List> Entries { get { return entries; } set { entries = value; } } /// /// Array accessor for the chest's contents. /// public ContentEntry this[int index] { get { return entries[index]; } } /// /// Returns true if the chest is empty. /// public bool IsEmpty { get { return ((gold <= 0) && (entries.Count <= 0)); } } #endregion #region Graphics Data /// /// The content name of the texture for this chest. /// private string textureName; /// /// The content name of the texture for this chest. /// public string TextureName { get { return textureName; } set { textureName = value; } } /// /// The texture for this chest. /// private Texture2D texture; /// /// The texture for this chest. /// [ContentSerializerIgnore] public Texture2D Texture { get { return texture; } set { texture = value; } } #endregion #region Content Type Reader /// /// Reads a Chest object from the content pipeline. /// public class ChestReader : ContentTypeReader { protected override Chest Read(ContentReader input, Chest existingInstance) { Chest chest = existingInstance; if (chest == null) { chest = new Chest(); } input.ReadRawObject(chest as WorldObject); chest.Gold = input.ReadInt32(); chest.Entries.AddRange( input.ReadObject>>()); foreach (ContentEntry contentEntry in chest.Entries) { contentEntry.Content = input.ContentManager.Load( System.IO.Path.Combine(@"Gear", contentEntry.ContentName)); } chest.TextureName = input.ReadString(); chest.Texture = input.ContentManager.Load( System.IO.Path.Combine(@"Textures\Chests", chest.TextureName)); return chest; } } #endregion #region ICloneable Members /// /// Clone implementation for chest copies. /// /// /// The game has to handle chests that have had some contents removed /// without modifying the original chest (and all chests that come after). /// public object Clone() { // create the new chest Chest chest = new Chest(); // copy the data chest.Gold = Gold; chest.Name = Name; chest.Texture = Texture; chest.TextureName = TextureName; // recreate the list and entries, as counts may have changed chest.entries = new List>(); foreach (ContentEntry originalEntry in Entries) { ContentEntry newEntry = new ContentEntry(); newEntry.Count = originalEntry.Count; newEntry.ContentName = originalEntry.ContentName; newEntry.Content = originalEntry.Content; chest.Entries.Add(newEntry); } return chest; } #endregion } }