| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- //-----------------------------------------------------------------------------
- // Player.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Xml.Linq;
- namespace RolePlaying.Data
- {
- /// <summary>
- /// A member of the player's party, also represented in the world before joining.
- /// </summary>
- /// <remarks>
- /// There is only one of a given Player in the game world at a time, and their
- /// current statistics persist after combat. Thererefore, current statistics
- /// are tracked here.
- /// </remarks>
- public class Player : FightingCharacter
- #if WINDOWS
- , ICloneable
- #endif
- {
- /// <summary>
- /// The current set of persistent statistics modifiers - damage, etc.
- /// </summary>
- [ContentSerializerIgnore]
- public StatisticsValue StatisticsModifiers = new StatisticsValue();
- /// <summary>
- /// The current set of statistics, including damage, etc.
- /// </summary>
- [ContentSerializerIgnore]
- public StatisticsValue CurrentStatistics
- {
- get { return CharacterStatistics + StatisticsModifiers; }
- }
- /// <summary>
- /// The amount of gold that the player has when it joins the party.
- /// </summary>
- private int gold;
- /// <summary>
- /// The amount of gold that the player has when it joins the party.
- /// </summary>
- public int Gold
- {
- get { return gold; }
- set { gold = value; }
- }
- /// <summary>
- /// The dialogue that the player says when it is greeted as an Npc in the world.
- /// </summary>
- private string introductionDialogue;
- /// <summary>
- /// The dialogue that the player says when it is greeted as an Npc in the world.
- /// </summary>
- public string IntroductionDialogue
- {
- get { return introductionDialogue; }
- set { introductionDialogue = value; }
- }
- /// <summary>
- /// The dialogue that the player says when its offer to join is accepted.
- /// </summary>
- private string joinAcceptedDialogue;
- /// <summary>
- /// The dialogue that the player says when its offer to join is accepted.
- /// </summary>
- public string JoinAcceptedDialogue
- {
- get { return joinAcceptedDialogue; }
- set { joinAcceptedDialogue = value; }
- }
- /// <summary>
- /// The dialogue that the player says when its offer to join is rejected.
- /// </summary>
- private string joinRejectedDialogue;
- /// <summary>
- /// The dialogue that the player says when its offer to join is rejected.
- /// </summary>
- public string JoinRejectedDialogue
- {
- get { return joinRejectedDialogue; }
- set { joinRejectedDialogue = value; }
- }
- /// <summary>
- /// The name of the active portrait texture.
- /// </summary>
- private string activePortraitTextureName;
- /// <summary>
- /// The name of the active portrait texture.
- /// </summary>
- public string ActivePortraitTextureName
- {
- get { return activePortraitTextureName; }
- set { activePortraitTextureName = value; }
- }
- /// <summary>
- /// The active portrait texture.
- /// </summary>
- private Texture2D activePortraitTexture;
- /// <summary>
- /// The active portrait texture.
- /// </summary>
- [ContentSerializerIgnore]
- public Texture2D ActivePortraitTexture
- {
- get { return activePortraitTexture; }
- set { activePortraitTexture = value; }
- }
- /// <summary>
- /// The name of the inactive portrait texture.
- /// </summary>
- private string inactivePortraitTextureName;
- /// <summary>
- /// The name of the inactive portrait texture.
- /// </summary>
- public string InactivePortraitTextureName
- {
- get { return inactivePortraitTextureName; }
- set { inactivePortraitTextureName = value; }
- }
- /// <summary>
- /// The inactive portrait texture.
- /// </summary>
- private Texture2D inactivePortraitTexture;
- /// <summary>
- /// The inactive portrait texture.
- /// </summary>
- [ContentSerializerIgnore]
- public Texture2D InactivePortraitTexture
- {
- get { return inactivePortraitTexture; }
- set { inactivePortraitTexture = value; }
- }
- /// <summary>
- /// The name of the unselectable portrait texture.
- /// </summary>
- private string unselectablePortraitTextureName;
- /// <summary>
- /// The name of the unselectable portrait texture.
- /// </summary>
- public string UnselectablePortraitTextureName
- {
- get { return unselectablePortraitTextureName; }
- set { unselectablePortraitTextureName = value; }
- }
- /// <summary>
- /// The unselectable portrait texture.
- /// </summary>
- private Texture2D unselectablePortraitTexture;
- /// <summary>
- /// The unselectable portrait texture.
- /// </summary>
- [ContentSerializerIgnore]
- public Texture2D UnselectablePortraitTexture
- {
- get { return unselectablePortraitTexture; }
- set { unselectablePortraitTexture = value; }
- }
- /// <summary>
- /// Read a Player object from the content pipeline.
- /// </summary>
- public class PlayerReader : ContentTypeReader<Player>
- {
- protected override Player Read(ContentReader input, Player existingInstance)
- {
- Player player = existingInstance;
- if (player == null)
- {
- player = new Player();
- }
- input.ReadRawObject<FightingCharacter>(player as FightingCharacter);
- player.Gold = input.ReadInt32();
- player.IntroductionDialogue = input.ReadString();
- player.JoinAcceptedDialogue = input.ReadString();
- player.JoinRejectedDialogue = input.ReadString();
- player.ActivePortraitTextureName = input.ReadString();
- player.activePortraitTexture =
- input.ContentManager.Load<Texture2D>(
- System.IO.Path.Combine(@"Textures\Characters\Portraits",
- player.ActivePortraitTextureName));
- player.InactivePortraitTextureName = input.ReadString();
- player.inactivePortraitTexture =
- input.ContentManager.Load<Texture2D>(
- System.IO.Path.Combine(@"Textures\Characters\Portraits",
- player.InactivePortraitTextureName));
- player.UnselectablePortraitTextureName = input.ReadString();
- player.unselectablePortraitTexture =
- input.ContentManager.Load<Texture2D>(
- System.IO.Path.Combine(@"Textures\Characters\Portraits",
- player.UnselectablePortraitTextureName));
- return player;
- }
- }
- public object Clone()
- {
- Player player = new Player();
- player.activePortraitTexture = activePortraitTexture;
- player.activePortraitTextureName = activePortraitTextureName;
- player.AssetName = AssetName;
- player.CharacterClass = CharacterClass;
- player.CharacterClassContentName = CharacterClassContentName;
- player.CharacterLevel = CharacterLevel;
- player.CombatAnimationInterval = CombatAnimationInterval;
- player.CombatSprite = CombatSprite.Clone() as AnimatingSprite;
- player.Direction = Direction;
- player.EquippedEquipment.AddRange(EquippedEquipment);
- player.Experience = Experience;
- player.gold = gold;
- player.inactivePortraitTexture = inactivePortraitTexture;
- player.inactivePortraitTextureName = inactivePortraitTextureName;
- player.InitialEquipmentContentNames.AddRange(InitialEquipmentContentNames);
- player.introductionDialogue = introductionDialogue;
- player.Inventory.AddRange(Inventory);
- player.joinAcceptedDialogue = joinAcceptedDialogue;
- player.joinRejectedDialogue = joinRejectedDialogue;
- player.MapIdleAnimationInterval = MapIdleAnimationInterval;
- player.MapPosition = MapPosition;
- player.MapSprite = MapSprite.Clone() as AnimatingSprite;
- player.MapWalkingAnimationInterval = MapWalkingAnimationInterval;
- player.Name = Name;
- player.ShadowTexture = ShadowTexture;
- player.State = State;
- player.unselectablePortraitTexture = unselectablePortraitTexture;
- player.unselectablePortraitTextureName = unselectablePortraitTextureName;
- player.WalkingSprite = WalkingSprite.Clone() as AnimatingSprite;
- player.RecalculateEquipmentStatistics();
- player.RecalculateTotalDefenseRanges();
- player.RecalculateTotalTargetDamageRange();
- player.ResetAnimation(false);
- player.ResetBaseStatistics();
- return player;
- }
- public static Player Load(string playerPath, ContentManager contentManager)
- {
- var asset = XmlHelper.GetAssetElementFromXML(playerPath);
- var player = new Player
- {
- Name = (string)asset.Element("Name"),
- MapSprite = new AnimatingSprite
- {
- TextureName = (string)asset.Element("MapSprite").Element("TextureName"),
- Texture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\", (string)asset.Element("MapSprite").Element("TextureName"))),
- FrameDimensions = new Point(
- int.Parse(asset.Element("MapSprite").Element("FrameDimensions").Value.Split(' ')[0]),
- int.Parse(asset.Element("MapSprite").Element("FrameDimensions").Value.Split(' ')[1])),
- FramesPerRow = (int)asset.Element("MapSprite").Element("FramesPerRow"),
- SourceOffset = new Vector2(
- int.Parse(asset.Element("MapSprite").Element("SourceOffset").Value.Split(' ')[0]),
- int.Parse(asset.Element("MapSprite").Element("SourceOffset").Value.Split(' ')[1])),
- // Handle Animations if needed
- },
- WalkingSprite = new AnimatingSprite
- {
- TextureName = (string)asset.Element("WalkingSprite").Element("TextureName"),
- Texture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\", (string)asset.Element("WalkingSprite").Element("TextureName"))),
- FrameDimensions = new Point(
- int.Parse(asset.Element("WalkingSprite").Element("FrameDimensions").Value.Split(' ')[0]),
- int.Parse(asset.Element("WalkingSprite").Element("FrameDimensions").Value.Split(' ')[1])),
- FramesPerRow = (int)asset.Element("WalkingSprite").Element("FramesPerRow"),
- SourceOffset = new Vector2(
- int.Parse(asset.Element("WalkingSprite").Element("SourceOffset").Value.Split(' ')[0]),
- int.Parse(asset.Element("WalkingSprite").Element("SourceOffset").Value.Split(' ')[1])),
- // Handle Animations if needed
- },
- MapIdleAnimationInterval = (int)asset.Element("MapIdleAnimationInterval"),
- CharacterClassContentName = (string)asset.Element("CharacterClassContentName"),
- CharacterLevel = (int)asset.Element("CharacterLevel"),
- InitialEquipmentContentNames = asset.Element("InitialEquipmentContentNames")
- .Elements("Item").Select(x => (string)x).ToList(),
- CombatSprite = new AnimatingSprite
- {
- TextureName = (string)asset.Element("CombatSprite").Element("TextureName"),
- Texture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\", (string)asset.Element("CombatSprite").Element("TextureName"))),
- FrameDimensions = new Point(
- int.Parse(asset.Element("CombatSprite").Element("FrameDimensions").Value.Split(' ')[0]),
- int.Parse(asset.Element("CombatSprite").Element("FrameDimensions").Value.Split(' ')[1])),
- FramesPerRow = (int)asset.Element("CombatSprite").Element("FramesPerRow"),
- SourceOffset = new Vector2(
- int.Parse(asset.Element("CombatSprite").Element("SourceOffset").Value.Split(' ')[0]),
- int.Parse(asset.Element("CombatSprite").Element("SourceOffset").Value.Split(' ')[1])),
- // Handle Animations if needed
- },
- Gold = (int)asset.Element("Gold"),
- IntroductionDialogue = (string)asset.Element("IntroductionDialogue"),
- JoinAcceptedDialogue = (string)asset.Element("JoinAcceptedDialogue"),
- JoinRejectedDialogue = (string)asset.Element("JoinRejectedDialogue"),
- ActivePortraitTextureName = (string)asset.Element("ActivePortraitTextureName"),
- ActivePortraitTexture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("ActivePortraitTextureName"))),
- InactivePortraitTextureName = (string)asset.Element("InactivePortraitTextureName"),
- InactivePortraitTexture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("InactivePortraitTextureName"))),
- UnselectablePortraitTextureName = (string)asset.Element("UnselectablePortraitTextureName"),
- UnselectablePortraitTexture = contentManager.Load<Texture2D>(
- Path.Combine(@"Textures\Characters\Portraits", (string)asset.Element("UnselectablePortraitTextureName"))),
- Inventory = asset.Element("Inventory")?.Elements("Item")
- .Select(x => new ContentEntry<Gear>
- {
- ContentName = (string)x.Element("ContentName"),
- Count = (int?)x.Element("Count") ?? 1 // Default to 1 if not specified
- })
- .ToList() ?? new List<ContentEntry<Gear>>()
- };
- // load the character class
- player.CharacterClass = CharacterClass.Load(Path.Combine("CharacterClasses", player.CharacterClassContentName));
- return player;
- }
- }
- }
|