PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/GameEngine/GameObjects/Actor.cs

https://bitbucket.org/begedin/tanked
C# | 131 lines | 86 code | 25 blank | 20 comment | 8 complexity | 8891e3217e9eec8bf2be9e1684ac8671 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using GameEngine.Drawing;
  5. using GameEngine.Interfaces;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace GameEngine.GameObjects
  10. {
  11. //An Actor class that should ideally be inherited for more precise functionality
  12. public class Actor : IGameDrawable
  13. {
  14. public float X { get; set; }
  15. public float Y { get; set; }
  16. public float Width { get; set; }
  17. public float Height { get; set; }
  18. public float Rotation { get; set; }
  19. public bool Visible { get; set; }
  20. public bool BoundingBoxVisible { get; set; }
  21. public Vector2 Origin { get; set; }
  22. public Color DrawColor { get; set; }
  23. public Dictionary<string, Animation> ActorAnimations { get; set; }
  24. //the current representation of the Actor which should be appriopiatly updated depending on its state
  25. public Animation CurrentAnimation { get; private set; }
  26. public string CurrentAnimationName { get; private set; }
  27. public Actor(float X, float Y, float Width = 1, float Height = 1, bool Visible = true)
  28. {
  29. this.X = X;
  30. this.Y = Y;
  31. this.Width = Width;
  32. this.Height = Height;
  33. this.Visible = Visible;
  34. this.DrawColor = Color.White;
  35. this.Origin = Vector2.Zero;
  36. this.BoundingBoxVisible = false;
  37. this.Rotation = 0;
  38. this.ActorAnimations = new Dictionary<string, Animation>();
  39. }
  40. public Texture2D GetTexture(GameTime GameTime)
  41. {
  42. return CurrentAnimation.SpriteSheet;
  43. }
  44. public Rectangle GetSourceRectangle(GameTime GameTime)
  45. {
  46. return CurrentAnimation.GetCurrentFrame(GameTime);
  47. }
  48. /// <summary>
  49. /// Sets the current animation to the specified animation name. The animation
  50. /// key name must exist in the ActorAnimations dictionary property for this actor
  51. /// or a KeyNotFoundException will be thrown. On succesful update, the CurrentAnimation
  52. /// property will be set to the specified animation.
  53. /// </summary>
  54. /// <param name="Name">String Name of the Animation to set the CurrentAnimation to.</param>
  55. public void SetCurrentAnimation(string Name)
  56. {
  57. CurrentAnimation = ActorAnimations[Name];
  58. CurrentAnimationName = Name;
  59. }
  60. /// <summary>
  61. /// Loads a Dictionary of Animations for the current actor as specified in an XML formatted .anim file.
  62. /// The method requires the string path to the xml file containing the animation data, a reference to the
  63. /// ContentManager, and optionally, a boolean value specifing whether the current animations should be
  64. /// cleared for the actor before loading the new ones.
  65. /// </summary>
  66. /// <param name="Path">String path to the XML formatted .anim file</param>
  67. /// <param name="Content">Reference to the ContentManager instance being used in the application</param>
  68. /// <param name="Clear">optional bool value specifying whether to clear the actors current animation dictionary. True by Default</param>
  69. public void LoadAnimationXML(string Path, ContentManager Content, bool Clear = true)
  70. {
  71. if (Clear)
  72. {
  73. this.ActorAnimations.Clear();
  74. CurrentAnimation = null;
  75. }
  76. XmlDocument document = new XmlDocument();
  77. document.Load(Path);
  78. foreach (XmlNode animNode in document.SelectNodes("Animations/Animation"))
  79. {
  80. //optional attributes
  81. XmlAttribute frameDelayAttr = animNode.Attributes["FrameDelay"];
  82. XmlAttribute loopAttr = animNode.Attributes["Loop"];
  83. int FrameDelay = (frameDelayAttr == null) ? Animation.FRAME_DELAY_DEFAULT : Convert.ToInt32(frameDelayAttr.Value);
  84. bool Loop = (loopAttr == null) ? false : Convert.ToBoolean(loopAttr.Value);
  85. string Name = Convert.ToString(animNode.Attributes["Name"].Value);
  86. string SpriteSheet = Convert.ToString(animNode.Attributes["SpriteSheet"].Value);
  87. XmlNodeList frameNodes = animNode.SelectNodes("Frames/Frame");
  88. Rectangle[] frames = new Rectangle[frameNodes.Count];
  89. for (int i = 0; i < frameNodes.Count; i++)
  90. {
  91. string[] tokens = frameNodes[i].InnerText.Split(',');
  92. if (tokens.Length != 4)
  93. throw new FormatException("Expected 4 Values for Frame Definition: X, Y, Width, Height");
  94. int X = Convert.ToInt32(tokens[0]);
  95. int Y = Convert.ToInt32(tokens[1]);
  96. int Width = Convert.ToInt32(tokens[2]);
  97. int Height = Convert.ToInt32(tokens[3]);
  98. frames[i] = new Rectangle(X, Y, Width, Height);
  99. }
  100. this.ActorAnimations[Name] = new Animation(Content.Load<Texture2D>(SpriteSheet), frames, FrameDelay, Loop);
  101. //if no current animation has been assigned, use the first animation found in the anim file (Considered Default)
  102. if (CurrentAnimation == null) CurrentAnimation = this.ActorAnimations[Name];
  103. }
  104. }
  105. }
  106. }