QuestNpc.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //-----------------------------------------------------------------------------
  2. // QuestNpc.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Content;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace RolePlaying.Data
  13. {
  14. /// <summary>
  15. /// An NPC that does not fight and does not join the party.
  16. /// </summary>
  17. public class QuestNpc : Character
  18. {
  19. /// <summary>
  20. /// The dialogue that the Npc says when it is greeted in the world.
  21. /// </summary>
  22. private string introductionDialogue;
  23. /// <summary>
  24. /// The dialogue that the Npc says when it is greeted in the world.
  25. /// </summary>
  26. public string IntroductionDialogue
  27. {
  28. get { return introductionDialogue; }
  29. set { introductionDialogue = value; }
  30. }
  31. public static QuestNpc Load(string npcPath, ContentManager contentManager)
  32. {
  33. var asset = XmlHelper.GetAssetElementFromXML(npcPath);
  34. var questNpc = new QuestNpc
  35. {
  36. AssetName = npcPath,
  37. Name = (string)asset.Element("Name"),
  38. Direction = Enum.TryParse<Direction>((string)asset.Element("Direction"), out var dir) ? dir : default,
  39. IntroductionDialogue = asset.Element("IntroductionDialogue").Value,
  40. MapIdleAnimationInterval = asset.Element("MapIdleAnimationInterval") != null
  41. ? int.TryParse((string)asset.Element("MapIdleAnimationInterval"), out var interval) ? interval : default
  42. : default,
  43. MapSprite = asset.Element("MapSprite") != null
  44. ? AnimatingSprite.Load(asset.Element("MapSprite"), contentManager)
  45. : null,
  46. };
  47. questNpc.AddStandardCharacterIdleAnimations();
  48. questNpc.AddStandardCharacterWalkingAnimations();
  49. questNpc.ResetAnimation(false);
  50. questNpc.ShadowTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "CharacterShadow"));
  51. return questNpc;
  52. }
  53. /// <summary>
  54. /// Read a QuestNpc object from the content pipeline.
  55. /// </summary>
  56. public class QuestNpcReader : ContentTypeReader<QuestNpc>
  57. {
  58. protected override QuestNpc Read(ContentReader input,
  59. QuestNpc existingInstance)
  60. {
  61. QuestNpc questNpc = existingInstance;
  62. if (questNpc == null)
  63. {
  64. questNpc = new QuestNpc();
  65. }
  66. input.ReadRawObject<Character>(questNpc as Character);
  67. questNpc.IntroductionDialogue = input.ReadString();
  68. return questNpc;
  69. }
  70. }
  71. }
  72. }