Player.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //-----------------------------------------------------------------------------
  2. // Player.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Xml.Linq;
  15. namespace RolePlaying.Data
  16. {
  17. /// <summary>
  18. /// A member of the player's party, also represented in the world before joining.
  19. /// </summary>
  20. /// <remarks>
  21. /// There is only one of a given Player in the game world at a time, and their
  22. /// current statistics persist after combat. Thererefore, current statistics
  23. /// are tracked here.
  24. /// </remarks>
  25. public class Player : FightingCharacter
  26. #if WINDOWS
  27. , ICloneable
  28. #endif
  29. {
  30. /// <summary>
  31. /// The current set of persistent statistics modifiers - damage, etc.
  32. /// </summary>
  33. [ContentSerializerIgnore]
  34. public StatisticsValue StatisticsModifiers = new StatisticsValue();
  35. /// <summary>
  36. /// The current set of statistics, including damage, etc.
  37. /// </summary>
  38. [ContentSerializerIgnore]
  39. public StatisticsValue CurrentStatistics
  40. {
  41. get { return CharacterStatistics + StatisticsModifiers; }
  42. }
  43. /// <summary>
  44. /// The amount of gold that the player has when it joins the party.
  45. /// </summary>
  46. private int gold;
  47. /// <summary>
  48. /// The amount of gold that the player has when it joins the party.
  49. /// </summary>
  50. public int Gold
  51. {
  52. get { return gold; }
  53. set { gold = value; }
  54. }
  55. /// <summary>
  56. /// The dialogue that the player says when it is greeted as an Npc in the world.
  57. /// </summary>
  58. private string introductionDialogue;
  59. /// <summary>
  60. /// The dialogue that the player says when it is greeted as an Npc in the world.
  61. /// </summary>
  62. public string IntroductionDialogue
  63. {
  64. get { return introductionDialogue; }
  65. set { introductionDialogue = value; }
  66. }
  67. /// <summary>
  68. /// The dialogue that the player says when its offer to join is accepted.
  69. /// </summary>
  70. private string joinAcceptedDialogue;
  71. /// <summary>
  72. /// The dialogue that the player says when its offer to join is accepted.
  73. /// </summary>
  74. public string JoinAcceptedDialogue
  75. {
  76. get { return joinAcceptedDialogue; }
  77. set { joinAcceptedDialogue = value; }
  78. }
  79. /// <summary>
  80. /// The dialogue that the player says when its offer to join is rejected.
  81. /// </summary>
  82. private string joinRejectedDialogue;
  83. /// <summary>
  84. /// The dialogue that the player says when its offer to join is rejected.
  85. /// </summary>
  86. public string JoinRejectedDialogue
  87. {
  88. get { return joinRejectedDialogue; }
  89. set { joinRejectedDialogue = value; }
  90. }
  91. /// <summary>
  92. /// The name of the active portrait texture.
  93. /// </summary>
  94. private string activePortraitTextureName;
  95. /// <summary>
  96. /// The name of the active portrait texture.
  97. /// </summary>
  98. public string ActivePortraitTextureName
  99. {
  100. get { return activePortraitTextureName; }
  101. set { activePortraitTextureName = value; }
  102. }
  103. /// <summary>
  104. /// The active portrait texture.
  105. /// </summary>
  106. private Texture2D activePortraitTexture;
  107. /// <summary>
  108. /// The active portrait texture.
  109. /// </summary>
  110. [ContentSerializerIgnore]
  111. public Texture2D ActivePortraitTexture
  112. {
  113. get { return activePortraitTexture; }
  114. set { activePortraitTexture = value; }
  115. }
  116. /// <summary>
  117. /// The name of the inactive portrait texture.
  118. /// </summary>
  119. private string inactivePortraitTextureName;
  120. /// <summary>
  121. /// The name of the inactive portrait texture.
  122. /// </summary>
  123. public string InactivePortraitTextureName
  124. {
  125. get { return inactivePortraitTextureName; }
  126. set { inactivePortraitTextureName = value; }
  127. }
  128. /// <summary>
  129. /// The inactive portrait texture.
  130. /// </summary>
  131. private Texture2D inactivePortraitTexture;
  132. /// <summary>
  133. /// The inactive portrait texture.
  134. /// </summary>
  135. [ContentSerializerIgnore]
  136. public Texture2D InactivePortraitTexture
  137. {
  138. get { return inactivePortraitTexture; }
  139. set { inactivePortraitTexture = value; }
  140. }
  141. /// <summary>
  142. /// The name of the unselectable portrait texture.
  143. /// </summary>
  144. private string unselectablePortraitTextureName;
  145. /// <summary>
  146. /// The name of the unselectable portrait texture.
  147. /// </summary>
  148. public string UnselectablePortraitTextureName
  149. {
  150. get { return unselectablePortraitTextureName; }
  151. set { unselectablePortraitTextureName = value; }
  152. }
  153. /// <summary>
  154. /// The unselectable portrait texture.
  155. /// </summary>
  156. private Texture2D unselectablePortraitTexture;
  157. /// <summary>
  158. /// The unselectable portrait texture.
  159. /// </summary>
  160. [ContentSerializerIgnore]
  161. public Texture2D UnselectablePortraitTexture
  162. {
  163. get { return unselectablePortraitTexture; }
  164. set { unselectablePortraitTexture = value; }
  165. }
  166. /// <summary>
  167. /// Read a Player object from the content pipeline.
  168. /// </summary>
  169. public class PlayerReader : ContentTypeReader<Player>
  170. {
  171. protected override Player Read(ContentReader input, Player existingInstance)
  172. {
  173. Player player = existingInstance;
  174. if (player == null)
  175. {
  176. player = new Player();
  177. }
  178. input.ReadRawObject<FightingCharacter>(player as FightingCharacter);
  179. player.Gold = input.ReadInt32();
  180. player.IntroductionDialogue = input.ReadString();
  181. player.JoinAcceptedDialogue = input.ReadString();
  182. player.JoinRejectedDialogue = input.ReadString();
  183. player.ActivePortraitTextureName = input.ReadString();
  184. player.activePortraitTexture =
  185. input.ContentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", player.ActivePortraitTextureName));
  186. player.InactivePortraitTextureName = input.ReadString();
  187. player.inactivePortraitTexture =
  188. input.ContentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", player.InactivePortraitTextureName));
  189. player.UnselectablePortraitTextureName = input.ReadString();
  190. player.unselectablePortraitTexture =
  191. input.ContentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", player.UnselectablePortraitTextureName));
  192. return player;
  193. }
  194. }
  195. public object Clone()
  196. {
  197. Player player = new Player();
  198. player.activePortraitTexture = activePortraitTexture;
  199. player.activePortraitTextureName = activePortraitTextureName;
  200. player.AssetName = AssetName;
  201. player.CharacterClass = CharacterClass;
  202. player.CharacterClassContentName = CharacterClassContentName;
  203. player.CharacterLevel = CharacterLevel;
  204. player.CombatAnimationInterval = CombatAnimationInterval;
  205. player.CombatSprite = CombatSprite.Clone() as AnimatingSprite;
  206. player.Direction = Direction;
  207. player.EquippedEquipment.AddRange(EquippedEquipment);
  208. player.Experience = Experience;
  209. player.gold = gold;
  210. player.inactivePortraitTexture = inactivePortraitTexture;
  211. player.inactivePortraitTextureName = inactivePortraitTextureName;
  212. player.InitialEquipmentContentNames.AddRange(InitialEquipmentContentNames);
  213. player.introductionDialogue = introductionDialogue;
  214. player.Inventory.AddRange(Inventory);
  215. player.joinAcceptedDialogue = joinAcceptedDialogue;
  216. player.joinRejectedDialogue = joinRejectedDialogue;
  217. player.MapIdleAnimationInterval = MapIdleAnimationInterval;
  218. player.MapPosition = MapPosition;
  219. player.MapSprite = MapSprite.Clone() as AnimatingSprite;
  220. player.MapWalkingAnimationInterval = MapWalkingAnimationInterval;
  221. player.Name = Name;
  222. player.ShadowTexture = ShadowTexture;
  223. player.State = State;
  224. player.unselectablePortraitTexture = unselectablePortraitTexture;
  225. player.unselectablePortraitTextureName = unselectablePortraitTextureName;
  226. player.WalkingSprite = WalkingSprite.Clone() as AnimatingSprite;
  227. player.RecalculateEquipmentStatistics();
  228. player.RecalculateTotalDefenseRanges();
  229. player.RecalculateTotalTargetDamageRange();
  230. player.ResetAnimation(false);
  231. player.ResetBaseStatistics();
  232. return player;
  233. }
  234. public static Player Load(string contentName, ContentManager contentManager)
  235. {
  236. var asset = XmlHelper.GetAssetElementFromXML(contentName);
  237. // Create a new Player instance and populate it with data from the XML asset
  238. var player = new Player
  239. {
  240. AssetName = contentName,
  241. Name = (string)asset.Element("Name"),
  242. Direction = Enum.TryParse<Direction>((string)asset.Element("Direction"), out var dir) ? dir : default,
  243. MapSprite = AnimatingSprite.Load(asset.Element("MapSprite"), contentManager),
  244. MapPosition = asset.Element("MapPosition") != null ? new Point(
  245. (int)asset.Element("MapPosition").Element("X"),
  246. (int)asset.Element("MapPosition").Element("Y")) : Point.Zero,
  247. WalkingSprite = AnimatingSprite.Load(asset.Element("WalkingSprite"), contentManager),
  248. MapIdleAnimationInterval = (int)asset.Element("MapIdleAnimationInterval"),
  249. CharacterClassContentName = (string)asset.Element("CharacterClassContentName"),
  250. CharacterLevel = (int)asset.Element("CharacterLevel"),
  251. InitialEquipmentContentNames = asset.Element("InitialEquipmentContentNames")
  252. .Elements("Item").Select(x => (string)x).ToList() ?? new List<string>(),
  253. CombatSprite = AnimatingSprite.Load(asset.Element("CombatSprite"), contentManager),
  254. Gold = (int)asset.Element("Gold"),
  255. IntroductionDialogue = (string)asset.Element("IntroductionDialogue"),
  256. JoinAcceptedDialogue = (string)asset.Element("JoinAcceptedDialogue"),
  257. JoinRejectedDialogue = (string)asset.Element("JoinRejectedDialogue"),
  258. ActivePortraitTextureName = (string)asset.Element("ActivePortraitTextureName"),
  259. ActivePortraitTexture = contentManager.Load<Texture2D>( Path.Combine("Textures", "Characters", "Portraits", (string)asset.Element("ActivePortraitTextureName"))),
  260. InactivePortraitTextureName = (string)asset.Element("InactivePortraitTextureName"),
  261. InactivePortraitTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", (string)asset.Element("InactivePortraitTextureName"))),
  262. UnselectablePortraitTextureName = (string)asset.Element("UnselectablePortraitTextureName"),
  263. UnselectablePortraitTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", (string)asset.Element("UnselectablePortraitTextureName"))),
  264. Inventory = asset.Element("Inventory")?.Elements("Item")
  265. .Select(x => new ContentEntry<Gear>
  266. {
  267. Content = Item.Load(Path.Combine("Gear", (string)x.Element("ContentName")), contentManager),
  268. ContentName = (string)x.Element("ContentName"),
  269. Count = (int?)x.Element("Count") ?? 1 // Default to 1 if not specified
  270. })
  271. .ToList() ?? new List<ContentEntry<Gear>>()
  272. };
  273. // load the character class
  274. player.CharacterClass = CharacterClass.Load(Path.Combine("CharacterClasses", player.CharacterClassContentName), contentManager);
  275. foreach (var item in player.InitialEquipmentContentNames)
  276. {
  277. player.EquippedEquipment.Add(Equipment.Load(Path.Combine("Gear", item), contentManager));
  278. }
  279. player.AddStandardCharacterCombatAnimations();
  280. player.AddStandardCharacterIdleAnimations();
  281. player.AddStandardCharacterWalkingAnimations();
  282. player.ResetAnimation(false);
  283. player.ShadowTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "CharacterShadow"));
  284. return player;
  285. }
  286. }
  287. }