A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0KB

  1. using System;
  2. using Svelto.ECS;
  3. using Unity.Mathematics;
  4. using UnityEngine;
  5. using RobocraftX.Common;
  6. using RobocraftX.Physics;
  7. namespace GamecraftModdingAPI
  8. {
  9. /// <summary>
  10. /// A rigid body (like a chunk of connected blocks) during simulation.
  11. /// </summary>
  12. public class SimBody : IEquatable<SimBody>, IEquatable<EGID>
  13. {
  14. public EGID Id { get; }
  15. public SimBody(EGID id)
  16. {
  17. Id = id;
  18. }
  19. public SimBody(uint id) : this(new EGID(id, CommonExclusiveGroups.SIMULATION_BODIES_GROUP))
  20. {
  21. }
  22. /// <summary>
  23. /// The position of this body. When setting the position, update the position of the connected bodies as well,
  24. /// otherwise unexpected forces may arise.
  25. /// </summary>
  26. public float3 Position
  27. {
  28. get => GetStruct().position;
  29. set => GetStruct().position = value;
  30. }
  31. public float3 Velocity
  32. {
  33. get => GetStruct().velocity;
  34. set => GetStruct().velocity = value;
  35. }
  36. public float3 AngularVelocity
  37. {
  38. get => GetStruct().angularVelocity;
  39. set => GetStruct().angularVelocity = value;
  40. } //Delta versions are used internally, can't be set or get
  41. public float3 Rotation
  42. {
  43. get => ((Quaternion) GetStruct().rotation).eulerAngles;
  44. set
  45. {
  46. ref var str = ref GetStruct();
  47. Quaternion quaternion = str.rotation;
  48. quaternion.eulerAngles = value;
  49. str.rotation = quaternion;
  50. }
  51. }
  52. public float Mass
  53. {
  54. get => math.rcp(GetStruct().physicsMass.InverseMass);
  55. //set => GetStruct().physicsMass.InverseMass = math.rcp(value);
  56. }
  57. public float3 CenterOfMass
  58. {
  59. get => GetStruct().physicsMass.CenterOfMass;
  60. //set => GetStruct().physicsMass.CenterOfMass = value;
  61. }
  62. /// <summary>
  63. /// Whether the body can be moved or static.
  64. /// </summary>
  65. public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic; //Setting it doesn't have any effect
  66. /// <summary>
  67. /// The rigid bodies connected to this one via functional joints (broken ones don't count).
  68. /// </summary>
  69. public SimBody[] GetConnectedBodies()
  70. {
  71. return Block.BlockEngine.GetConnectedSimBodies(Id.entityID);
  72. }
  73. private ref RigidBodyEntityStruct GetStruct()
  74. {
  75. return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id);
  76. }
  77. public override string ToString()
  78. {
  79. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Mass)}: {Mass}, {nameof(Static)}: {Static}";
  80. }
  81. public bool Equals(SimBody other)
  82. {
  83. if (ReferenceEquals(null, other)) return false;
  84. if (ReferenceEquals(this, other)) return true;
  85. return Id.Equals(other.Id);
  86. }
  87. public bool Equals(EGID other)
  88. {
  89. return Id.Equals(other);
  90. }
  91. public override bool Equals(object obj)
  92. {
  93. if (ReferenceEquals(null, obj)) return false;
  94. if (ReferenceEquals(this, obj)) return true;
  95. if (obj.GetType() != this.GetType()) return false;
  96. return Equals((SimBody) obj);
  97. }
  98. public override int GetHashCode()
  99. {
  100. return Id.GetHashCode();
  101. }
  102. /// <summary>
  103. /// Returns the object identified by the given ID (A-Z).
  104. /// This has the same result as calling ObjectIdentifier.GetByID(id) and then GetRigidBody() with the duplicates filtered out.
  105. /// </summary>
  106. /// <param name="id">The alphabetical ID</param>
  107. /// <returns>An array that may be empty</returns>
  108. public static SimBody[] GetFromObjectID(char id) => Block.BlockEngine.GetSimBodiesFromID((byte) (id - 'A'));
  109. }
  110. }