PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/XSpriter/CharacterReader.cs

https://bitbucket.org/dylanwolf/xspriter
C# | 119 lines | 101 code | 13 blank | 5 comment | 10 complexity | d4925baa38313a0c1652bf0c2051bcca MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace FuncWorks.XNA.XSpriter
  8. {
  9. public class CharacterReader : ContentTypeReader<CharacterData>
  10. {
  11. protected override CharacterData Read(ContentReader input, CharacterData existingInstance)
  12. {
  13. CharacterData character = new CharacterData();
  14. character.FramesPerSecond = input.ReadInt32();
  15. int count, count2, count3;
  16. // Get textures
  17. count = input.ReadInt32();
  18. character.Textures = new Image[count][];
  19. for (int i = 0; i < count; i++)
  20. {
  21. count2 = input.ReadInt32();
  22. character.Textures[i] = new Image[count2];
  23. for (int j = 0; j < count2; j++)
  24. {
  25. Image img = new Image();
  26. img.Pivot = input.ReadVector2();
  27. character.Textures[i][j] = img;
  28. }
  29. }
  30. // Get animations
  31. count = input.ReadInt32();
  32. character.Animations = new AnimationList();
  33. for (int i = 0; i < count; i++)
  34. {
  35. Animation anim = new Animation();
  36. anim.Name = input.ReadString();
  37. anim.Looping = input.ReadBoolean();
  38. anim.Length = input.ReadInt64();
  39. count2 = input.ReadInt32();
  40. anim.Frames = new Frame[count2];
  41. for (int j = 0; j < count2; j++)
  42. {
  43. Frame key = new Frame();
  44. count3 = input.ReadInt32();
  45. key.Objects = new FrameImage[count3];
  46. for (int k = 0; k < count3; k++)
  47. {
  48. FrameImage img = new FrameImage();
  49. img.Angle = input.ReadSingle();
  50. img.Clockwise = input.ReadBoolean();
  51. img.Pivot = input.ReadVector2();
  52. img.Position = input.ReadVector2();
  53. img.TextureFolder = input.ReadInt32();
  54. img.TextureFile = input.ReadInt32();
  55. img.TimelineId = input.ReadInt32();
  56. img.Parent = input.ReadInt32();
  57. img.Scale = input.ReadVector2();
  58. key.Objects[k] = img;
  59. }
  60. count3 = input.ReadInt32();
  61. key.Bones = new Bone[count3];
  62. for (int k = 0; k < count3; k++)
  63. {
  64. Bone bone = new Bone();
  65. bone.Angle = input.ReadSingle();
  66. bone.Clockwise = input.ReadBoolean();
  67. bone.Id = input.ReadInt32();
  68. bone.Parent = input.ReadInt32();
  69. bone.Position = input.ReadVector2();
  70. bone.Scale = input.ReadVector2();
  71. bone.TimelineId = input.ReadInt32();
  72. bone.Name = input.ReadString();
  73. key.Bones[k] = bone;
  74. }
  75. anim.Frames[j] = key;
  76. }
  77. // Read timeline/texture lookups
  78. count2 = input.ReadInt32();
  79. anim.TextureTimelines = new Dictionary<string, int>();
  80. for (int k = 0; k < count2; k++)
  81. {
  82. anim.TextureTimelines[input.ReadString()] = input.ReadInt32();
  83. }
  84. // Read timeline/bone lookups
  85. count2 = input.ReadInt32();
  86. anim.BoneTimelines = new Dictionary<string, int>();
  87. for (int k = 0; k < count2; k++)
  88. {
  89. anim.BoneTimelines[input.ReadString()] = input.ReadInt32();
  90. }
  91. character.Animations.Add(anim);
  92. }
  93. // Load textures
  94. for (int i = 0; i < character.Textures.Length; i++)
  95. {
  96. for (int j = 0; j < character.Textures[i].Length; j++)
  97. {
  98. character.Textures[i][j].Texture =
  99. input.ContentManager.Load<Texture2D>(String.Format("{0}/{1}/{2}", input.AssetName,
  100. i.ToString("00"), j.ToString("00")));
  101. }
  102. }
  103. return character;
  104. }
  105. }
  106. }