Vertex.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Microsoft.Xna.Framework;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. namespace OpenVIII.Battle.Dat
  8. {
  9. /// <summary>
  10. /// Section 2d: Vertex
  11. /// </summary>
  12. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/FileFormat_DAT#Useful_structures"/>
  13. [StructLayout(LayoutKind.Explicit, Pack = 1, Size = ByteSize)]
  14. [SuppressMessage("ReSharper", "UnusedMember.Local")]
  15. public class Vertex
  16. {
  17. #region Fields
  18. private const int ByteSize = 6;
  19. #endregion Fields
  20. #region Constructors
  21. private Vertex(short x, short y, short z)
  22. => (X, Y, Z) = (x, y, z);
  23. private Vertex(BinaryReader br)
  24. => (X, Y, Z) = (br.ReadInt16(), br.ReadInt16(), br.ReadInt16());
  25. private Vertex()
  26. {
  27. }
  28. #endregion Constructors
  29. #region Properties
  30. [field: FieldOffset(0)]
  31. public short X { get; }
  32. [field: FieldOffset(2)]
  33. public short Y { get; }
  34. [field: FieldOffset(4)]
  35. public short Z { get; }
  36. #endregion Properties
  37. #region Methods
  38. public static Vertex CreateInstance(short x, short y, short z) => new Vertex(x, y, z);
  39. public static Vertex CreateInstance(BinaryReader br) => Extended.ByteArrayToClass<Vertex>(br.ReadBytes(ByteSize));
  40. public static IReadOnlyList<Vector3> CreateInstances(BinaryReader br, ushort count) => Enumerable
  41. .Range(0, count).Select(_ => (Vector3)CreateInstance(br)).ToList().AsReadOnly();
  42. public static implicit operator Vector3(Vertex v) => new Vector3(-v.X / DatFile.ScaleHelper,
  43. -v.Z / DatFile.ScaleHelper, -v.Y / DatFile.ScaleHelper);
  44. public override string ToString() =>
  45. $"{nameof(X)}={X}, {nameof(Y)}={Y}, {nameof(Z)}={Z}, {nameof(Vector3)}={(Vector3)this}";
  46. #endregion Methods
  47. }
  48. }