Player.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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>(
  186. System.IO.Path.Combine(@"Textures\Characters\Portraits",
  187. player.ActivePortraitTextureName));
  188. player.InactivePortraitTextureName = input.ReadString();
  189. player.inactivePortraitTexture =
  190. input.ContentManager.Load<Texture2D>(
  191. System.IO.Path.Combine(@"Textures\Characters\Portraits",
  192. player.InactivePortraitTextureName));
  193. player.UnselectablePortraitTextureName = input.ReadString();
  194. player.unselectablePortraitTexture =
  195. input.ContentManager.Load<Texture2D>(
  196. System.IO.Path.Combine(@"Textures\Characters\Portraits",
  197. player.UnselectablePortraitTextureName));
  198. return player;
  199. }
  200. }
  201. public object Clone()
  202. {
  203. Player player = new Player();
  204. player.activePortraitTexture = activePortraitTexture;
  205. player.activePortraitTextureName = activePortraitTextureName;
  206. player.AssetName = AssetName;
  207. player.CharacterClass = CharacterClass;
  208. player.CharacterClassContentName = CharacterClassContentName;
  209. player.CharacterLevel = CharacterLevel;
  210. player.CombatAnimationInterval = CombatAnimationInterval;
  211. player.CombatSprite = CombatSprite.Clone() as AnimatingSprite;
  212. player.Direction = Direction;
  213. player.EquippedEquipment.AddRange(EquippedEquipment);
  214. player.Experience = Experience;
  215. player.gold = gold;
  216. player.inactivePortraitTexture = inactivePortraitTexture;
  217. player.inactivePortraitTextureName = inactivePortraitTextureName;
  218. player.InitialEquipmentContentNames.AddRange(InitialEquipmentContentNames);
  219. player.introductionDialogue = introductionDialogue;
  220. player.Inventory.AddRange(Inventory);
  221. player.joinAcceptedDialogue = joinAcceptedDialogue;
  222. player.joinRejectedDialogue = joinRejectedDialogue;
  223. player.MapIdleAnimationInterval = MapIdleAnimationInterval;
  224. player.MapPosition = MapPosition;
  225. player.MapSprite = MapSprite.Clone() as AnimatingSprite;
  226. player.MapWalkingAnimationInterval = MapWalkingAnimationInterval;
  227. player.Name = Name;
  228. player.ShadowTexture = ShadowTexture;
  229. player.State = State;
  230. player.unselectablePortraitTexture = unselectablePortraitTexture;
  231. player.unselectablePortraitTextureName = unselectablePortraitTextureName;
  232. player.WalkingSprite = WalkingSprite.Clone() as AnimatingSprite;
  233. player.RecalculateEquipmentStatistics();
  234. player.RecalculateTotalDefenseRanges();
  235. player.RecalculateTotalTargetDamageRange();
  236. player.ResetAnimation(false);
  237. player.ResetBaseStatistics();
  238. return player;
  239. }
  240. public static Player Load(string playerPath, ContentManager contentManager)
  241. {
  242. var asset = XmlHelper.GetAssetElementFromXML(playerPath);
  243. var player = new Player
  244. {
  245. Name = (string)asset.Element("Name"),
  246. MapSprite = new AnimatingSprite
  247. {
  248. TextureName = (string)asset.Element("MapSprite").Element("TextureName"),
  249. Texture = contentManager.Load<Texture2D>(
  250. Path.Combine(@"Textures\", (string)asset.Element("MapSprite").Element("TextureName"))),
  251. FrameDimensions = new Point(
  252. int.Parse(asset.Element("MapSprite").Element("FrameDimensions").Value.Split(' ')[0]),
  253. int.Parse(asset.Element("MapSprite").Element("FrameDimensions").Value.Split(' ')[1])),
  254. FramesPerRow = (int)asset.Element("MapSprite").Element("FramesPerRow"),
  255. SourceOffset = new Vector2(
  256. int.Parse(asset.Element("MapSprite").Element("SourceOffset").Value.Split(' ')[0]),
  257. int.Parse(asset.Element("MapSprite").Element("SourceOffset").Value.Split(' ')[1])),
  258. // Handle Animations if needed
  259. },
  260. WalkingSprite = new AnimatingSprite
  261. {
  262. TextureName = (string)asset.Element("WalkingSprite").Element("TextureName"),
  263. Texture = contentManager.Load<Texture2D>(
  264. Path.Combine(@"Textures\", (string)asset.Element("WalkingSprite").Element("TextureName"))),
  265. FrameDimensions = new Point(
  266. int.Parse(asset.Element("WalkingSprite").Element("FrameDimensions").Value.Split(' ')[0]),
  267. int.Parse(asset.Element("WalkingSprite").Element("FrameDimensions").Value.Split(' ')[1])),
  268. FramesPerRow = (int)asset.Element("WalkingSprite").Element("FramesPerRow"),
  269. SourceOffset = new Vector2(
  270. int.Parse(asset.Element("WalkingSprite").Element("SourceOffset").Value.Split(' ')[0]),
  271. int.Parse(asset.Element("WalkingSprite").Element("SourceOffset").Value.Split(' ')[1])),
  272. // Handle Animations if needed
  273. },
  274. MapIdleAnimationInterval = (int)asset.Element("MapIdleAnimationInterval"),
  275. CharacterClassContentName = (string)asset.Element("CharacterClassContentName"),
  276. CharacterLevel = (int)asset.Element("CharacterLevel"),
  277. InitialEquipmentContentNames = asset.Element("InitialEquipmentContentNames")
  278. .Elements("Item").Select(x => (string)x).ToList(),
  279. CombatSprite = new AnimatingSprite
  280. {
  281. TextureName = (string)asset.Element("CombatSprite").Element("TextureName"),
  282. Texture = contentManager.Load<Texture2D>(
  283. Path.Combine(@"Textures\", (string)asset.Element("CombatSprite").Element("TextureName"))),
  284. FrameDimensions = new Point(
  285. int.Parse(asset.Element("CombatSprite").Element("FrameDimensions").Value.Split(' ')[0]),
  286. int.Parse(asset.Element("CombatSprite").Element("FrameDimensions").Value.Split(' ')[1])),
  287. FramesPerRow = (int)asset.Element("CombatSprite").Element("FramesPerRow"),
  288. SourceOffset = new Vector2(
  289. int.Parse(asset.Element("CombatSprite").Element("SourceOffset").Value.Split(' ')[0]),
  290. int.Parse(asset.Element("CombatSprite").Element("SourceOffset").Value.Split(' ')[1])),
  291. // Handle Animations if needed
  292. },
  293. Gold = (int)asset.Element("Gold"),
  294. IntroductionDialogue = (string)asset.Element("IntroductionDialogue"),
  295. JoinAcceptedDialogue = (string)asset.Element("JoinAcceptedDialogue"),
  296. JoinRejectedDialogue = (string)asset.Element("JoinRejectedDialogue"),
  297. ActivePortraitTextureName = (string)asset.Element("ActivePortraitTextureName"),
  298. ActivePortraitTexture = contentManager.Load<Texture2D>(
  299. Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("ActivePortraitTextureName"))),
  300. InactivePortraitTextureName = (string)asset.Element("InactivePortraitTextureName"),
  301. InactivePortraitTexture = contentManager.Load<Texture2D>(
  302. Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("InactivePortraitTextureName"))),
  303. UnselectablePortraitTextureName = (string)asset.Element("UnselectablePortraitTextureName"),
  304. UnselectablePortraitTexture = contentManager.Load<Texture2D>(
  305. Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("UnselectablePortraitTextureName"))),
  306. Inventory = asset.Element("Inventory")?.Elements("Item")
  307. .Select(x => new ContentEntry<Gear>
  308. {
  309. ContentName = (string)x.Element("ContentName"),
  310. Count = (int?)x.Element("Count") ?? 1 // Default to 1 if not specified
  311. })
  312. .ToList() ?? new List<ContentEntry<Gear>>()
  313. };
  314. // load the character class
  315. player.CharacterClass = CharacterClass.Load(Path.Combine("CharacterClasses", player.CharacterClassContentName));
  316. return player;
  317. }
  318. }
  319. }