Party.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //-----------------------------------------------------------------------------
  2. // Party.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 System.Collections.ObjectModel;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Xml.Linq;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RolePlaying.Data;
  17. namespace RolePlaying
  18. {
  19. /// <summary>
  20. /// The group of players, under control of the user.
  21. /// </summary>
  22. public class Party
  23. {
  24. /// <summary>
  25. /// The ordered list of players in the party.
  26. /// </summary>
  27. /// <remarks>The first entry is the leader.</remarks>
  28. private List<Player> players = new List<Player>();
  29. /// <summary>
  30. /// The ordered list of players in the party.
  31. /// </summary>
  32. /// <remarks>The first entry is the leader.</remarks>
  33. [ContentSerializerIgnore]
  34. public List<Player> Players
  35. {
  36. get { return players; }
  37. set { players = value; }
  38. }
  39. /// <summary>
  40. /// Add a new player to the party.
  41. /// </summary>
  42. /// <param name="player">The new player.</param>
  43. /// <remarks>Instead of using the lists directly, this fixes all data.</remarks>
  44. public void JoinParty(Player player)
  45. {
  46. // check the parameter
  47. if (player == null)
  48. {
  49. throw new ArgumentNullException("player");
  50. }
  51. if (players.Contains(player))
  52. {
  53. throw new ArgumentException("The player was already in the party.");
  54. }
  55. // add the new player to the list
  56. players.Add(player);
  57. // add the initial gold
  58. partyGold += player.Gold;
  59. // only as NPCs are players allowed to have their own gold
  60. player.Gold = 0;
  61. // add the player's inventory items
  62. foreach (ContentEntry<Gear> contentEntry in player.Inventory)
  63. {
  64. AddToInventory(contentEntry.Content, contentEntry.Count);
  65. }
  66. // only as NPCs are players allowed to have their own non-equipped gear
  67. player.Inventory.Clear();
  68. }
  69. /// <summary>
  70. /// Gives the experience amount specified to all party members.
  71. /// </summary>
  72. public void GiveExperience(int experience)
  73. {
  74. // check the parameters
  75. if (experience < 0)
  76. {
  77. throw new ArgumentOutOfRangeException("experience");
  78. }
  79. else if (experience == 0)
  80. {
  81. return;
  82. }
  83. List<Player> leveledUpPlayers = null;
  84. foreach (Player player in players)
  85. {
  86. int oldLevel = player.CharacterLevel;
  87. player.Experience += experience;
  88. if (player.CharacterLevel > oldLevel)
  89. {
  90. if (leveledUpPlayers == null)
  91. {
  92. leveledUpPlayers = new List<Player>();
  93. }
  94. leveledUpPlayers.Add(player);
  95. }
  96. }
  97. if ((leveledUpPlayers != null) && (leveledUpPlayers.Count > 0))
  98. {
  99. Session.ScreenManager.AddScreen(new LevelUpScreen(leveledUpPlayers));
  100. }
  101. }
  102. /// <summary>
  103. /// The items held by the party.
  104. /// </summary>
  105. private List<ContentEntry<Gear>> inventory = new List<ContentEntry<Gear>>();
  106. /// <summary>
  107. /// The items held by the party.
  108. /// </summary>
  109. [ContentSerializerIgnore]
  110. public ReadOnlyCollection<ContentEntry<Gear>> Inventory
  111. {
  112. get { return inventory.AsReadOnly(); }
  113. }
  114. /// <summary>
  115. /// Add the given gear, in the given quantity, to the party's inventory.
  116. /// </summary>
  117. public void AddToInventory(Gear gear, int count)
  118. {
  119. // check the parameters
  120. if ((gear == null) || (count <= 0))
  121. {
  122. return;
  123. }
  124. // search for an existing entry
  125. ContentEntry<Gear> existingEntry = inventory.Find(
  126. delegate (ContentEntry<Gear> entry)
  127. {
  128. return (entry.Content == gear);
  129. });
  130. // increment the existing entry, if any
  131. if (existingEntry != null)
  132. {
  133. existingEntry.Count += count;
  134. return;
  135. }
  136. // no existing entry - create a new entry
  137. ContentEntry<Gear> newEntry = new ContentEntry<Gear>();
  138. newEntry.Content = gear;
  139. newEntry.Count = count;
  140. newEntry.ContentName = gear.AssetName;
  141. if (newEntry.ContentName.StartsWith(@"Gear\"))
  142. {
  143. newEntry.ContentName = newEntry.ContentName.Substring(5);
  144. }
  145. inventory.Add(newEntry);
  146. }
  147. /// <summary>
  148. /// Remove the given quantity of the given gear from the party's inventory.
  149. /// </summary>
  150. /// <returns>True if the quantity specified could be removed.</returns>
  151. public bool RemoveFromInventory(Gear gear, int count)
  152. {
  153. // check the parameters
  154. if (gear == null)
  155. {
  156. throw new ArgumentNullException("gear");
  157. }
  158. if (count <= 0)
  159. {
  160. throw new ArgumentOutOfRangeException("count");
  161. }
  162. // search for an existing entry
  163. ContentEntry<Gear> existingEntry = inventory.Find(
  164. delegate (ContentEntry<Gear> entry)
  165. {
  166. return (entry.Content == gear);
  167. });
  168. // no existing entry, so this is moot
  169. if (existingEntry == null)
  170. {
  171. return false;
  172. }
  173. // decrement the existing entry
  174. existingEntry.Count -= count;
  175. bool fullRemoval = (existingEntry.Count >= 0);
  176. // if the entry is empty, then remove it
  177. if (existingEntry.Count <= 0)
  178. {
  179. inventory.Remove(existingEntry);
  180. }
  181. return fullRemoval;
  182. }
  183. /// <summary>
  184. /// The gold held by the party.
  185. /// </summary>
  186. private int partyGold;
  187. /// <summary>
  188. /// The gold held by the party.
  189. /// </summary>
  190. [ContentSerializer(Optional = true)]
  191. public int PartyGold
  192. {
  193. get { return partyGold; }
  194. set { partyGold = value; }
  195. }
  196. /// <summary>
  197. /// The name and kill-count of monsters killed in the active quest.
  198. /// </summary>
  199. /// <remarks>
  200. /// Used to determine if the requirements for the active quest have been met.
  201. /// </remarks>
  202. private Dictionary<string, int> monsterKills = new Dictionary<string, int>();
  203. /// <summary>
  204. /// The name and kill-count of monsters killed in the active quest.
  205. /// </summary>
  206. /// <remarks>
  207. /// Used to determine if the requirements for the active quest have been met.
  208. /// </remarks>
  209. public Dictionary<string, int> MonsterKills
  210. {
  211. get { return monsterKills; }
  212. }
  213. /// <summary>
  214. /// Add a new monster-kill to the party's records.
  215. /// </summary>
  216. public void AddMonsterKill(Monster monster)
  217. {
  218. if (monsterKills.ContainsKey(monster.AssetName))
  219. {
  220. monsterKills[monster.AssetName]++;
  221. }
  222. else
  223. {
  224. monsterKills.Add(monster.AssetName, 1);
  225. }
  226. }
  227. /// <summary>
  228. /// Creates a new Party object from the game-start description.
  229. /// </summary>
  230. public Party(GameStartDescription gameStartDescription,
  231. ContentManager contentManager)
  232. {
  233. // check the parameters
  234. if (gameStartDescription == null)
  235. {
  236. throw new ArgumentNullException("gameStartDescription");
  237. }
  238. if (contentManager == null)
  239. {
  240. throw new ArgumentNullException("contentManager");
  241. }
  242. // load the players
  243. foreach (string contentName in gameStartDescription.PlayerContentNames)
  244. {
  245. // load the player and add it to the party
  246. /* TODO var player = contentManager.Load<Player>(
  247. Path.Combine(@"Characters\Players", contentName)).Clone()
  248. as Player;*/
  249. var player = Player.Load(Path.Combine(@"Characters\Players", contentName), contentManager);
  250. JoinParty(player);
  251. }
  252. }
  253. /// <summary>
  254. /// Create a new Party object from serialized party data.
  255. /// </summary>
  256. public Party(PartySaveData partyData, ContentManager contentManager)
  257. {
  258. // check the parameters
  259. if (partyData == null)
  260. {
  261. throw new ArgumentNullException("partyData");
  262. }
  263. if (contentManager == null)
  264. {
  265. throw new ArgumentNullException("contentManager");
  266. }
  267. // load the players
  268. foreach (PlayerSaveData playerData in partyData.players)
  269. {
  270. /*Player player =
  271. contentManager.Load<Player>(playerData.assetName).Clone() as Player;*/
  272. var player = Player.Load(playerData.assetName, contentManager);
  273. player.CharacterLevel = playerData.characterLevel;
  274. player.Experience = playerData.experience;
  275. player.EquippedEquipment.Clear();
  276. foreach (string equipmentAssetName in playerData.equipmentAssetNames)
  277. {
  278. // TODO var equipment = contentManager.Load<Equipment>(equipmentAssetName);
  279. var equipment = Equipment.Load(equipmentAssetName);
  280. player.Equip(equipment);
  281. }
  282. player.StatisticsModifiers = playerData.statisticsModifiers;
  283. JoinParty(player);
  284. }
  285. // load the party inventory
  286. inventory.Clear();
  287. inventory.AddRange(partyData.inventory);
  288. foreach (ContentEntry<Gear> entry in inventory)
  289. {
  290. entry.Content = contentManager.Load<Gear>(
  291. Path.Combine(@"Gear", entry.ContentName));
  292. }
  293. // set the party gold
  294. partyGold = partyData.partyGold;
  295. // load the monster kills
  296. for (int i = 0; i < partyData.monsterKillNames.Count; i++)
  297. {
  298. monsterKills.Add(partyData.monsterKillNames[i],
  299. partyData.monsterKillCounts[i]);
  300. }
  301. }
  302. }
  303. }